Skip to content

fix(cpp): reject invalid Sprintz special-block value counts - #1126

Open
ColinLeeo wants to merge 1 commit into
apache:developfrom
ColinLeeo:sprintz-special-block-count
Open

ColinLeeo wants to merge 1 commit into
apache:developfrom
ColinLeeo:sprintz-special-block-count

Conversation

@ColinLeeo

Copy link
Copy Markdown
Contributor

The Sprintz decoder took a special block's value count from the header and used it to fill the fixed-size block buffer without validation. Encoders only write 1..block_size values in a special block; reject any other count (including 0) with E_TSFILE_CORRUPTED before indexing. Valid files are unaffected.

Tests: SprintzCodecTest.{Int32,Int64,Float,Double}SpecialBlockOversizedCountIsRejected and Int32SpecialBlockAtCapacityStillDecodes. Full C++ suite passes.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The malformed-count tests omit zero-count headers for all four decoder types, leaving a regression that returns a fabricated value with E_OK undetected.

1 open issue
What changed in this PR

This PR hardens C++ Sprintz decoding by rejecting invalid special-block value counts before writing to the fixed-size buffer, while preserving valid-file behavior.

Changes:

  • Reject zero and oversized counts with E_TSFILE_CORRUPTED across all four numeric decoders.
  • Add oversized-count regression tests and an INT32 test confirming the maximum valid special block still decodes.
File Description
cpp/​test/​encoding/​sprintz_codec_test.cc Adds malformed-count and valid-capacity regression tests.
cpp/​src/​encoding/​int64_sprintz_decoder.h Validates INT64 special-block counts before decoding.
cpp/​src/​encoding/​int32_sprintz_decoder.h Validates INT32 special-block counts before decoding.
cpp/​src/​encoding/​float_sprintz_decoder.h Validates FLOAT special-block counts before decoding.
cpp/​src/​encoding/​double_sprintz_decoder.h Validates DOUBLE special-block counts before decoding.

Review effort: Balanced

uint8_t header = static_cast<uint8_t>(0x80 | claimed_size);
ASSERT_EQ(page.write_buf(&header, 1), E_OK);
ASSERT_EQ(merge_byte_stream(page, payload, false), E_OK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants