Skip to content

Fix overflow panic for very small fp_rate in constructors - #26

Merged
arthurprs merged 2 commits into
arthurprs:masterfrom
Booyaka101:fix-fp-rate-overflow
Jun 8, 2026
Merged

Fix overflow panic for very small fp_rate in constructors#26
arthurprs merged 2 commits into
arthurprs:masterfrom
Booyaka101:fix-fp-rate-overflow

Conversation

@Booyaka101

Copy link
Copy Markdown
Contributor

Filter::new, Filter::new_resizeable, Filter::new_with_hasher, Filter::new_resizeable_with_hasher and filter_specs panic with "attempt to add with overflow" when given an extremely small fp_rate (e.g. 0.0 or f64::MIN_POSITIVE).

fp_rate is clamped to f64::MIN_POSITIVE, so (-fp_rate.log2()).ceil() reaches ~1074. That saturates the as u8 cast to 255, and the following u8 additions (+ (max_qbits - qbits) and the qbits + rbits > 64 check) overflow.

Clamp the rate to 2^-64 instead — the smallest rate that still fits in 64 fingerprint bits. The arithmetic can no longer overflow, and an unachievable rate now returns Error::NotEnoughFingerprintBits (as moderately-small rates already do) rather than panicking. Added a regression test covering all entry points.

Closes #25

Booyaka101 and others added 2 commits June 7, 2026 18:41
The filter constructors and `filter_specs` panic with "attempt to add with
overflow" when given an extremely small `fp_rate` (e.g. `0.0` or
`f64::MIN_POSITIVE`). The rate was clamped to `f64::MIN_POSITIVE`, so
`(-fp_rate.log2()).ceil()` reaches ~1074, the `as u8` cast saturates to 255,
and the following u8 additions overflow.

Clamp to 2^-64 instead (the smallest rate that fits in 64 fingerprint bits).
Unachievable rates now return Error::NotEnoughFingerprintBits, as
moderately-small rates already do, instead of panicking.

Closes arthurprs#25
Express the clamp floor directly as 1/2^64 (f64::powi isn't const) and
correct the doc comment: rates below 2^-64 are rejected by the existing
fingerprint_bits > 64 check, not necessarily by a u8 overflow.
@arthurprs

Copy link
Copy Markdown
Owner

Thank you!

@arthurprs
arthurprs merged commit dc2306b into arthurprs:master Jun 8, 2026
12 checks passed
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.

Filter::new_resizeable_with_hasher - attempt to add with overflow

2 participants