Skip to content

Commit

Permalink
Checked arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Apr 12, 2023
1 parent 9b61c62 commit 185c095
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion arrow-buffer/src/buffer/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ impl<O: ArrowNativeType> OffsetBuffer<O> {

/// Create a new [`OffsetBuffer`] containing `len + 1` `0` values
pub fn new_zeroed(len: usize) -> Self {
let buffer = MutableBuffer::from_len_zeroed((len + 1) * std::mem::size_of::<O>());
let len_bytes = len
.checked_add(1)
.and_then(|o| o.checked_mul(std::mem::size_of::<O>()))
.expect("overflow");
let buffer = MutableBuffer::from_len_zeroed(len_bytes);
Self(buffer.into_buffer().into())
}

Expand Down Expand Up @@ -116,6 +120,18 @@ mod tests {
#[test]
fn offsets() {
OffsetBuffer::new(vec![0, 1, 2, 3].into());

let offsets = OffsetBuffer::<i32>::new_zeroed(3);
assert_eq!(offsets.as_ref(), &[0; 4]);

let offsets = OffsetBuffer::<i32>::new_zeroed(0);
assert_eq!(offsets.as_ref(), &[0; 1]);
}

#[test]
#[should_panic(expected = "overflow")]
fn offsets_new_zeroed_overflow() {
OffsetBuffer::<i32>::new_zeroed(usize::MAX);
}

#[test]
Expand Down

0 comments on commit 185c095

Please sign in to comment.