From becd14518e44711d743f88c49e4e8c55a2236452 Mon Sep 17 00:00:00 2001 From: Dricmoy Date: Wed, 16 Sep 2026 17:11:50 -0600 Subject: [PATCH 1/4] GH-51222: [C++] Avoid overflow in binary_slice output sizing --- cpp/src/arrow/compute/kernels/scalar_string_ascii.cc | 3 ++- cpp/src/arrow/compute/kernels/scalar_string_test.cc | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc index dd3c507ef53e..2bf0a3229a98 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc @@ -2640,7 +2640,8 @@ 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; + const int64_t stop = std::clamp(opt.stop, -input_bytes, input_bytes); + int64_t max_slice_bytes = (stop - opt.start + opt.step - 1) / opt.step; return std::min(input_bytes, ninputs * std::max(0, max_slice_bytes)); } diff --git a/cpp/src/arrow/compute/kernels/scalar_string_test.cc b/cpp/src/arrow/compute/kernels/scalar_string_test.cc index 74650c1d76c5..66eef0965a7e 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_test.cc @@ -2702,6 +2702,13 @@ TYPED_TEST(TestBinaryKernels, SliceBytesBasic) { } TYPED_TEST(TestBinaryKernels, SliceBytesPosPos) { + SliceOptions options_default_stop{/*start=*/1}; + const std::string long_input(300, 'x'); + this->CheckUnary( + "binary_slice", this->template MakeArray({"abcdef", long_input}), + this->template MakeArray({"bcdef", std::string(299, 'x')}), + &options_default_stop); + SliceOptions options{2, 4}; this->CheckUnary( "binary_slice", From e7e6639ca4d9357e77ae65e2f8147900ace164b0 Mon Sep 17 00:00:00 2001 From: Dricmoy Date: Wed, 14 Oct 2026 11:11:42 -0600 Subject: [PATCH 2/4] GH-51222: Address binary slice review feedback --- .../compute/kernels/scalar_string_ascii.cc | 2 ++ .../compute/kernels/scalar_string_test.cc | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc index 2bf0a3229a98..0c6182ae1257 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc @@ -2640,6 +2640,8 @@ struct SliceBytesTransform : StringSliceTransformBase { // on the resulting slice lengths, so return a worst case estimate. return input_bytes; } + // SliceOptions uses INT64_MIN/MAX for open-ended bounds. Clamp stop before + // arithmetic so equivalent out-of-range indices cannot overflow the size estimate. const int64_t stop = std::clamp(opt.stop, -input_bytes, input_bytes); int64_t max_slice_bytes = (stop - opt.start + opt.step - 1) / opt.step; return std::min(input_bytes, ninputs * std::max(0, max_slice_bytes)); diff --git a/cpp/src/arrow/compute/kernels/scalar_string_test.cc b/cpp/src/arrow/compute/kernels/scalar_string_test.cc index 66eef0965a7e..733373c25d77 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_test.cc @@ -2702,12 +2702,18 @@ TYPED_TEST(TestBinaryKernels, SliceBytesBasic) { } TYPED_TEST(TestBinaryKernels, SliceBytesPosPos) { + constexpr auto max = std::numeric_limits::max(); SliceOptions options_default_stop{/*start=*/1}; const std::string long_input(300, 'x'); this->CheckUnary( "binary_slice", this->template MakeArray({"abcdef", long_input}), this->template MakeArray({"bcdef", std::string(299, 'x')}), &options_default_stop); + SliceOptions options_default_stop_step{/*start=*/1, /*stop=*/max, /*step=*/5}; + this->CheckUnary("binary_slice", + this->template MakeArray({"abcdef", long_input}), + this->template MakeArray({"b", std::string(60, 'x')}), + &options_default_stop_step); SliceOptions options{2, 4}; this->CheckUnary( @@ -2755,6 +2761,18 @@ TYPED_TEST(TestBinaryKernels, SliceBytesPosNeg) { } TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) { + constexpr auto min = std::numeric_limits::min(); + const std::string long_input(300, 'x'); + SliceOptions options_min{/*start=*/-1, /*stop=*/min}; + this->CheckUnary("binary_slice", + this->template MakeArray({"abcdef", long_input}), + this->template MakeArray({"", ""}), &options_min); + SliceOptions options_min_step_neg{/*start=*/-1, /*stop=*/min, /*step=*/-5}; + this->CheckUnary("binary_slice", + this->template MakeArray({"abcdef", long_input}), + this->template MakeArray({"fa", std::string(60, 'x')}), + &options_min_step_neg); + SliceOptions options{-2, -1}; this->CheckUnary( "binary_slice", From 4b75d502fcf515e6f78abfe5fdbc1082cf1e6c49 Mon Sep 17 00:00:00 2001 From: Dricmoy Date: Sun, 18 Oct 2026 16:18:31 -0600 Subject: [PATCH 3/4] GH-51222: Clamp binary slice estimate bounds --- cpp/src/arrow/compute/kernels/scalar_string_ascii.cc | 7 ++++--- cpp/src/arrow/compute/kernels/scalar_string_test.cc | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc index 0c6182ae1257..d42b2e947d96 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc @@ -2640,10 +2640,11 @@ struct SliceBytesTransform : StringSliceTransformBase { // on the resulting slice lengths, so return a worst case estimate. return input_bytes; } - // SliceOptions uses INT64_MIN/MAX for open-ended bounds. Clamp stop before - // arithmetic so equivalent out-of-range indices cannot overflow the size estimate. + // 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); - int64_t max_slice_bytes = (stop - opt.start + opt.step - 1) / opt.step; + int64_t max_slice_bytes = (stop - start + opt.step - 1) / opt.step; return std::min(input_bytes, ninputs * std::max(0, max_slice_bytes)); } diff --git a/cpp/src/arrow/compute/kernels/scalar_string_test.cc b/cpp/src/arrow/compute/kernels/scalar_string_test.cc index 733373c25d77..340a5efba177 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_test.cc @@ -2763,6 +2763,11 @@ TYPED_TEST(TestBinaryKernels, SliceBytesPosNeg) { TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) { constexpr auto min = std::numeric_limits::min(); const std::string long_input(300, 'x'); + SliceOptions options_extreme_start{/*start=*/min, /*stop=*/-1}; + this->CheckUnary("binary_slice", this->template MakeArray({"x"}), + this->template MakeArray({""}), + &options_extreme_start); + SliceOptions options_min{/*start=*/-1, /*stop=*/min}; this->CheckUnary("binary_slice", this->template MakeArray({"abcdef", long_input}), From 6c0d83f99d85088e6dc38d70a690ae2e8855920a Mon Sep 17 00:00:00 2001 From: Dricmoy Bhattacharjee Date: Wed, 11 Nov 2026 16:17:01 -0600 Subject: [PATCH 4/4] GH-51222: [C++] Avoid overflow in binary_slice with extreme step and bounds Co-Authored-By: Claude Opus 5.5 --- .../compute/kernels/scalar_string_ascii.cc | 64 ++++++++++++++----- .../compute/kernels/scalar_string_test.cc | 30 ++++++++- 2 files changed, 76 insertions(+), 18 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc index d42b2e947d96..c7e36e4b2183 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc @@ -2640,12 +2640,36 @@ struct SliceBytesTransform : StringSliceTransformBase { // on the resulting slice lengths, so return a worst case estimate. return input_bytes; } - // 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); - int64_t max_slice_bytes = (stop - start + opt.step - 1) / opt.step; - return std::min(input_bytes, ninputs * std::max(0, max_slice_bytes)); + const int64_t max_slice_bytes = MaxSliceBytes(opt, input_bytes); + // The output is never larger than the input; avoid overflowing the product + if (max_slice_bytes > 0 && ninputs > input_bytes / max_slice_bytes) { + return input_bytes; + } + return std::min(input_bytes, ninputs * max_slice_bytes); + } + + // Upper bound on the output size for one string of at most `max_string_bytes` + // bytes, when start and stop have the same sign. + static int64_t MaxSliceBytes(const SliceOptions& opt, int64_t max_string_bytes) { + // start and stop may be INT64_MIN/INT64_MAX (e.g. the default stop). Indices + // past either end select the same bytes as the end itself, where + // -max_string_bytes - 1 is "before the first byte" when slicing backward. + const int64_t lower = -max_string_bytes - 1; + const int64_t start = std::clamp(opt.start, lower, max_string_bytes); + const int64_t stop = std::clamp(opt.stop, lower, max_string_bytes); + return NumSelectedBytes(opt.step > 0 ? stop - start : start - stop, opt.step); + } + + // ceil(span / |step|), without overflow for any non-zero step + static int64_t NumSelectedBytes(int64_t span, int64_t step) { + if (span <= 0) { + return 0; + } + if (step >= span || step <= -span) { + return 1; + } + // |step| < span, so negating step cannot overflow + return (span - 1) / (step > 0 ? step : -step) + 1; } int64_t Transform(const uint8_t* input, int64_t input_string_bytes, uint8_t* output) { @@ -2728,11 +2752,14 @@ struct SliceBytesTransform : StringSliceTransformBase { uint8_t* dest = output; const uint8_t* i = begin_sliced; - while (i < end_sliced) { - *dest = *i; + while (true) { + *dest++ = *i; + // Don't step past end_sliced, a large step would overflow the pointer + if (opt.step >= end_sliced - i) { + break; + } // skip step codeunits i += opt.step; - dest++; } return dest - output; } @@ -2747,7 +2774,8 @@ struct SliceBytesTransform : StringSliceTransformBase { if (opt.start >= 0) { // +1 because begin_sliced acts as as the end of a reverse iterator - begin_sliced = std::min(begin + opt.start + 1, end); + // (clamp first so that INT64_MAX doesn't overflow) + begin_sliced = std::min(opt.start, end - 1) + 1; } else { // -1 because start=-1 means the last byte, which is 0 advances begin_sliced = std::max(end + opt.start + 1, begin); @@ -2756,7 +2784,8 @@ struct SliceBytesTransform : StringSliceTransformBase { // similar to opt.start if (opt.stop >= 0) { - end_sliced = std::min(begin + opt.stop + 1, end); + // clamp first so that INT64_MAX doesn't overflow + end_sliced = std::min(opt.stop, end - 1) + 1; } else { end_sliced = std::max(end + opt.stop + 1, begin); } @@ -2787,12 +2816,15 @@ struct SliceBytesTransform : StringSliceTransformBase { // Copy computed slice to output uint8_t* dest = output; const uint8_t* i = begin_sliced; - while (i > end_sliced) { + while (true) { // write a single codepoint - *dest = *i; + *dest++ = *i; + // Don't step past end_sliced, a large step would overflow the pointer + if (opt.step <= end_sliced - i) { + break; + } // and skip the remainder i += opt.step; - dest++; } return dest - output; @@ -2806,11 +2838,11 @@ struct SliceBytesTransform : StringSliceTransformBase { if (step > 0) { // forward slice auto [begin_index, end_index] = SliceForwardRange(options, input_width_32); - return static_cast((end_index - begin_index + step - 1) / step); + return static_cast(NumSelectedBytes(end_index - begin_index, step)); } else { // backward slice auto [begin_index, end_index] = SliceBackwardRange(options, input_width_32); - return static_cast((end_index - begin_index + step + 1) / step); + return static_cast(NumSelectedBytes(begin_index - end_index, step)); } } }; diff --git a/cpp/src/arrow/compute/kernels/scalar_string_test.cc b/cpp/src/arrow/compute/kernels/scalar_string_test.cc index 340a5efba177..1833fddd20be 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_test.cc @@ -858,6 +858,15 @@ TEST_F(TestFixedSizeBinaryKernels, BinarySlicePosPos) { SliceOptions options_step_neg{5, 0, -2}; CheckUnary("binary_slice", R"(["abcdef", "foobaz"])", fixed_size_binary(3), R"(["fdb", "zbo"])", &options_step_neg); + + constexpr auto min = std::numeric_limits::min(); + constexpr auto max = std::numeric_limits::max(); + SliceOptions options_max_step{1, max, max}; + CheckUnary("binary_slice", R"(["abcdef", "foobaz"])", fixed_size_binary(1), + R"(["b", "o"])", &options_max_step); + SliceOptions options_min_step{max, 0, min}; + CheckUnary("binary_slice", R"(["abcdef", "foobaz"])", fixed_size_binary(1), + R"(["f", "z"])", &options_min_step); } TEST_F(TestFixedSizeBinaryKernels, BinarySlicePosNeg) { @@ -2714,6 +2723,15 @@ TYPED_TEST(TestBinaryKernels, SliceBytesPosPos) { this->template MakeArray({"abcdef", long_input}), this->template MakeArray({"b", std::string(60, 'x')}), &options_default_stop_step); + SliceOptions options_max_step{/*start=*/1, /*stop=*/max, /*step=*/max}; + this->CheckUnary("binary_slice", this->MakeArray({"", "a", "abc"}), + this->MakeArray({"", "", "b"}), &options_max_step); + SliceOptions options_max_start_neg{/*start=*/max, /*stop=*/0, /*step=*/-1}; + this->CheckUnary("binary_slice", this->MakeArray({"", "a", "abcdef"}), + this->MakeArray({"", "", "fedcb"}), &options_max_start_neg); + options_max_start_neg.step = std::numeric_limits::min(); + this->CheckUnary("binary_slice", this->MakeArray({"", "a", "abcdef"}), + this->MakeArray({"", "", "f"}), &options_max_start_neg); SliceOptions options{2, 4}; this->CheckUnary( @@ -2765,8 +2783,7 @@ TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) { const std::string long_input(300, 'x'); SliceOptions options_extreme_start{/*start=*/min, /*stop=*/-1}; this->CheckUnary("binary_slice", this->template MakeArray({"x"}), - this->template MakeArray({""}), - &options_extreme_start); + this->template MakeArray({""}), &options_extreme_start); SliceOptions options_min{/*start=*/-1, /*stop=*/min}; this->CheckUnary("binary_slice", @@ -2777,6 +2794,9 @@ TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) { this->template MakeArray({"abcdef", long_input}), this->template MakeArray({"fa", std::string(60, 'x')}), &options_min_step_neg); + SliceOptions options_reverse{/*start=*/-1, /*stop=*/min, /*step=*/-1}; + this->CheckUnary("binary_slice", this->MakeArray({"abcdef"}), + this->MakeArray({"fedcba"}), &options_reverse); SliceOptions options{-2, -1}; this->CheckUnary( @@ -2801,6 +2821,12 @@ TYPED_TEST(TestBinaryKernels, SliceBytesNegNeg) { } TYPED_TEST(TestBinaryKernels, SliceBytesNegPos) { + SliceOptions options_max_stop_neg{/*start=*/-1, + /*stop=*/std::numeric_limits::max(), + /*step=*/-1}; + this->CheckUnary("binary_slice", this->MakeArray({"", "a", "abcdef"}), + this->MakeArray({"", "", ""}), &options_max_stop_neg); + SliceOptions options{-2, 4}; this->CheckUnary( "binary_slice",