Skip to content

Check header fields on deserialize (Bloom, HLL, density, tuple, VarOpt) - #608

Merged
proost merged 3 commits into
masterfrom
deserialize-header-checks
Dec 3, 2026
Merged

proost merged 3 commits into
masterfrom
deserialize-header-checks

Conversation

@leerho

@leerho leerho commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

This PR makes five readers check header fields that they used to take as given, so that an inconsistent image is rejected with an exception.

Changes

  • Bloom filter: a non-empty image must have the standard preamble length (4 longs), at least one hash function and a non-empty bit array. Checked for deserialize from bytes and from a stream, and for wrap and writable_wrap.
  • HLL: an image whose target type is not HLL_4 must not declare aux entries, since only HLL_4 has an aux map. Checked in both readers before the aux map is built.
  • Density: each level size is read only when the input has room for it, and must not exceed the retained items left to read. The stream reader stops at the end of the stream. The dimension must be at least 1, in the constructor and in both readers, so that every sketch that can be built can also be read back.
  • Compact tuple: a non-empty image must have 1, 2 or 3 preamble longs, and the bytes reader checks that all of them are present before reading them. The stream reader gets the same check.
  • VarOpt: the full-mode check h + r == k is done in 64 bits, and h must fit the arrays that the reader allocates.

Compatibility

Images that the library writes are unchanged and still read back. The one API change: density_sketch now throws std::invalid_argument for a dimension of 0 (in Python, a ValueError).

Tests

One new test per family builds an inconsistent image from a real serialized sketch and checks that both readers throw. Each new test fails on master and passes with this change. The bloom_filter, hll, density, tuple and var_opt test suites pass.

🤖 Generated with Claude Code

leerho and others added 2 commits November 30, 2026 17:28
A non-empty image must have the standard 4-long preamble, since the number of bits set follows the empty preamble, and at least one hash function. The constructor used for a non-empty image does not check the number of hashes, unlike the public ones. The existing check for an empty bit array moves into the same helper, used by the bytes and stream paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- HLL: an image whose target type is not HLL_4 must not declare aux
  entries; only HLL_4 has an aux map. Checked in both readers before
  the aux map is built.
- Density: each level size is read only when the input has room for it,
  and must not exceed the retained items left to read. The stream reader
  stops at the end of the stream. The dimension must be at least 1,
  in the constructor and in both readers, so that every sketch that
  can be built can also be read back.
- Compact tuple: a non-empty image must have 1, 2 or 3 preamble longs,
  and the bytes reader checks that all of them are present before
  reading them.
- VarOpt: the full-mode check h + r == k is done in 64 bits, and h must
  fit the arrays the reader allocates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leerho
leerho requested review from proost and tisonkun December 2, 2026 01:08
@coveralls

coveralls commented Dec 2, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 46966733390

Coverage increased (+0.007%) to 97.05%

Details

  • Coverage increased (+0.007%) from the base build.
  • Patch coverage: 2 uncovered changes across 1 file (54 of 56 lines covered, 96.43%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
sampling/include/var_opt_sketch_impl.hpp 6 4 66.67%
Total (5 files) 56 54 96.43%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 17558
Covered Lines: 17040
Line Coverage: 97.05%
Coverage Strength: 1890882.74 hits per line

💛 - Coveralls

@proost proost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What you covered is correct, But during reviews i found good to add.

@@ -356,8 +362,10 @@ density_sketch<T, K, A> density_sketch<T, K, A>::deserialize(const void* bytes,
Levels levels(allocator);
int64_t num_to_read = num_retained; // num_retained is uint32_t so this allows error checking

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

non-empty sketch case, so we should check "num_retained" is not 0 in here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, thanks. A non-empty image with num_retained 0 loaded as a sketch with no levels, and the next update() never returned. Both readers now reject it, with a test (62c4072).

}

template<typename A>
void HllArray<A>::checkAuxCount(uint32_t auxCount, target_hll_type tgtHllType) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

how about adding hllByteArr's token count and auxCount comparison too here? Maybe corrupted image can be different two counts.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed, that is worth checking. It is part of a follow-up PR on the HLL readers that I will open after this one, to keep this one small. There, each HLL_4 register holding the aux token must have an aux entry whose value does not fit in the register, and the number of such registers must equal auxCount, in both readers.

Suggested by proost in review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leerho
leerho requested a review from proost December 2, 2026 21:07

@proost proost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@proost
proost merged commit 240a4a8 into master Dec 3, 2026
35 checks passed
@proost
proost deleted the deserialize-header-checks branch December 3, 2026 02:52
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.

3 participants