contain-rs / bit-set

A Set of Bits
Apache License 2.0
63 stars 25 forks source link

shrink_to_fit method corrupts an empty bitset #25

Closed alexeiakimov closed 2 years ago

alexeiakimov commented 2 years ago

Bug in BitSet::shrink_to_fit method

Crate version: 0.5.2

Description: BitSet::shrink_to_fit(...) method being applied to a newly created bitset corrupts the internals state of the bit set. The following test demonstrates the problem:

#[test]
fn shrink_new_bit_set() {
    let mut set = BitSet::new();
    set.shrink_to_fit();
    set.contains(1);
}

The last line panics with message "index out of bounds". The cause is that in lib.rs, line 389 the trunc_len is set to 1 if there are no blocks, after truncation the vector of blocks is still empty but the number of bits is set to the size of the single block.

Gankra commented 2 years ago

wow, yep! good catch!

Gankra commented 2 years ago

fixed in 0.5.3, which is now published