Skip to content

Commit cead076

Browse files
Fix buffer overrun in test_simple16.cpp
1 parent eb74a8d commit cead076

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

unittest/test_simple16.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ TEST(Simple16Test, DecodesWithUnknownLength) {
1111
for (uint32_t i = 0; i < 128; ++i) {
1212
in.push_back(i);
1313
}
14-
verifyUnknownInputLengthDecode(codec, in);
14+
15+
// Simple16 may overrun the output buffer regardless of `n`, so a headroom of
16+
// 28 (the maximum number of elements in a single pack) is added.
17+
std::vector<uint32_t> decoded(in.size() + 28, 0);
18+
verifyUnknownInputLengthDecode(codec, in, decoded);
1519
}
1620
} // namespace FastPForLib

unittest/test_simple8b.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ TEST(Simple8bTest, DecodesWithUnknownLength) {
1111
for (uint32_t i = 0; i < 128; ++i) {
1212
in.push_back(i);
1313
}
14-
verifyUnknownInputLengthDecode(codec, in);
14+
15+
std::vector<uint32_t> decoded(in.size(), 0);
16+
verifyUnknownInputLengthDecode(codec, in, decoded);
1517
}
1618
} // namespace FastPForLib

unittest/util.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
namespace FastPForLib {
99

1010
template <class Codec>
11-
void verifyUnknownInputLengthDecode(Codec &codec, const std::vector<uint32_t> &in) {
11+
void verifyUnknownInputLengthDecode(Codec &codec,
12+
const std::vector<uint32_t> &in,
13+
std::vector<uint32_t> &decoded) {
1214
std::vector<uint32_t> encoded(in.size() * 2, 0);
1315
size_t encodedSize;
1416
codec.encodeArray(in.data(), in.size(), encoded.data(), encodedSize);
1517
encoded.resize(encodedSize);
1618

17-
std::vector<uint32_t> decoded(in.size(), 0);
1819
size_t n = in.size();
1920
const uint32_t *decodedUntil =
2021
codec.decodeArray(encoded.data(), 0, decoded.data(), n);
22+
decoded.resize(n);
2123

2224
// Check that the decoded size matches the input size.
2325
EXPECT_EQ(n, in.size());

0 commit comments

Comments
 (0)