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
23 changes: 22 additions & 1 deletion arrow-buffer/src/buffer/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,15 @@ impl MutableBuffer {
}

let bytes_to_repeat = size_of_val(slice_to_repeat);
let repeated_bytes = repeat_count
.checked_mul(bytes_to_repeat)
.expect("repeated slice byte length overflow");
self.len
.checked_add(repeated_bytes)
.expect("mutable buffer length overflow");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't love introducing panics (tho clearly that's better than UB).

Any possibility to make this API more robust?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it would be fine to add another function like try_repeat_slice_n_times... that returned a Result for those that wanted it


// Ensure capacity
self.reserve(repeat_count * bytes_to_repeat);
self.reserve(repeated_bytes);

// Save the length before we do all the copies to know where to start from
let length_before = self.len;
Expand Down Expand Up @@ -1474,6 +1480,21 @@ mod tests {
test_repeat_count(0, &[1i32, 2, 3]);
}

#[test]
#[should_panic(expected = "repeated slice byte length overflow")]
fn test_repeat_slice_count_multiply_overflow() {
let mut buffer = MutableBuffer::new(0);
buffer.repeat_slice_n_times(&[0_u64], usize::MAX / mem::size_of::<u64>() + 1);
}

#[test]
#[should_panic(expected = "mutable buffer length overflow")]
fn test_repeat_slice_count_len_overflow() {
let mut buffer = MutableBuffer::new(0);
buffer.push(0_u8);
buffer.repeat_slice_n_times(&[0_u8], usize::MAX);
}

#[test]
fn test_small_repeats_counts() {
// test any special implementation for small repeat counts
Expand Down
Loading