@@ -323,7 +323,26 @@ void TBufferFile::SetByteCount(ULong64_t cntpos, Bool_t packInVersion)
323323 && (fBufCur >= fBuffer )
324324 && static_cast <ULong64_t>(fBufCur - fBuffer ) <= std::numeric_limits<UInt_t>::max ()
325325 && " Byte count position is after the end of the buffer" );
326- const UInt_t cnt = UInt_t (fBufCur - fBuffer ) - UInt_t (cntpos) - sizeof (UInt_t);
326+
327+ // We can either make this unconditional or we could split the routine
328+ // in two, one with a new signature and guarantee to get the 64bit position
329+ // (which may be chunk number + local offset) and one with the old signature
330+ // which uses the stack to get the position and call the new one.
331+ // This (of course) also requires that we do the 'same' to the WriteVersion
332+ // routines.
333+ R__ASSERT ( !fByteCountStack .empty () );
334+ if (cntpos == kMaxInt ) {
335+ cntpos = fByteCountStack .back ();
336+ }
337+ fByteCountStack .pop_back ();
338+ // if we are not in the same TKey chunk or if the cntpos is too large to fit in UInt_t
339+ // let's postpone the writing of the byte count
340+ const ULong64_t full_cnt = ULong64_t (fBufCur - fBuffer ) - cntpos - sizeof (UInt_t);
341+ if (full_cnt >= kMaxMapCount ) {
342+ fByteCounts [cntpos] = full_cnt;
343+ return ;
344+ }
345+ UInt_t cnt = static_cast <UInt_t>(full_cnt);
327346 char *buf = (char *)(fBuffer + cntpos);
328347
329348 // if true, pack byte count in two consecutive shorts, so it can
@@ -2709,8 +2728,7 @@ void TBufferFile::WriteObjectClass(const void *actualObjectStart, const TClass *
27092728 }
27102729
27112730 // reserve space for leading byte count
2712- UInt_t cntpos = UInt_t (fBufCur -fBuffer );
2713- fBufCur += sizeof (UInt_t);
2731+ UInt_t cntpos = ReserveByteCount ();
27142732
27152733 // write class of object first
27162734 Int_t mapsize = fMap ->Capacity (); // The slot depends on the capacity and WriteClass might induce an increase.
@@ -3207,8 +3225,7 @@ UInt_t TBufferFile::WriteVersion(const TClass *cl, Bool_t useBcnt)
32073225 UInt_t cntpos = 0 ;
32083226 if (useBcnt) {
32093227 // reserve space for leading byte count
3210- cntpos = UInt_t (fBufCur -fBuffer );
3211- fBufCur += sizeof (UInt_t);
3228+ cntpos = ReserveByteCount ();
32123229 }
32133230
32143231 Version_t version = cl->GetClassVersion ();
@@ -3237,8 +3254,7 @@ UInt_t TBufferFile::WriteVersionMemberWise(const TClass *cl, Bool_t useBcnt)
32373254 UInt_t cntpos = 0 ;
32383255 if (useBcnt) {
32393256 // reserve space for leading byte count
3240- cntpos = UInt_t (fBufCur -fBuffer );
3241- fBufCur += sizeof (UInt_t);
3257+ cntpos = ReserveByteCount ();
32423258 }
32433259
32443260 Version_t version = cl->GetClassVersion ();
0 commit comments