fix: validate Bloom bit counts during deserialization - #190
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens BloomFilter deserialization by validating the serialized num_bits_set cache against the decoded bit array’s actual population count, preventing corrupted metadata from causing false negatives in contains.
Changes:
- Compute the bit-array popcount during Bloom deserialization and require
raw_num_bits_setto match it for non-dirty images. - Continue accepting the dirty sentinel (
u64::MAX) and recomputenum_bits_setfrom the decoded bit array. - Add regression tests covering undercount/overcount/zero-count corruption and dirty-sentinel recomputation behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| datasketches/src/bloom/sketch.rs | Validates num_bits_set against the computed popcount during deserialization; recomputes when the dirty sentinel is present. |
| datasketches/tests/serde_tests/bloom.rs | Adds regression tests ensuring inconsistent cached counts are rejected and dirty counts are recomputed while preserving membership. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
tisonkun
marked this pull request as draft
August 9, 2026 16:44
tisonkun
marked this pull request as ready for review
August 10, 2026 01:34
Member
Author
Member
|
In go, no issues for following this. And I also agree that more strict validation makes sense in here. |
Member
Author
Thanks for your feedback! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Validate the serialized Bloom
num_bits_setcache against the decoded bit array.For every non-empty image, deserialization now computes the population count. The dirty sentinel (
u64::MAX) continues to request a recount, while any other mismatched cached count returnsErrorKind::InvalidData.This prevents inconsistent metadata from making a non-empty bit array appear empty and causing
containsto return a false negative.Closes #187.
Regression coverage
The new tests verify that:
The existing Java, C++, and Go snapshot suites pass unchanged.
Relationship to other implementations
Current Java, C++, and Go readers, like the previous Rust implementation, trust non-dirty cached counts and only recount the dirty sentinel:
This PR intentionally makes Rust stricter. It does not change the wire format and accepts every internally consistent image, including the shared dirty-sentinel representation. It only rejects corrupted metadata that can violate the Bloom no-false-negative contract.
Validation
cargo x prepare-testdatacargo x checkcargo x testcargo x lint