Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2642,7 +2642,11 @@ struct SliceBytesTransform : StringSliceTransformBase {
// on the resulting slice lengths, so return a worst case estimate.
return input_bytes;
}
int64_t max_slice_bytes = (opt.stop - opt.start + opt.step - 1) / opt.step;
// SliceOptions uses INT64_MIN/MAX for open-ended bounds. Clamp both bounds
// before arithmetic so equivalent out-of-range indices cannot overflow the size estimate.
const int64_t start = std::clamp(opt.start, -input_bytes, input_bytes);
const int64_t stop = std::clamp(opt.stop, -input_bytes, input_bytes);
Comment thread
Dricmoy marked this conversation as resolved.
int64_t max_slice_bytes = (stop - start + opt.step - 1) / opt.step;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check: input "abc", start=1, the default stop, and step=INT64_MAX

The code around here is short, but the arithmetic is fragile. See if you can separate out boundary checks although this might as well be "one last boundary to enforce"...

return std::min(input_bytes, ninputs * std::max<int64_t>(0, max_slice_bytes));
}

Expand Down
30 changes: 30 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,19 @@ TYPED_TEST(TestBinaryKernels, SliceBytesBasic) {
}

TYPED_TEST(TestBinaryKernels, SliceBytesPosPos) {
constexpr auto max = std::numeric_limits<int64_t>::max();
SliceOptions options_default_stop{/*start=*/1};
const std::string long_input(300, 'x');
this->CheckUnary(
"binary_slice", this->template MakeArray<std::string>({"abcdef", long_input}),
this->template MakeArray<std::string>({"bcdef", std::string(299, 'x')}),
&options_default_stop);
Comment thread
Dricmoy marked this conversation as resolved.
SliceOptions options_default_stop_step{/*start=*/1, /*stop=*/max, /*step=*/5};
this->CheckUnary("binary_slice",
this->template MakeArray<std::string>({"abcdef", long_input}),
this->template MakeArray<std::string>({"b", std::string(60, 'x')}),
&options_default_stop_step);

SliceOptions options{2, 4};
this->CheckUnary(
"binary_slice",
Expand Down Expand Up @@ -2748,6 +2761,23 @@ TYPED_TEST(TestBinaryKernels, SliceBytesPosNeg) {
}

TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) {
constexpr auto min = std::numeric_limits<int64_t>::min();
const std::string long_input(300, 'x');
SliceOptions options_extreme_start{/*start=*/min, /*stop=*/-1};
this->CheckUnary("binary_slice", this->template MakeArray<std::string>({"x"}),
this->template MakeArray<std::string>({""}),
&options_extreme_start);

SliceOptions options_min{/*start=*/-1, /*stop=*/min};
this->CheckUnary("binary_slice",
this->template MakeArray<std::string>({"abcdef", long_input}),
this->template MakeArray<std::string>({"", ""}), &options_min);
SliceOptions options_min_step_neg{/*start=*/-1, /*stop=*/min, /*step=*/-5};
this->CheckUnary("binary_slice",
this->template MakeArray<std::string>({"abcdef", long_input}),
this->template MakeArray<std::string>({"fa", std::string(60, 'x')}),
&options_min_step_neg);

SliceOptions options{-2, -1};
this->CheckUnary(
"binary_slice",
Expand Down
Loading