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

BitIterator panics when retrieving length #6480

Closed
Jefffrey opened this issue Sep 29, 2024 · 2 comments · Fixed by #6495
Closed

BitIterator panics when retrieving length #6480

Jefffrey opened this issue Sep 29, 2024 · 2 comments · Fixed by #6495
Assignees
Labels
arrow Changes to the arrow crate bug good first issue Good for newcomers

Comments

@Jefffrey
Copy link
Contributor

Describe the bug

BitIterator panics when retrieving its length via ExactSizeIterator

To Reproduce

    #[test]
    fn test123() {
        let b = BitIterator::new(&[0b00000010], 0, 2);
        dbg!(b.len());
    }

Output:

arrow-rs$ cargo test -p arrow-buffer --lib util::bit_iterator::tests::test123 -- --nocapture
    Blocking waiting for file lock on build directory
   Compiling arrow-buffer v53.0.0 (/home/jeffrey/Code/arrow-rs/arrow-buffer)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 9.48s
     Running unittests src/lib.rs (/media/jeffrey/1tb_860evo_ssd/.cargo_target_cache/debug/deps/arrow_buffer-9f4040912c29ccf9)

running 1 test
thread 'util::bit_iterator::tests::test123' panicked at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/iter/traits/exact_size.rs:122:9:
assertion `left == right` failed
  left: None
 right: Some(0)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test util::bit_iterator::tests::test123 ... FAILED

failures:

failures:
    util::bit_iterator::tests::test123

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 128 filtered out; finished in 0.00s

error: test failed, to rerun pass `-p arrow-buffer --lib`

Expected behavior

Doesn't panic

Additional context

Panic is here: https://github.com/rust-lang/rust/blob/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/iter/traits/exact_size.rs#L122

Looks like BitIterator implements ExactSizeIterator:

impl<'a> ExactSizeIterator for BitIterator<'a> {}

But it does not override the default size_hint from Iterator:

impl<'a> Iterator for BitIterator<'a> {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
if self.current_offset == self.end_offset {
return None;
}
// Safety:
// offsets in bounds
let v = unsafe { get_bit_raw(self.buffer.as_ptr(), self.current_offset) };
self.current_offset += 1;
Some(v)
}
}

According to docs: https://doc.rust-lang.org/std/iter/trait.ExactSizeIterator.html

When doing so, the implementation of [Iterator::size_hint] must return the exact size of the iterator.

@Jefffrey Jefffrey added bug good first issue Good for newcomers labels Sep 29, 2024
@Beihao-Zhou
Copy link
Member

take

@alamb
Copy link
Contributor

alamb commented Oct 2, 2024

label_issue.py automatically added labels {'arrow'} from #6495

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate bug good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants