@@ -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) {
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
@@ -2702,8 +2721,7 @@ void TBufferFile::WriteObjectClass(const void *actualObjectStart, const TClass *
27022721 }
27032722
27042723 // reserve space for leading byte count
2705- UInt_t cntpos = UInt_t (fBufCur -fBuffer );
2706- fBufCur += sizeof (UInt_t);
2724+ UInt_t cntpos = ReserveByteCount ();
27072725
27082726 // write class of object first
27092727 Int_t mapsize = fMap ->Capacity (); // The slot depends on the capacity and WriteClass might induce an increase.
@@ -3183,8 +3201,7 @@ UInt_t TBufferFile::WriteVersion(const TClass *cl, Bool_t useBcnt)
31833201 UInt_t cntpos = 0 ;
31843202 if (useBcnt) {
31853203 // reserve space for leading byte count
3186- cntpos = UInt_t (fBufCur -fBuffer );
3187- fBufCur += sizeof (UInt_t);
3204+ cntpos = ReserveByteCount ();
31883205 }
31893206
31903207 Version_t version = cl->GetClassVersion ();
@@ -3213,8 +3230,7 @@ UInt_t TBufferFile::WriteVersionMemberWise(const TClass *cl, Bool_t useBcnt)
32133230 UInt_t cntpos = 0 ;
32143231 if (useBcnt) {
32153232 // reserve space for leading byte count
3216- cntpos = UInt_t (fBufCur -fBuffer );
3217- fBufCur += sizeof (UInt_t);
3233+ cntpos = ReserveByteCount ();
32183234 }
32193235
32203236 Version_t version = cl->GetClassVersion ();
0 commit comments