Skip to content

Commit 82185b4

Browse files
committed
[ntuple] RNTupleDecompressor::Unzip: replace some asserts with exceptions
These errors are due to misconstructed input rather than programming mistakes, so we should not assert on them, but give the caller the chance to handle them.
1 parent af1e4ce commit 82185b4

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

tree/ntuple/inc/ROOT/RNTupleZip.hxx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <RZip.h>
1818
#include <TError.h>
1919

20+
#include <ROOT/RError.hxx>
21+
2022
#include <algorithm>
2123
#include <array>
2224
#include <cstring>
@@ -116,15 +118,17 @@ public:
116118
int szSource;
117119
int szTarget;
118120
int retval = R__unzip_header(&szSource, source, &szTarget);
119-
R__ASSERT(retval == 0);
120-
R__ASSERT(szSource > 0);
121-
R__ASSERT(szTarget > szSource);
122-
R__ASSERT(static_cast<unsigned int>(szSource) <= nbytes);
123-
R__ASSERT(static_cast<unsigned int>(szTarget) <= dataLen);
121+
if (R__unlikely(!((retval == 0) && (szSource > 0) && (szTarget > szSource) &&
122+
(static_cast<unsigned int>(szSource) <= nbytes) &&
123+
(static_cast<unsigned int>(szTarget) <= dataLen)))) {
124+
throw ROOT::RException(R__FAIL("failed to unzip buffer header"));
125+
}
124126

125127
int unzipBytes = 0;
126128
R__unzip(&szSource, source, &szTarget, target, &unzipBytes);
127-
R__ASSERT(unzipBytes == szTarget);
129+
if (R__unlikely(unzipBytes != szTarget))
130+
throw ROOT::RException(R__FAIL(std::string("unexpected length after unzipping the buffer (wanted: ") +
131+
std::to_string(szTarget) + ", got: " + std::to_string(unzipBytes) + ")"));
128132

129133
target += szTarget;
130134
source += szSource;

tree/ntuple/test/ntuple_zip.cxx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,16 @@ TEST(RNTupleZip, LargeWithOutputBuffer)
5656
RNTupleDecompressor::Unzip(zipBuffer.get(), szZip, N, unzipBuffer.get());
5757
EXPECT_EQ(data, std::string_view(unzipBuffer.get(), N));
5858
}
59+
60+
TEST(RNTupleZip, CorruptedInput)
61+
{
62+
std::string data = "xxxxxxxxxxxxxxxxxxxxxxxx";
63+
auto zipBuffer = MakeUninitArray<unsigned char>(data.length());
64+
auto szZipped = RNTupleCompressor::Zip(data.data(), data.length(), 101, zipBuffer.get());
65+
EXPECT_LT(szZipped, data.length());
66+
auto unzipBuffer = MakeUninitArray<unsigned char>(data.length());
67+
// corrupt the buffer header
68+
memset(zipBuffer.get() + 1, 0xCD, 5);
69+
EXPECT_THROW(RNTupleDecompressor::Unzip(zipBuffer.get(), szZipped, data.length(), unzipBuffer.get()),
70+
ROOT::RException);
71+
}

0 commit comments

Comments
 (0)