Skip to content

Commit 749b542

Browse files
committed
apacheGH-51222: Address binary slice review feedback
1 parent 5f8ddeb commit 749b542

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,8 @@ 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.
26452647
const int64_t stop = std::clamp(opt.stop, -input_bytes, input_bytes);
26462648
int64_t max_slice_bytes = (stop - opt.start + opt.step - 1) / opt.step;
26472649
return std::min(input_bytes, ninputs * std::max<int64_t>(0, max_slice_bytes));

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

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

27042704
TYPED_TEST(TestBinaryKernels, SliceBytesPosPos) {
2705+
constexpr auto max = std::numeric_limits<int64_t>::max();
27052706
SliceOptions options_default_stop{/*start=*/1};
27062707
const std::string long_input(300, 'x');
27072708
this->CheckUnary(
27082709
"binary_slice", this->template MakeArray<std::string>({"abcdef", long_input}),
27092710
this->template MakeArray<std::string>({"bcdef", std::string(299, 'x')}),
27102711
&options_default_stop);
2712+
SliceOptions options_default_stop_step{/*start=*/1, /*stop=*/max, /*step=*/5};
2713+
this->CheckUnary("binary_slice",
2714+
this->template MakeArray<std::string>({"abcdef", long_input}),
2715+
this->template MakeArray<std::string>({"b", std::string(60, 'x')}),
2716+
&options_default_stop_step);
27112717

27122718
SliceOptions options{2, 4};
27132719
this->CheckUnary(
@@ -2755,6 +2761,18 @@ TYPED_TEST(TestBinaryKernels, SliceBytesPosNeg) {
27552761
}
27562762

27572763
TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) {
2764+
constexpr auto min = std::numeric_limits<int64_t>::min();
2765+
const std::string long_input(300, 'x');
2766+
SliceOptions options_min{/*start=*/-1, /*stop=*/min};
2767+
this->CheckUnary("binary_slice",
2768+
this->template MakeArray<std::string>({"abcdef", long_input}),
2769+
this->template MakeArray<std::string>({"", ""}), &options_min);
2770+
SliceOptions options_min_step_neg{/*start=*/-1, /*stop=*/min, /*step=*/-5};
2771+
this->CheckUnary("binary_slice",
2772+
this->template MakeArray<std::string>({"abcdef", long_input}),
2773+
this->template MakeArray<std::string>({"fa", std::string(60, 'x')}),
2774+
&options_min_step_neg);
2775+
27582776
SliceOptions options{-2, -1};
27592777
this->CheckUnary(
27602778
"binary_slice",

0 commit comments

Comments
 (0)