diff --git a/arrow-array/src/array/fixed_size_list_array.rs b/arrow-array/src/array/fixed_size_list_array.rs index 4a338591e5aa..637767996dbc 100644 --- a/arrow-array/src/array/fixed_size_list_array.rs +++ b/arrow-array/src/array/fixed_size_list_array.rs @@ -539,7 +539,7 @@ mod tests { } #[test] - #[should_panic(expected = "assertion failed: (offset + length) <= self.len()")] + #[should_panic(expected = "assertion failed: end <= self.len()")] // Different error messages, so skip for now // https://github.com/apache/arrow-rs/issues/1545 #[cfg(not(feature = "force_validate"))] diff --git a/arrow-array/src/array/struct_array.rs b/arrow-array/src/array/struct_array.rs index fbc34ef0c85b..7e35eb6450b2 100644 --- a/arrow-array/src/array/struct_array.rs +++ b/arrow-array/src/array/struct_array.rs @@ -642,7 +642,7 @@ mod tests { } #[test] - #[should_panic(expected = "assertion failed: (offset + length) <= self.len()")] + #[should_panic(expected = "assertion failed: end <= self.len()")] fn test_struct_array_from_data_with_offset_and_length_error() { let int_arr = Int32Array::from(vec![1, 2, 3, 4, 5]); let int_field = Field::new("x", DataType::Int32, false); diff --git a/arrow-data/src/data.rs b/arrow-data/src/data.rs index c8202f564133..10770d39cb84 100644 --- a/arrow-data/src/data.rs +++ b/arrow-data/src/data.rs @@ -560,9 +560,12 @@ impl ArrayData { /// /// # Panics /// - /// Panics if `offset + length > self.len()`. + /// Panics if `offset + length` overflows or is greater than `self.len()`. pub fn slice(&self, offset: usize, length: usize) -> ArrayData { - assert!((offset + length) <= self.len()); + let end = offset + .checked_add(length) + .expect("offset + length overflow"); + assert!(end <= self.len()); if let DataType::Struct(_) = self.data_type() { // Slice into children @@ -2285,6 +2288,19 @@ mod tests { assert_eq!(data.null_count() - 1, new_data.null_count()); } + #[test] + #[should_panic(expected = "offset + length overflow")] + fn test_slice_panics_on_offset_length_overflow() { + let data = ArrayData::builder(DataType::Int32) + .len(4) + .add_buffer(make_i32_buffer(4)) + .build() + .unwrap(); + let sliced = data.slice(1, 3); + + sliced.slice(1, usize::MAX); + } + #[test] fn test_typed_offsets_length_overflow() { let data = ArrayData {