Skip to content

fix: validate Bloom bit counts during deserialization - #190

Merged
tisonkun merged 2 commits into
mainfrom
codex/validate-bloom-bit-count
Aug 10, 2026
Merged

fix: validate Bloom bit counts during deserialization#190
tisonkun merged 2 commits into
mainfrom
codex/validate-bloom-bit-count

Conversation

@tisonkun

@tisonkun tisonkun commented Aug 9, 2026

Copy link
Copy Markdown
Member

Summary

Validate the serialized Bloom num_bits_set cache 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 returns ErrorKind::InvalidData.

This prevents inconsistent metadata from making a non-empty bit array appear empty and causing contains to return a false negative.

Closes #187.

Regression coverage

The new tests verify that:

  • a cached count of zero over a non-empty bit array is rejected,
  • non-zero undercounts and overcounts are rejected,
  • the dirty sentinel is accepted and recomputed, and
  • inserted values remain present after the dirty-image decode.

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-testdata
  • cargo x check
  • cargo x test
  • cargo x lint

@tisonkun
tisonkun requested review from notfilippo and a lite review from Copilot August 9, 2026 16:23

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.

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_set to match it for non-dirty images.
  • Continue accepting the dirty sentinel (u64::MAX) and recompute num_bits_set from 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.

Comment thread datasketches/tests/serde_tests/bloom.rs
Comment thread datasketches/src/bloom/sketch.rs Outdated
@tisonkun
tisonkun marked this pull request as draft August 9, 2026 16:44
@tisonkun
tisonkun marked this pull request as ready for review August 10, 2026 01:34
@tisonkun

tisonkun commented Aug 10, 2026

Copy link
Copy Markdown
Member Author

Current Java, C++, and Go readers, like the previous Rust implementation, trust non-dirty cached counts and only recount the dirty sentinel:

cc @leerho @proost I'd appreicate it if you can check if it's an issue for Java/C++/Go impls also.

@proost

proost commented Aug 10, 2026

Copy link
Copy Markdown
Member

In go, no issues for following this. And I also agree that more strict validation makes sense in here.

@tisonkun

Copy link
Copy Markdown
Member Author

In go, no issues for following this. And I also agree that more strict validation makes sense in here.

Thanks for your feedback!

@tisonkun
tisonkun merged commit db0a20e into main Aug 10, 2026
10 checks passed
@tisonkun
tisonkun deleted the codex/validate-bloom-bit-count branch August 10, 2026 23:04
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.

Bloom deserialization trusts inconsistent num_bits_set metadata

3 participants