Skip to content

Bound the exceptional bits read to the validated length - #163

Merged
lalinsky merged 1 commit into
masterfrom
bound-exceptional-bits-read
Jul 27, 2026
Merged

lalinsky merged 1 commit into
masterfrom
bound-exceptional-bits-read

Conversation

@acoustid-bot

Copy link
Copy Markdown
Contributor

m_exceptional_bits is sized from num_exceptional_bits, but UnpackInt5Array was given the whole remainder of the input as its source range:

m_exceptional_bits.resize(GetUnpackedInt5ArraySize(GetPackedInt5ArraySize(num_exceptional_bits)));
UnpackInt5Array(input.begin() + offset, input.end(), m_exceptional_bits.begin());

UnpackInt5Array writes one output byte per 5 input bits and stops when the source runs out, so the number of writes follows the input length rather than the size of the destination. Any input longer than the fingerprint actually needs therefore writes past the end of the vector.

The check just above only establishes a lower bound:

if (input.size() < offset + GetPackedInt5ArraySize(num_exceptional_bits)) {

so it does not constrain this.

The fix passes the end iterator at the length that check already validated. Trailing data is then ignored, which is what the surrounding code assumes anyway — m_bits is truncated to the number of values the header declares, and everything after the exceptional bits was already unused.

UnpackInt3Array a few lines earlier is not affected: there the destination is sized from the same input length that drives the loop, so the two agree.

Testing

Added OneItemOneBitExceptWithTrailingData — the existing OneItemOneBitExcept case with four trailing bytes, asserting it still decodes to the same value.

On master that test aborts in a Debug build with a past-the-end iterator from the libstdc++ debug checks. With the fix, the full suite passes (98 tests, -DFFT_LIB=kissfft -DCMAKE_BUILD_TYPE=Debug).

m_exceptional_bits is sized from num_exceptional_bits, but UnpackInt5Array was
given the whole remainder of the input as its source range. That function is
bounded by the source, not the destination, so any input longer than the
fingerprint needs writes past the end of the vector.

The length check above it only establishes a lower bound on the input size,
which does not help here.

Pass the end iterator at the length that check validated. Trailing data is then
ignored, which is what the surrounding code already assumes.
@coderabbitai

coderabbitai Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 2a071bda-bf92-4697-84da-e1a4bd08f1cb

📥 Commits

Reviewing files that changed from the base of the PR and between ab48115 and f7387ee.

📒 Files selected for processing (2)
  • src/fingerprint_decompressor.cpp
  • tests/test_fingerprint_decompressor.cpp

Walkthrough

Changes

Fingerprint decompression

Layer / File(s) Summary
Bounded exceptional-bit decoding
src/fingerprint_decompressor.cpp, tests/test_fingerprint_decompressor.cpp
Exceptional-bit unpacking now uses the exact packed segment size, and a test verifies trailing bytes do not affect the decoded value or algorithm output.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main fix: limiting exceptional-bit reads to the validated length.
Description check ✅ Passed The description matches the patch and test changes, explaining the bounds fix and the trailing-data regression test.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bound-exceptional-bits-read

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@lalinsky
lalinsky merged commit 721b1ad into master Jul 27, 2026
27 checks passed
@lalinsky
lalinsky deleted the bound-exceptional-bits-read branch July 28, 2026 06:08
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