Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subtraction overflow in BitVec::last_one() and last_zero() #114

Closed
VilleHallivuori opened this issue Mar 31, 2021 · 1 comment
Closed

Subtraction overflow in BitVec::last_one() and last_zero() #114

VilleHallivuori opened this issue Mar 31, 2021 · 1 comment

Comments

@VilleHallivuori
Copy link

It seems that there is subraction overflow in BitVec last_one()/last_zero() on cases where there are not matches. Counting works properly.

Example to reproduce:

#[test]
fn test_bitvec_last_ones() {
    let bv = bitvec![0];
    assert_eq!(0, bv.count_ones());
    println!("bv={:?}", bv);
    assert_eq!(None, bv.last_one()); // <-- Causes underflow
}

Result:

---- test_bitvec_last_ones stdout ----
bv=BitVec<bitvec::order::Lsb0, usize> { addr: 0x7f7fcc0009a0, head: 000000, bits: 1, capacity: 64 } [0]
thread 'test_bitvec_last_ones' panicked at 'attempt to subtract with overflow', /home/vhallivu/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.22.0/src/slice/specialization.rs:200:21
stack backtrace:
   0: rust_begin_unwind
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/panicking.rs:92:14
   2: core::panicking::panic
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/panicking.rs:50:5
   3: bitvec::slice::specialization::<impl bitvec::slice::BitSlice<bitvec::order::Lsb0,T>>::sp_iter_ones_last
             at /home/vhallivu/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.22.0/src/slice/specialization.rs:200:6
   4: <bitvec::slice::iter::IterOnes<O,T> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back
             at /home/vhallivu/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.22.0/src/slice/iter.rs:2405:4
   5: bitvec::slice::BitSlice<O,T>::last_one
             at /home/vhallivu/.cargo/registry/src/github.com-1ecc6299db9ec823/bitvec-0.22.0/src/slice.rs:1209:3
   6: bitvec_test::test_bitvec_last_ones
             at ./src/main.rs:12:22
   7: bitvec_test::test_bitvec_last_ones::{{closure}}
             at ./src/main.rs:8:1
   8: core::ops::function::FnOnce::call_once
             at /home/vhallivu/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
   9: core::ops::function::FnOnce::call_once
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.


failures:
    test_bitvec_last_ones
@myrrlyn myrrlyn closed this as completed in 3775eba Apr 1, 2021
@myrrlyn
Copy link
Collaborator

myrrlyn commented Apr 1, 2021

... Huh.

This crash makes complete sense, but the fix absolutely does not.

I am now issuing backports for all the minor series currently live. Thank you for the report! Please re-open this issue if you encounter this again.

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

No branches or pull requests

2 participants