Repository navigation
Check header fields on deserialize (Bloom, HLL, density, tuple, VarOpt) - #608
Conversation
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>
Coverage Report for CI Build 46966733390Coverage increased (+0.007%) to 97.05%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
proost
left a comment
There was a problem hiding this comment.
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 | |||
There was a problem hiding this comment.
non-empty sketch case, so we should check "num_retained" is not 0 in here.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
how about adding hllByteArr's token count and auxCount comparison too here? Maybe corrupted image can be different two counts.
There was a problem hiding this comment.
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>
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
h + r == kis done in 64 bits, andhmust fit the arrays that the reader allocates.Compatibility
Images that the library writes are unchanged and still read back. The one API change:
density_sketchnow throwsstd::invalid_argumentfor a dimension of 0 (in Python, aValueError).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