Skip to content

Commit becd145

Browse files
committed
apacheGH-51222: [C++] Avoid overflow in binary_slice output sizing
1 parent 976779b commit becd145

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎cpp/src/arrow/compute/kernels/scalar_string_ascii.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2640,7 +2640,8 @@ struct SliceBytesTransform : StringSliceTransformBase {
26402640
// on the resulting slice lengths, so return a worst case estimate.
26412641
return input_bytes;
26422642
}
2643-
int64_t max_slice_bytes = (opt.stop - opt.start + opt.step - 1) / opt.step;
2643+
const int64_t stop = std::clamp(opt.stop, -input_bytes, input_bytes);
2644+
int64_t max_slice_bytes = (stop - opt.start + opt.step - 1) / opt.step;
26442645
return std::min(input_bytes, ninputs * std::max<int64_t>(0, max_slice_bytes));
26452646
}
26462647

‎cpp/src/arrow/compute/kernels/scalar_string_test.cc‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,13 @@ TYPED_TEST(TestBinaryKernels, SliceBytesBasic) {
27022702
}
27032703

27042704
TYPED_TEST(TestBinaryKernels, SliceBytesPosPos) {
2705+
SliceOptions options_default_stop{/*start=*/1};
2706+
const std::string long_input(300, 'x');
2707+
this->CheckUnary(
2708+
"binary_slice", this->template MakeArray<std::string>({"abcdef", long_input}),
2709+
this->template MakeArray<std::string>({"bcdef", std::string(299, 'x')}),
2710+
&options_default_stop);
2711+
27052712
SliceOptions options{2, 4};
27062713
this->CheckUnary(
27072714
"binary_slice",

0 commit comments

Comments
 (0)