Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/slice/specialization/msb0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ where T: BitStore
Domain::Region { head, body, tail } => {
if let Some(elem) = tail {
let val = elem.load_value();
let dead_bits =
bits_of::<T::Mem>() - elem.tail().into_inner() as usize;
out -= val.trailing_zeros() as usize - dead_bits;
if has_one(val, elem.mask().into_inner()) {
let dead_bits = bits_of::<T::Mem>()
- elem.tail().into_inner() as usize;
out -= val.trailing_zeros() as usize - dead_bits;
return Some(out);
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,22 @@ fn issue_170() {
.expect("read should not fail");
assert_eq!(bytes, 1, "should read exactly 1 byte");
}

/** Test case for [Issue #166], opened by [@Dr-Emann].

`last_one` on an all-zero `Msb0` bit-slice subtracted from the output index
before checking whether the tail element held a `1`, underflowing `usize` and
panicking. `Lsb0` was unaffected. The subtraction must be guarded by the
`has_one` check, matching the `head`-element path.

[Issue #166]: https://github.com/ferrilab/bitvec/issues/166
[@Dr-Emann]: https://github.com/Dr-Emann
**/
#[test]
fn issue_166() {
assert_eq!(bits![usize, Msb0; 0].last_one(), None);
assert_eq!(bits![usize, Msb0; 0, 0, 0].last_one(), None);

assert_eq!(bits![usize, Msb0; 0, 1, 0].last_one(), Some(1));
assert_eq!(bits![usize, Msb0; 1].last_one(), Some(0));
}