Skip to content

Commit 3edfa46

Browse files
committed
use size_t for sizes in TBuffer
1 parent b74bd1c commit 3edfa46

4 files changed

Lines changed: 33 additions & 51 deletions

File tree

core/base/inc/TBuffer.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TBuffer : public TObject {
4747

4848
Bool_t fMode; //Read or write mode
4949
Int_t fVersion; //Buffer format version
50-
Int_t fBufSize; //Size of buffer
50+
size_t fBufSize; //Size of buffer
5151
char *fBuffer; //Buffer used to store objects
5252
char *fBufCur; //Current position in buffer
5353
char *fBufMax; //End of buffer
@@ -87,19 +87,19 @@ class TBuffer : public TObject {
8787
Bool_t IsWriting() const { return (fMode & kWrite) != 0; }
8888
void SetReadMode();
8989
void SetWriteMode();
90-
void SetBuffer(void *buf, UInt_t bufsiz = 0, Bool_t adopt = kTRUE, ReAllocCharFun_t reallocfunc = nullptr);
90+
void SetBuffer(void *buf, size_t bufsiz = 0, Bool_t adopt = kTRUE, ReAllocCharFun_t reallocfunc = nullptr);
9191
ReAllocCharFun_t GetReAllocFunc() const;
9292
void SetReAllocFunc(ReAllocCharFun_t reallocfunc = nullptr);
93-
void SetBufferOffset(Int_t offset = 0) { fBufCur = fBuffer+offset; }
93+
void SetBufferOffset(size_t offset = 0) { fBufCur = fBuffer+offset; }
9494
void SetParent(TObject *parent);
9595
TObject *GetParent() const;
9696
char *Buffer() const { return fBuffer; }
9797
char *GetCurrent() const { return fBufCur; }
98-
Int_t BufferSize() const { return fBufSize; }
98+
size_t BufferSize() const { return fBufSize; }
9999
void DetachBuffer() { fBuffer = nullptr; }
100-
Int_t Length() const { return (Int_t)(fBufCur - fBuffer); }
101-
void Expand(Int_t newsize, Bool_t copy = kTRUE); // expand buffer to newsize
102-
void AutoExpand(Int_t size_needed); // expand buffer to newsize
100+
size_t Length() const { return fBufCur - fBuffer; }
101+
void Expand(size_t newsize, Bool_t copy = kTRUE); // expand buffer to newsize
102+
void AutoExpand(size_t size_needed); // expand buffer to newsize
103103
Bool_t ByteSwapBuffer(Long64_t n, EDataType type); // Byte-swap N primitive-elements in the buffer
104104

105105
virtual Bool_t CheckObject(const TObject *obj) = 0;

core/base/src/TBuffer.cxx

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ Buffer base class used for serializing objects.
1919
#include "TClass.h"
2020
#include "TProcessID.h"
2121

22-
constexpr Int_t kExtraSpace = 8; // extra space at end of buffer (used for free block count)
23-
constexpr Int_t kMaxBufferSize = 0x7FFFFFFE; // largest possible size.
22+
constexpr size_t kExtraSpace = 8; // extra space at end of buffer (used for free block count)
2423

2524

2625
ClassImp(TBuffer);
@@ -72,8 +71,6 @@ TBuffer::TBuffer(EMode mode)
7271

7372
TBuffer::TBuffer(EMode mode, Int_t bufsiz)
7473
{
75-
if (bufsiz < 0)
76-
Fatal("TBuffer","Request to create a buffer with a negative size, likely due to an integer overflow: 0x%x for a max of 0x%x.", bufsiz, kMaxBufferSize);
7774
if (bufsiz < kMinimalSize) bufsiz = kMinimalSize;
7875
fBufSize = bufsiz;
7976
fMode = mode;
@@ -103,8 +100,6 @@ TBuffer::TBuffer(EMode mode, Int_t bufsiz)
103100

104101
TBuffer::TBuffer(EMode mode, Int_t bufsiz, void *buf, Bool_t adopt, ReAllocCharFun_t reallocfunc)
105102
{
106-
if (bufsiz < 0)
107-
Fatal("TBuffer","Request to create a buffer with a negative size, likely due to an integer overflow: 0x%x for a max of 0x%x.", bufsiz, kMaxBufferSize);
108103
fBufSize = bufsiz;
109104
fMode = mode;
110105
fVersion = 0;
@@ -115,6 +110,8 @@ TBuffer::TBuffer(EMode mode, Int_t bufsiz, void *buf, Bool_t adopt, ReAllocCharF
115110
if (buf) {
116111
fBuffer = (char *)buf;
117112
if ( (fMode&kWrite)!=0 ) {
113+
if (fBufSize <= kExtraSpace)
114+
Expand( kMinimalSize );
118115
fBufSize -= kExtraSpace;
119116
}
120117
if (!adopt) ResetBit(kIsOwner);
@@ -128,10 +125,6 @@ TBuffer::TBuffer(EMode mode, Int_t bufsiz, void *buf, Bool_t adopt, ReAllocCharF
128125
fBufMax = fBuffer + fBufSize;
129126

130127
SetReAllocFunc( reallocfunc );
131-
132-
if (buf && ( (fMode&kWrite)!=0 ) && fBufSize < 0) {
133-
Expand( kMinimalSize );
134-
}
135128
}
136129

137130
////////////////////////////////////////////////////////////////////////////////
@@ -155,15 +148,10 @@ TBuffer::~TBuffer()
155148
/// If the size_needed is larger than the current size, the policy
156149
/// is to expand to double the current size or the size_needed which ever is largest.
157150

158-
void TBuffer::AutoExpand(Int_t size_needed)
151+
void TBuffer::AutoExpand(size_t size_needed)
159152
{
160-
if (size_needed < 0) {
161-
Fatal("AutoExpand","Request to expand to a negative size, likely due to an integer overflow: 0x%x for a max of 0x%x.", size_needed, kMaxBufferSize);
162-
}
163153
if (size_needed > fBufSize) {
164-
Long64_t doubling = 2LLU * fBufSize;
165-
if (doubling > kMaxBufferSize)
166-
doubling = kMaxBufferSize;
154+
size_t doubling = fBufSize * 2;
167155
if (size_needed > doubling) {
168156
Expand(size_needed);
169157
} else {
@@ -184,7 +172,7 @@ void TBuffer::AutoExpand(Int_t size_needed)
184172
/// is provided, a Fatal error will be issued if the Buffer attempts to
185173
/// expand.
186174

187-
void TBuffer::SetBuffer(void *buf, UInt_t newsiz, Bool_t adopt, ReAllocCharFun_t reallocfunc)
175+
void TBuffer::SetBuffer(void *buf, size_t newsiz, Bool_t adopt, ReAllocCharFun_t reallocfunc)
188176
{
189177
if (fBuffer && TestBit(kIsOwner))
190178
delete [] fBuffer;
@@ -198,6 +186,8 @@ void TBuffer::SetBuffer(void *buf, UInt_t newsiz, Bool_t adopt, ReAllocCharFun_t
198186
fBufCur = fBuffer;
199187
if (newsiz > 0) {
200188
if ( (fMode&kWrite)!=0 ) {
189+
if (fBufSize <= kExtraSpace)
190+
Expand(kMinimalSize);
201191
fBufSize = newsiz - kExtraSpace;
202192
} else {
203193
fBufSize = newsiz;
@@ -206,10 +196,6 @@ void TBuffer::SetBuffer(void *buf, UInt_t newsiz, Bool_t adopt, ReAllocCharFun_t
206196
fBufMax = fBuffer + fBufSize;
207197

208198
SetReAllocFunc( reallocfunc );
209-
210-
if (buf && ( (fMode&kWrite)!=0 ) && fBufSize < 0) {
211-
Expand( kMinimalSize );
212-
}
213199
}
214200

215201
////////////////////////////////////////////////////////////////////////////////
@@ -220,13 +206,12 @@ void TBuffer::SetBuffer(void *buf, UInt_t newsiz, Bool_t adopt, ReAllocCharFun_t
220206
/// In order to avoid losing data, if the current length is greater than
221207
/// the requested size, we only shrink down to the current length.
222208

223-
void TBuffer::Expand(Int_t newsize, Bool_t copy)
209+
void TBuffer::Expand(size_t newsize, Bool_t copy)
224210
{
225-
Int_t l = Length();
211+
size_t l = Length();
226212
if ( (l > newsize) && copy ) {
227213
newsize = l;
228214
}
229-
const Int_t extraspace = (fMode&kWrite)!=0 ? kExtraSpace : 0;
230215

231216
if ( (fMode&kWrite)!=0 ) {
232217
fBuffer = fReAllocFunc(fBuffer, newsize+kExtraSpace,

io/io/src/TBufferFile.cxx

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,29 +2091,21 @@ void TBufferFile::WriteFastArray(const Short_t *h, Long64_t n)
20912091

20922092
////////////////////////////////////////////////////////////////////////////////
20932093
/// Write array of n ints into the I/O buffer.
2094-
/// \note Due to the current limit of the buffer size, the function aborts execution of the program in case of underflow or overflow. See https://github.com/root-project/root/issues/6734 for more details.
20952094

20962095
void TBufferFile::WriteFastArray(const Int_t *ii, Long64_t n)
20972096
{
20982097
if (n == 0) return;
20992098

2100-
constexpr Int_t dataWidth = 4;
2101-
const Int_t maxElements = (std::numeric_limits<Int_t>::max() - Length())/dataWidth;
2102-
if (n < 0 || n > maxElements)
2103-
{
2104-
Fatal("WriteFastArray", "Not enough space left in the buffer (1GB limit). %lld elements is greater than the max left of %d", n, maxElements);
2105-
return; // In case the user re-routes the error handler to not die when Fatal is called
2106-
}
2107-
2108-
Int_t l = sizeof(Int_t)*n;
2109-
if (fBufCur + l > fBufMax) AutoExpand(fBufSize+l);
2099+
size_t l = sizeof(Int_t) * n;
2100+
if (fBufCur + l > fBufMax)
2101+
AutoExpand(fBufSize + l);
21102102

21112103
#ifdef R__BYTESWAP
21122104
# ifdef USE_BSWAPCPY
21132105
bswapcpy32(fBufCur, ii, n);
21142106
fBufCur += l;
21152107
# else
2116-
for (int i = 0; i < n; i++)
2108+
for (size_t i = 0; i < n; i++)
21172109
tobuf(fBufCur, ii[i]);
21182110
# endif
21192111
#else
@@ -3458,7 +3450,8 @@ Int_t TBufferFile::ReadClassBuffer(const TClass *cl, void *pointer, Int_t versio
34583450
sinfo = (TStreamerInfo*)cl->GetConversionStreamerInfo( onFileClass, version );
34593451
if( !sinfo ) {
34603452
Error("ReadClassBuffer",
3461-
"Could not find the right streamer info to convert %s version %d into a %s, object skipped at offset %d",
3453+
"Could not find the right streamer info to convert %s version %d into a %s, "
3454+
"object skipped at offset %zu",
34623455
onFileClass->GetName(), version, cl->GetName(), Length() );
34633456
CheckByteCount(start, count, onFileClass);
34643457
return 0;
@@ -3474,7 +3467,7 @@ Int_t TBufferFile::ReadClassBuffer(const TClass *cl, void *pointer, Int_t versio
34743467
auto infos = cl->GetStreamerInfos();
34753468
auto ninfos = infos->GetSize();
34763469
if (version < -1 || version >= ninfos) {
3477-
Error("ReadClassBuffer", "class: %s, attempting to access a wrong version: %d, object skipped at offset %d",
3470+
Error("ReadClassBuffer", "class: %s, attempting to access a wrong version: %d, object skipped at offset %zu",
34783471
cl->GetName(), version, Length() );
34793472
CheckByteCount(start, count, cl);
34803473
return 0;
@@ -3505,7 +3498,8 @@ Int_t TBufferFile::ReadClassBuffer(const TClass *cl, void *pointer, Int_t versio
35053498
CheckByteCount(start, count, cl);
35063499
return 0;
35073500
} else {
3508-
Error("ReadClassBuffer", "Could not find the StreamerInfo for version %d of the class %s, object skipped at offset %d",
3501+
Error("ReadClassBuffer", "Could not find the StreamerInfo for version %d of the class %s, "
3502+
"object skipped at offset %zu",
35093503
version, cl->GetName(), Length() );
35103504
CheckByteCount(start, count, cl);
35113505
return 0;
@@ -3567,7 +3561,8 @@ Int_t TBufferFile::ReadClassBuffer(const TClass *cl, void *pointer, const TClass
35673561
sinfo = (TStreamerInfo*)cl->GetConversionStreamerInfo( onFileClass, version );
35683562
if( !sinfo ) {
35693563
Error("ReadClassBuffer",
3570-
"Could not find the right streamer info to convert %s version %d into a %s, object skipped at offset %d",
3564+
"Could not find the right streamer info to convert %s version %d into a %s, "
3565+
"object skipped at offset %zu",
35713566
onFileClass->GetName(), version, cl->GetName(), Length() );
35723567
CheckByteCount(R__s, R__c, onFileClass);
35733568
return 0;
@@ -3591,7 +3586,8 @@ Int_t TBufferFile::ReadClassBuffer(const TClass *cl, void *pointer, const TClass
35913586
Int_t infocapacity = infos->Capacity();
35923587
if (infocapacity) {
35933588
if (version < -1 || version >= infocapacity) {
3594-
Error("ReadClassBuffer","class: %s, attempting to access a wrong version: %d, object skipped at offset %d",
3589+
Error("ReadClassBuffer","class: %s, attempting to access a wrong version: %d, "
3590+
"object skipped at offset %zu",
35953591
cl->GetName(), version, Length());
35963592
CheckByteCount(R__s, R__c, cl);
35973593
return 0;
@@ -3653,7 +3649,8 @@ Int_t TBufferFile::ReadClassBuffer(const TClass *cl, void *pointer, const TClass
36533649
CheckByteCount(R__s, R__c, cl);
36543650
return 0;
36553651
} else {
3656-
Error( "ReadClassBuffer", "Could not find the StreamerInfo for version %d of the class %s, object skipped at offset %d",
3652+
Error("ReadClassBuffer", "Could not find the StreamerInfo for version %d of the class %s, "
3653+
"object skipped at offset %zu",
36573654
version, cl->GetName(), Length() );
36583655
CheckByteCount(R__s, R__c, cl);
36593656
return 0;

io/io/src/TStreamerInfoWriteBuffer.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Int_t TStreamerInfo::WriteBufferAux(TBuffer &b, const T &arr,
151151
} else {
152152
if (gDebug > 1) {
153153
printf("WriteBuffer, class:%s, name=%s, fType[%d]=%d,"
154-
" %s, bufpos=%d, arr=%p, eoffset=%d, Redirect=%p\n",
154+
" %s, bufpos=%zu, arr=%p, eoffset=%d, Redirect=%p\n",
155155
fClass->GetName(), aElement->GetName(), i, compinfo[i]->fType, aElement->ClassName(),
156156
b.Length(), arr[0], eoffset, b.PeekDataCache()->GetObjectAt(0));
157157
}

0 commit comments

Comments
 (0)