Skip to content

Commit 61c9a9c

Browse files
committed
apacheGH-51222: Clamp binary slice estimate bounds
1 parent 749b542 commit 61c9a9c

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,10 +2642,11 @@ struct SliceBytesTransform : StringSliceTransformBase {
26422642
// on the resulting slice lengths, so return a worst case estimate.
26432643
return input_bytes;
26442644
}
2645-
// SliceOptions uses INT64_MIN/MAX for open-ended bounds. Clamp stop before
2646-
// arithmetic so equivalent out-of-range indices cannot overflow the size estimate.
2645+
// SliceOptions uses INT64_MIN/MAX for open-ended bounds. Clamp both bounds
2646+
// before arithmetic so equivalent out-of-range indices cannot overflow the size estimate.
2647+
const int64_t start = std::clamp(opt.start, -input_bytes, input_bytes);
26472648
const int64_t stop = std::clamp(opt.stop, -input_bytes, input_bytes);
2648-
int64_t max_slice_bytes = (stop - opt.start + opt.step - 1) / opt.step;
2649+
int64_t max_slice_bytes = (stop - start + opt.step - 1) / opt.step;
26492650
return std::min(input_bytes, ninputs * std::max<int64_t>(0, max_slice_bytes));
26502651
}
26512652

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,6 +2763,11 @@ TYPED_TEST(TestBinaryKernels, SliceBytesPosNeg) {
27632763
TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) {
27642764
constexpr auto min = std::numeric_limits<int64_t>::min();
27652765
const std::string long_input(300, 'x');
2766+
SliceOptions options_extreme_start{/*start=*/min, /*stop=*/-1};
2767+
this->CheckUnary("binary_slice", this->template MakeArray<std::string>({"x"}),
2768+
this->template MakeArray<std::string>({""}),
2769+
&options_extreme_start);
2770+
27662771
SliceOptions options_min{/*start=*/-1, /*stop=*/min};
27672772
this->CheckUnary("binary_slice",
27682773
this->template MakeArray<std::string>({"abcdef", long_input}),

0 commit comments

Comments
 (0)