Skip to content

Mask trailing bits in with_capacity_and_blocks - #151

Open
amalsaidov wants to merge 1 commit into
petgraph:masterfrom
amalsaidov:fix-with-capacity-and-blocks-trailing-bits
Open

amalsaidov wants to merge 1 commit into
petgraph:masterfrom
amalsaidov:fix-with-capacity-and-blocks-trailing-bits

Conversation

@amalsaidov

Copy link
Copy Markdown

Problem

with_capacity_and_blocks stores the provided blocks verbatim, including any bits set beyond the requested capacity in the final block.

Every other part of the crate upholds the invariant that bits at index >= length are unset: contains/count_ones mask them off, and the existing with_blocks_too_big* tests explicitly assert that such bits are not part of the set. But PartialEq, Hash and Ord compare the raw blocks (as_simd_slice), so those stray bits leak through and two logically-equal bitsets can compare unequal and hash differently:

let mut a = FixedBitSet::with_capacity(10);
a.insert_range(..);
let b = FixedBitSet::with_capacity_and_blocks(10, std::iter::repeat(!0));

assert_eq!(a.count_ones(..), b.count_ones(..)); // 10 == 10
assert_eq!(a.len(), b.len());                   // 10 == 10
assert_eq!(a, b);                               // panics: a != b

This also means such a value hashes differently from its logical equal, so it misbehaves as a key in hash-based collections.

Fix

Clear the out-of-range bits in the final block at the end of with_capacity_and_blocks, so the constructed bitset upholds the same invariant as every other constructor/operation. The mask touches only the last used block and leaves well-formed inputs (trailing bits already zero) unchanged — including the documented example and all existing with_blocks_* tests.

Test

Added with_blocks_too_big_eq_and_hash, which asserts that a bitset built via insert_range and one built via with_capacity_and_blocks with the same length and in-range bits are equal, hash equally, order equally, and expose identical blocks. It fails before the change and passes after. Full suite (cargo test, incl. the quickcheck tests) passes.

Fixes #139

with_capacity_and_blocks stored the provided blocks verbatim, including
any bits set beyond the requested capacity in the final block. Every
other operation upholds the invariant that bits at index >= length are
unset (contains/count_ones mask them off, and the existing tests assert
they are not part of the set), but PartialEq, Hash and Ord compare the
raw blocks. As a result two logically-equal bitsets could compare
unequal and hash differently, e.g.:

    let mut a = FixedBitSet::with_capacity(10);
    a.insert_range(..);
    let b = FixedBitSet::with_capacity_and_blocks(10, repeat(!0));
    assert_eq!(a, b); // failed: a != b

Clear the out-of-range bits in the final block so the constructed
bitset upholds the invariant.

Fixes petgraph#139
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.

with_capacity_and_blocks() can create bitsets that don't compare equal to logically identical ones

1 participant