Skip to content
Merged
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
56 changes: 55 additions & 1 deletion arrow-buffer/src/util/bit_chunk_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ pub struct BitChunks<'a> {
impl<'a> BitChunks<'a> {
/// Create a new [`BitChunks`] from a byte array, and an offset and length in bits
pub fn new(buffer: &'a [u8], offset: usize, len: usize) -> Self {
assert!(ceil(offset + len, 8) <= buffer.len() * 8);
assert!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that buffer.len() is already in bytes, right? So multiplying by 8 scales it too much

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

ceil(offset + len, 8) <= buffer.len(),
"offset + len out of bounds"
);

let byte_offset = offset / 8;
let bit_offset = offset % 8;
Expand Down Expand Up @@ -476,6 +479,57 @@ mod tests {
assert_eq!(0x7F, bitchunks.remainder_bits());
}

#[test]
#[should_panic(expected = "offset + len out of bounds")]
fn test_out_of_bound_should_panic_length_is_more_than_buffer_length() {
const ALLOC_SIZE: usize = 4 * 1024;
let input = vec![0xFF_u8; ALLOC_SIZE];

let buffer: Buffer = Buffer::from_vec(input);

// We are reading more than exists in the buffer
buffer.bit_chunks(0, (ALLOC_SIZE + 1) * 8);
}

#[test]
#[should_panic(expected = "offset + len out of bounds")]
fn test_out_of_bound_should_panic_length_is_more_than_buffer_length_but_not_when_not_using_ceil()
{
const ALLOC_SIZE: usize = 4 * 1024;
let input = vec![0xFF_u8; ALLOC_SIZE];

let buffer: Buffer = Buffer::from_vec(input);

// We are reading more than exists in the buffer
buffer.bit_chunks(0, (ALLOC_SIZE * 8) + 1);
}

#[test]
#[should_panic(expected = "offset + len out of bounds")]
fn test_out_of_bound_should_panic_when_offset_is_not_zero_and_length_is_the_entire_buffer_length()
{
const ALLOC_SIZE: usize = 4 * 1024;
let input = vec![0xFF_u8; ALLOC_SIZE];

let buffer: Buffer = Buffer::from_vec(input);

// We are reading more than exists in the buffer
buffer.bit_chunks(8, ALLOC_SIZE * 8);
}

#[test]
#[should_panic(expected = "offset + len out of bounds")]
fn test_out_of_bound_should_panic_when_offset_is_not_zero_and_length_is_the_entire_buffer_length_with_ceil()
{
const ALLOC_SIZE: usize = 4 * 1024;
let input = vec![0xFF_u8; ALLOC_SIZE];

let buffer: Buffer = Buffer::from_vec(input);

// We are reading more than exists in the buffer
buffer.bit_chunks(1, ALLOC_SIZE * 8);
}

#[test]
#[allow(clippy::assertions_on_constants)]
fn test_unaligned_bit_chunk_iterator() {
Expand Down
Loading