This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12252-b09dcfbf21e7161b6a8d7b4404e1e04825a710c1 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 1207168b9fd151e9e87b22a527675fd4e195c0fe Author: RIchard Baah <[email protected]> AuthorDate: Thu Nov 19 02:38:37 2026 +0000 refactor : hoist ByteView offset check (#12252) Relates to #12210. Follow-up to #12212 and https://github.com/apache/arrow-rs/pull/12212#discussion_r4568078515. Cap `max_buffer_size` at `i32::MAX` before copying and use that bound for the `u32` offset conversions. The only non-test caller already passes `i32::MAX`, so this is a cleanup with no behavior change. The removed `expect` could only fire if the caller violated the `selected_bytes` contract; debug assertions now catch that mistake in tests and Miri. --- arrow-select/src/coalesce/byte_view.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/arrow-select/src/coalesce/byte_view.rs b/arrow-select/src/coalesce/byte_view.rs index 2e7ebf61c0..53984d97a9 100644 --- a/arrow-select/src/coalesce/byte_view.rs +++ b/arrow-select/src/coalesce/byte_view.rs @@ -416,6 +416,8 @@ impl<B: ByteViewType> InProgressByteViewArray<B> { } } + /// Copies indexed views, capping `max_buffer_size` at `i32::MAX`. + /// /// # Safety /// /// Every value in `indices` must be less than `src_views.len()`, and every @@ -443,6 +445,7 @@ impl<B: ByteViewType> InProgressByteViewArray<B> { } return; } + let max_buffer_size = max_buffer_size.min(i32::MAX as usize); let mut dst_buffer = match self.current.take() { Some(current) @@ -502,10 +505,10 @@ impl<B: ByteViewType> InProgressByteViewArray<B> { .get_unchecked(source_buffer_index) .get_unchecked(source_buffer_offset..source_buffer_offset + length) }; - view.offset = dst_buffer - .len() - .try_into() - .expect("byte view offset exceeds u32::MAX"); + // `selected_bytes` keeps the buffer within `max_buffer_size`, which + // is at most `i32::MAX`, so the offset fits in `u32`. + debug_assert!(dst_buffer.len() + length <= max_buffer_size); + view.offset = dst_buffer.len() as u32; view.buffer_index = buffer_index; debug_assert!(dst_buffer.capacity() - dst_buffer.len() >= length); dst_buffer.extend_from_slice(src); @@ -524,8 +527,9 @@ impl<B: ByteViewType> InProgressByteViewArray<B> { /// /// Every value in `indices` must be less than `src_views.len()`, every /// selected non-inline view must reference a valid range in `src_buffers`, - /// every selected value must fit within `max_buffer_size`, and - /// `selected_bytes` must equal their total length. + /// every selected value must fit within `max_buffer_size`, + /// `max_buffer_size` must be at most `i32::MAX`, and `selected_bytes` must + /// equal their total length. #[cold] #[inline(never)] unsafe fn append_indexed_views_and_copy_strings_split( @@ -574,10 +578,10 @@ impl<B: ByteViewType> InProgressByteViewArray<B> { .get_unchecked(buffer_index) .get_unchecked(buffer_offset..buffer_offset + str_len) }; - view.offset = dst_buffer - .len() - .try_into() - .expect("byte view offset exceeds u32::MAX"); + // The buffer stays within `max_buffer_size`, which is at most + // `i32::MAX`, so the offset fits in `u32`. + debug_assert!(dst_buffer.len() + str_len <= max_buffer_size); + view.offset = dst_buffer.len() as u32; view.buffer_index = self.completed.len().try_into().expect("too many buffers"); debug_assert!(dst_buffer.capacity() - dst_buffer.len() >= str_len); dst_buffer.extend_from_slice(src); @@ -1289,7 +1293,7 @@ mod tests { &indices, typed.data_buffers(), selected_bytes, - selected_bytes, + usize::MAX, ); } let actual = in_progress.finish().unwrap();
