From 5f83fdafe5cb608f9b414bfe86791b569247d369 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Wed, 18 Nov 2026 15:59:15 +0800 Subject: [PATCH] fix(cpp): reject invalid Sprintz special-block value counts The Sprintz special-block header stores its value count in the low 7 bits of the header byte. decode_block() used that count unchecked and wrote current_buffer_[i] for every entry, but current_buffer_ holds only block_size_ + 1 values while a malformed header can claim up to 127. Encoders write 1..block_size values in a special block and never 0, so reject any other count with E_TSFILE_CORRUPTED before indexing. Applied to the int32, int64, float and double decoders. Valid files are unaffected; tests cover counts of 9 and 127 and the valid 8-value block. --- cpp/src/encoding/double_sprintz_decoder.h | 7 ++ cpp/src/encoding/float_sprintz_decoder.h | 7 ++ cpp/src/encoding/int32_sprintz_decoder.h | 7 ++ cpp/src/encoding/int64_sprintz_decoder.h | 7 ++ cpp/test/encoding/sprintz_codec_test.cc | 108 ++++++++++++++++++++++ 5 files changed, 136 insertions(+) diff --git a/cpp/src/encoding/double_sprintz_decoder.h b/cpp/src/encoding/double_sprintz_decoder.h index bee5ecece..00e01ff67 100644 --- a/cpp/src/encoding/double_sprintz_decoder.h +++ b/cpp/src/encoding/double_sprintz_decoder.h @@ -128,7 +128,14 @@ class DoubleSprintzDecoder : public SprintzDecoder { bit_width_ = static_cast(bit_width); if ((bit_width_ & (1 << 7)) != 0) { + // A special block stores its value count in the low 7 bits of the + // header byte. Encoders write 1..block_size; any other count + // would either index past current_buffer_ (block_size_ + 1 + // entries) or describe a block no encoder can produce. decode_size_ = bit_width_ & ~(1 << 7); + if (decode_size_ == 0 || decode_size_ > block_size_) { + return common::E_TSFILE_CORRUPTED; + } DoubleGorillaDecoder decoder; for (int i = 0; i < decode_size_; ++i) { if (RET_FAIL(decoder.read_double(current_buffer_[i], input))) { diff --git a/cpp/src/encoding/float_sprintz_decoder.h b/cpp/src/encoding/float_sprintz_decoder.h index 7cabb18ca..baceedfec 100644 --- a/cpp/src/encoding/float_sprintz_decoder.h +++ b/cpp/src/encoding/float_sprintz_decoder.h @@ -129,7 +129,14 @@ class FloatSprintzDecoder : public SprintzDecoder { bit_width_ = static_cast(bit_width); if ((bit_width_ & (1 << 7)) != 0) { + // A special block stores its value count in the low 7 bits of the + // header byte. Encoders write 1..block_size; any other count + // would either index past current_buffer_ (block_size_ + 1 + // entries) or describe a block no encoder can produce. decode_size_ = bit_width_ & ~(1 << 7); + if (decode_size_ == 0 || decode_size_ > block_size_) { + return common::E_TSFILE_CORRUPTED; + } FloatGorillaDecoder decoder; for (int i = 0; i < decode_size_; ++i) { if (RET_FAIL(decoder.read_float(current_buffer_[i], input))) { diff --git a/cpp/src/encoding/int32_sprintz_decoder.h b/cpp/src/encoding/int32_sprintz_decoder.h index d361acecd..a845fdc8a 100644 --- a/cpp/src/encoding/int32_sprintz_decoder.h +++ b/cpp/src/encoding/int32_sprintz_decoder.h @@ -131,7 +131,14 @@ class Int32SprintzDecoder : public SprintzDecoder { bit_width_ = static_cast(bit_width); if ((bit_width_ & (1 << 7)) != 0) { + // A special block stores its value count in the low 7 bits of the + // header byte. Encoders write 1..block_size; any other count + // would either index past current_buffer_ (block_size_ + 1 + // entries) or describe a block no encoder can produce. decode_size_ = bit_width_ & ~(1 << 7); + if (decode_size_ == 0 || decode_size_ > block_size_) { + return common::E_TSFILE_CORRUPTED; + } Int32RleDecoder decoder; for (int i = 0; i < decode_size_; ++i) { if (RET_FAIL(decoder.read_int(current_buffer_[i], input))) { diff --git a/cpp/src/encoding/int64_sprintz_decoder.h b/cpp/src/encoding/int64_sprintz_decoder.h index e7870fd73..57ddab578 100644 --- a/cpp/src/encoding/int64_sprintz_decoder.h +++ b/cpp/src/encoding/int64_sprintz_decoder.h @@ -131,7 +131,14 @@ class Int64SprintzDecoder : public SprintzDecoder { bit_width_ = static_cast(bit_width); if ((bit_width_ & (1 << 7)) != 0) { + // A special block stores its value count in the low 7 bits of the + // header byte. Encoders write 1..block_size; any other count + // would either index past current_buffer_ (block_size_ + 1 + // entries) or describe a block no encoder can produce. decode_size_ = bit_width_ & ~(1 << 7); + if (decode_size_ == 0 || decode_size_ > block_size_) { + return common::E_TSFILE_CORRUPTED; + } Int64RleDecoder decoder; for (int i = 0; i < decode_size_; ++i) { if (RET_FAIL(decoder.read_int(current_buffer_[i], input))) { diff --git a/cpp/test/encoding/sprintz_codec_test.cc b/cpp/test/encoding/sprintz_codec_test.cc index d06ee205b..1d04dd3ea 100644 --- a/cpp/test/encoding/sprintz_codec_test.cc +++ b/cpp/test/encoding/sprintz_codec_test.cc @@ -1313,4 +1313,112 @@ TEST_F(SprintzCodecTest, JavaGoldenDouble) { values.data(), static_cast(values.size())); } +// Builds a Sprintz page whose first byte is a special-block header claiming +// `claimed_size` values, followed by the RLE/Gorilla payload that a malformed +// file uses to satisfy every claimed entry. +void BuildSpecialBlockPage(int claimed_size, ByteStream& payload, + ByteStream& page) { + uint8_t header = static_cast(0x80 | claimed_size); + ASSERT_EQ(page.write_buf(&header, 1), E_OK); + ASSERT_EQ(merge_byte_stream(page, payload, false), E_OK); +} + +// A malformed special-block header may claim any count in 0..127, but +// current_buffer_ only holds block_size_ + 1 values and encoders only write +// 1..block_size. The decoder must reject such a block instead of writing past +// the buffer. +TEST_F(SprintzCodecTest, Int32SpecialBlockOversizedCountIsRejected) { + for (int claimed : {9, 127}) { + SCOPED_TRACE(::testing::Message() << "claimed=" << claimed); + ByteStream payload(1024, MOD_ENCODER_OBJ); + Int32RleEncoder encoder; + for (int i = 0; i < claimed; ++i) { + ASSERT_EQ(encoder.encode(1000 + i, payload), E_OK); + } + ASSERT_EQ(encoder.flush(payload), E_OK); + ByteStream page(4096, MOD_DECODER_OBJ); + BuildSpecialBlockPage(claimed, payload, page); + + Int32SprintzDecoder decoder; + int32_t value = 0; + EXPECT_EQ(decoder.read_int32(value, page), common::E_TSFILE_CORRUPTED); + } +} + +TEST_F(SprintzCodecTest, Int64SpecialBlockOversizedCountIsRejected) { + for (int claimed : {9, 127}) { + SCOPED_TRACE(::testing::Message() << "claimed=" << claimed); + ByteStream payload(1024, MOD_ENCODER_OBJ); + Int64RleEncoder encoder; + for (int i = 0; i < claimed; ++i) { + ASSERT_EQ( + encoder.encode(static_cast(1) << 40 | i, payload), + E_OK); + } + ASSERT_EQ(encoder.flush(payload), E_OK); + ByteStream page(4096, MOD_DECODER_OBJ); + BuildSpecialBlockPage(claimed, payload, page); + + Int64SprintzDecoder decoder; + int64_t value = 0; + EXPECT_EQ(decoder.read_int64(value, page), common::E_TSFILE_CORRUPTED); + } +} + +TEST_F(SprintzCodecTest, FloatSpecialBlockOversizedCountIsRejected) { + for (int claimed : {9, 127}) { + SCOPED_TRACE(::testing::Message() << "claimed=" << claimed); + ByteStream payload(1024, MOD_ENCODER_OBJ); + FloatGorillaEncoder encoder; + for (int i = 0; i < claimed; ++i) { + ASSERT_EQ(encoder.encode(1000.25f + i, payload), E_OK); + } + ASSERT_EQ(encoder.flush(payload), E_OK); + ByteStream page(4096, MOD_DECODER_OBJ); + BuildSpecialBlockPage(claimed, payload, page); + + FloatSprintzDecoder decoder; + float value = 0; + EXPECT_EQ(decoder.read_float(value, page), common::E_TSFILE_CORRUPTED); + } +} + +TEST_F(SprintzCodecTest, DoubleSpecialBlockOversizedCountIsRejected) { + for (int claimed : {9, 127}) { + SCOPED_TRACE(::testing::Message() << "claimed=" << claimed); + ByteStream payload(1024, MOD_ENCODER_OBJ); + DoubleGorillaEncoder encoder; + for (int i = 0; i < claimed; ++i) { + ASSERT_EQ(encoder.encode(1000.25 + i, payload), E_OK); + } + ASSERT_EQ(encoder.flush(payload), E_OK); + ByteStream page(4096, MOD_DECODER_OBJ); + BuildSpecialBlockPage(claimed, payload, page); + + DoubleSprintzDecoder decoder; + double value = 0; + EXPECT_EQ(decoder.read_double(value, page), common::E_TSFILE_CORRUPTED); + } +} + +// Eight values is the largest special block an encoder writes; it must keep +// decoding after the count validation above. +TEST_F(SprintzCodecTest, Int32SpecialBlockAtCapacityStillDecodes) { + Int32SprintzEncoder encoder; + ByteStream stream(128, MOD_ENCODER_OBJ); + for (int i = 0; i < 8; ++i) { + ASSERT_EQ(encoder.encode(i * 7, stream), E_OK); + } + ASSERT_EQ(encoder.flush(stream), E_OK); + + Int32SprintzDecoder decoder; + for (int i = 0; i < 8; ++i) { + int32_t actual = 0; + ASSERT_TRUE(decoder.has_remaining(stream)); + ASSERT_EQ(decoder.read_int32(actual, stream), E_OK); + ASSERT_EQ(actual, i * 7); + } + ASSERT_FALSE(decoder.has_remaining(stream)); +} + } // namespace