Fix overflow panic for very small fp_rate in constructors - #26
Merged
Conversation
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.
Owner
|
Thank you! |
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.
Filter::new,Filter::new_resizeable,Filter::new_with_hasher,Filter::new_resizeable_with_hasherandfilter_specspanic with "attempt to add with overflow" when given an extremely smallfp_rate(e.g.0.0orf64::MIN_POSITIVE).fp_rateis clamped tof64::MIN_POSITIVE, so(-fp_rate.log2()).ceil()reaches ~1074. That saturates theas u8cast to 255, and the followingu8additions (+ (max_qbits - qbits)and theqbits + rbits > 64check) overflow.Clamp the rate to
2^-64instead — the smallest rate that still fits in 64 fingerprint bits. The arithmetic can no longer overflow, and an unachievable rate now returnsError::NotEnoughFingerprintBits(as moderately-small rates already do) rather than panicking. Added a regression test covering all entry points.Closes #25