Skip to content

Commit 0024260

Browse files
authored
Replace interleave overflow panic with error (#9549)
# Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. --> - Closes #NNN. # Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> # What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Replace interleave overflow panic with error # Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes UT # Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please call them out. -->
1 parent 3931179 commit 0024260

1 file changed

Lines changed: 40 additions & 6 deletions

File tree

arrow-select/src/interleave.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,15 @@ fn interleave_bytes<T: ByteArrayType>(
173173
let mut capacity = 0;
174174
let mut offsets = Vec::with_capacity(indices.len() + 1);
175175
offsets.push(T::Offset::from_usize(0).unwrap());
176-
offsets.extend(indices.iter().map(|(a, b)| {
176+
for (a, b) in indices {
177177
let o = interleaved.arrays[*a].value_offsets();
178178
let element_len = o[*b + 1].as_usize() - o[*b].as_usize();
179179
capacity += element_len;
180-
T::Offset::from_usize(capacity).expect("overflow")
181-
}));
180+
offsets.push(
181+
T::Offset::from_usize(capacity)
182+
.ok_or_else(|| ArrowError::OffsetOverflowError(capacity))?,
183+
);
184+
}
182185

183186
let mut values = Vec::with_capacity(capacity);
184187
for (a, b) in indices {
@@ -331,12 +334,14 @@ fn interleave_list<O: OffsetSizeTrait>(
331334
let mut capacity = 0usize;
332335
let mut offsets = Vec::with_capacity(indices.len() + 1);
333336
offsets.push(O::from_usize(0).unwrap());
334-
offsets.extend(indices.iter().map(|(array, row)| {
337+
for (array, row) in indices {
335338
let o = interleaved.arrays[*array].value_offsets();
336339
let element_len = o[*row + 1].as_usize() - o[*row].as_usize();
337340
capacity += element_len;
338-
O::from_usize(capacity).expect("offset overflow")
339-
}));
341+
offsets.push(
342+
O::from_usize(capacity).ok_or_else(|| ArrowError::OffsetOverflowError(capacity))?,
343+
);
344+
}
340345

341346
let mut child_indices = Vec::with_capacity(capacity);
342347
for (array, row) in indices {
@@ -1414,4 +1419,33 @@ mod tests {
14141419
]
14151420
);
14161421
}
1422+
1423+
#[test]
1424+
fn test_interleave_bytes_offset_overflow() {
1425+
let indices: Vec<(usize, usize)> = vec![(0, 0); (i32::MAX >> 4) as usize];
1426+
let text = ('a'..='z').collect::<String>();
1427+
let values = StringArray::from(vec![Some(text)]);
1428+
assert!(matches!(
1429+
interleave(&[&values], &indices),
1430+
Err(ArrowError::OffsetOverflowError(_))
1431+
));
1432+
}
1433+
1434+
#[test]
1435+
fn test_interleave_list_offset_overflow() {
1436+
// Build a ListArray<i32> with a single row containing many elements
1437+
let mut builder = GenericListBuilder::<i32, _>::new(Int32Builder::new());
1438+
for i in 0..32 {
1439+
builder.values().append_value(i);
1440+
}
1441+
builder.append(true);
1442+
let list = builder.finish();
1443+
1444+
// Interleave enough copies to overflow i32 offsets
1445+
let indices: Vec<(usize, usize)> = vec![(0, 0); (i32::MAX as usize / 32) + 1];
1446+
assert!(matches!(
1447+
interleave(&[&list], &indices),
1448+
Err(ArrowError::OffsetOverflowError(_))
1449+
));
1450+
}
14171451
}

0 commit comments

Comments
 (0)