Skip to content

Commit 43a7dc3

Browse files
committed
[io] adjust array length checks in text-based buffers
Text-based buffers do not have 1GB limitation for object store. But all kind of loops over array indicies made with Int_t so elements count cann't be larger than max int value. Adjust error message
1 parent 607c4aa commit 43a7dc3

3 files changed

Lines changed: 11 additions & 19 deletions

File tree

io/io/src/TBufferJSON.cxx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3346,11 +3346,9 @@ void TBufferJSON::JsonWriteFastArray(const T *arr, Long64_t arrsize, const char
33463346
fValue.Append("[]");
33473347
return;
33483348
}
3349-
constexpr Int_t dataWidth = 1; // at least 1
3350-
const Int_t maxElements = (std::numeric_limits<Int_t>::max() - Length())/dataWidth;
3351-
if (arrsize > maxElements)
3352-
{
3353-
Fatal("JsonWriteFastArray", "Not enough space left in the buffer (1GB limit). %lld elements is greater than the max left of %d", arrsize, maxElements);
3349+
const Int_t maxElements = std::numeric_limits<Int_t>::max();
3350+
if (arrsize > maxElements) {
3351+
Fatal("JsonWriteFastArray", "Array larger than 2^31 elements cannot be stored in JSON");
33543352
return; // In case the user re-routes the error handler to not die when Fatal is called
33553353
}
33563354

io/sql/src/TBufferSQL2.cxx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,15 +1376,12 @@ Int_t TBufferSQL2::SqlReadArraySize()
13761376
template <typename T>
13771377
R__ALWAYS_INLINE void TBufferSQL2::SqlWriteArray(T *arr, Long64_t arrsize, Bool_t withsize)
13781378
{
1379-
constexpr Int_t dataWidth = 1; // at least 1
1380-
const Int_t maxElements = (std::numeric_limits<Int_t>::max() - Length())/dataWidth;
1381-
if (arrsize < 0 || arrsize > maxElements)
1382-
{
1383-
Fatal("SqlWriteArray", "Not enough space left in the buffer (1GB limit). %lld elements is greater than the max left of %d", arrsize, maxElements);
1384-
return; // In case the user re-routes the error handler to not die when Fatal is called
1385-
}
13861379
if (!withsize && (arrsize <= 0))
13871380
return;
1381+
if (arrsize > std::numeric_limits<Int_t>::max()) {
1382+
Fatal("SqlWriteArray", "Array larger than 2^31 elements cannot be stored in SQL");
1383+
return; // In case the user re-routes the error handler to not die when Fatal is called
1384+
}
13881385
PushStack()->SetArray(withsize ? arrsize : -1);
13891386
Int_t indx = 0;
13901387
if (fCompressLevel > 0) {

io/xml/src/TBufferXML.cxx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,16 +2160,13 @@ void TBufferXML::WriteArray(const Double_t *d, Int_t n)
21602160
template <typename T>
21612161
R__ALWAYS_INLINE void TBufferXML::XmlWriteFastArray(const T *arr, Long64_t n)
21622162
{
2163-
constexpr Int_t dataWidth = 1; // at least 1
2164-
const Int_t maxElements = (std::numeric_limits<Int_t>::max() - Length())/dataWidth;
2165-
if (n < 0 || n > maxElements)
2166-
{
2167-
Fatal("XmlWriteFastArray", "Not enough space left in the buffer (1GB limit). %lld elements is greater than the max left of %d", n, maxElements);
2168-
return; // In case the user re-routes the error handler to not die when Fatal is called
2169-
}
21702163
BeforeIOoperation();
21712164
if (n <= 0)
21722165
return;
2166+
if (n > std::numeric_limits<Int_t>::max()) {
2167+
Fatal("XmlWriteFastArray", "Array larger than 2^31 elements cannot be stored in XML");
2168+
return; // In case the user re-routes the error handler to not die when Fatal is called
2169+
}
21732170
XMLNodePointer_t arrnode = CreateItemNode(xmlio::Array);
21742171
PushStack(arrnode);
21752172
XmlWriteArrayContent(arr, n);

0 commit comments

Comments
 (0)