From b236128f25413dfc85ec4e20c34f6719c645854a Mon Sep 17 00:00:00 2001 From: Iason Krommydas Date: Tue, 24 Nov 2026 17:25:52 +0100 Subject: [PATCH 1/2] GH-37757: [C++][Compute] Fix out-of-bounds read in utf8_split_whitespace Signed-off-by: Iason Krommydas --- .../compute/kernels/scalar_string_ascii.cc | 4 +- .../compute/kernels/scalar_string_test.cc | 71 +++++++++++++++++++ .../compute/kernels/scalar_string_utf8.cc | 20 ++++-- 3 files changed, 87 insertions(+), 8 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc index dd3c507ef53e..d17ca03a27ac 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc @@ -2940,7 +2940,7 @@ struct SplitWhitespaceAsciiFinder : public StringSplitFinderBase { *separator_begin = i; do { i++; - } while (IsSpaceCharacterAscii(*i) && i < end); + } while (i < end && IsSpaceCharacterAscii(*i)); *separator_end = i; return true; } @@ -2958,7 +2958,7 @@ struct SplitWhitespaceAsciiFinder : public StringSplitFinderBase { *separator_end = i + 1; do { i--; - } while (IsSpaceCharacterAscii(*i) && i >= begin); + } while (i >= begin && IsSpaceCharacterAscii(*i)); *separator_begin = i + 1; return true; } diff --git a/cpp/src/arrow/compute/kernels/scalar_string_test.cc b/cpp/src/arrow/compute/kernels/scalar_string_test.cc index 74650c1d76c5..c6023bdab71b 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_test.cc @@ -1962,6 +1962,25 @@ TYPED_TEST(TestBaseBinaryKernels, SplitMax) { R"([["foo", "bar"], ["foo"], ["foo---bar", "", "ar"]])", &options_reverse); } +template +std::shared_ptr MakeArrayInExactBuffer(const std::vector& values, + const std::string& prefix = "", + const std::string& suffix = "") { + using offset_type = typename TypeTraits::OffsetType::c_type; + std::string data = prefix; + std::vector offsets = {static_cast(data.size())}; + for (const auto& value : values) { + data += value; + offsets.push_back(static_cast(data.size())); + } + data += suffix; + return MakeArray(ArrayData::Make( + TypeTraits::type_singleton(), static_cast(values.size()), + {nullptr, Buffer::FromVector(std::move(offsets)), + Buffer::FromVector(std::vector(data.begin(), data.end()))}, + /*null_count=*/0)); +} + TYPED_TEST(TestStringKernels, SplitWhitespaceAscii) { SplitOptions options; SplitOptions options_max{1}; @@ -1986,6 +2005,16 @@ TYPED_TEST(TestStringKernels, SplitWhitespaceAsciiReverse) { &options_max); } +TYPED_TEST(TestStringKernels, SplitWhitespaceAsciiStaysInBounds) { + SplitOptions options; + SplitOptions options_reverse{-1, /*reverse=*/true}; + auto input = MakeArrayInExactBuffer({" foo ", " bar "}); + this->CheckUnary("ascii_split_whitespace", input, list(this->type()), + R"([["", "foo", ""], ["", "bar", ""]])", &options); + this->CheckUnary("ascii_split_whitespace", input, list(this->type()), + R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse); +} + #ifdef ARROW_WITH_UTF8PROC TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8) { SplitOptions options; @@ -2011,6 +2040,48 @@ TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8Reverse) { "[[\"foo\", \"bar\"], [\"foo\xe2\x80\x88 bar\", \"ba\"]]", &options_max); } + +TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8LeadingTrailing) { + SplitOptions options; + SplitOptions options_max{1}; + SplitOptions options_reverse{-1, /*reverse=*/true}; + SplitOptions options_reverse_max{1, /*reverse=*/true}; + // \xe2\x80\x88 is punctuation space + const char* input = + "[\"foo \", \" foo\", \" foo \", \" \", \" \", \"\", " + "\"foo\xe2\x80\x88\xe2\x80\x88\", \"\xe2\x80\x88 foo\"]"; + this->CheckUnary("utf8_split_whitespace", input, list(this->type()), + R"([["foo", ""], ["", "foo"], ["", "foo", ""], ["", ""], ["", ""], + [""], ["foo", ""], ["", "foo"]])", + &options); + this->CheckUnary("utf8_split_whitespace", input, list(this->type()), + R"([["foo", ""], ["", "foo"], ["", "foo "], ["", ""], ["", ""], + [""], ["foo", ""], ["", "foo"]])", + &options_max); + this->CheckUnary("utf8_split_whitespace", input, list(this->type()), + R"([["foo", ""], ["", "foo"], ["", "foo", ""], ["", ""], ["", ""], + [""], ["foo", ""], ["", "foo"]])", + &options_reverse); + this->CheckUnary("utf8_split_whitespace", input, list(this->type()), + R"([["foo", ""], ["", "foo"], [" foo", ""], ["", ""], ["", ""], + [""], ["foo", ""], ["", "foo"]])", + &options_reverse_max); +} + +TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8StaysInBounds) { + SplitOptions options; + SplitOptions options_reverse{-1, /*reverse=*/true}; + auto input = MakeArrayInExactBuffer({" foo ", " bar "}, "\xff", "\xff"); + this->CheckUnary("utf8_split_whitespace", input, list(this->type()), + R"([["", "foo", ""], ["", "bar", ""]])", &options); + this->CheckUnary("utf8_split_whitespace", input, list(this->type()), + R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse); + input = MakeArrayInExactBuffer({" foo ", " bar "}); + this->CheckUnary("utf8_split_whitespace", input, list(this->type()), + R"([["", "foo", ""], ["", "bar", ""]])", &options); + this->CheckUnary("utf8_split_whitespace", input, list(this->type()), + R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse); +} #endif #ifdef ARROW_WITH_RE2 diff --git a/cpp/src/arrow/compute/kernels/scalar_string_utf8.cc b/cpp/src/arrow/compute/kernels/scalar_string_utf8.cc index 6d4525c28696..d292a50fd4ab 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_utf8.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_utf8.cc @@ -1381,12 +1381,16 @@ struct SplitWhitespaceUtf8Finder : public StringSplitFinderBase { return false; } if (IsSpaceCharacterUnicode(codepoint)) { - do { - *separator_end = i; + *separator_end = i; + while (i < end) { if (ARROW_PREDICT_FALSE(!arrow::util::UTF8Decode(&i, &codepoint))) { return false; } - } while (IsSpaceCharacterUnicode(codepoint) && i < end); + if (!IsSpaceCharacterUnicode(codepoint)) { + break; + } + *separator_end = i; + } return true; } } @@ -1404,12 +1408,16 @@ struct SplitWhitespaceUtf8Finder : public StringSplitFinderBase { return false; } if (IsSpaceCharacterUnicode(codepoint)) { - do { - *separator_begin = i + 1; + *separator_begin = i + 1; + while (i >= begin) { if (ARROW_PREDICT_FALSE(!arrow::util::UTF8DecodeReverse(&i, &codepoint))) { return false; } - } while (IsSpaceCharacterUnicode(codepoint) && i >= begin); + if (!IsSpaceCharacterUnicode(codepoint)) { + break; + } + *separator_begin = i + 1; + } return true; } } From 523c04df62a063f575e4b0b826ebb1e86bc7172f Mon Sep 17 00:00:00 2001 From: Iason Krommydas Date: Tue, 1 Dec 2026 19:15:03 +0100 Subject: [PATCH 2/2] Address review comments Signed-off-by: Iason Krommydas --- .../arrow/compute/kernels/scalar_string_internal.h | 2 +- cpp/src/arrow/compute/kernels/scalar_string_test.cc | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/scalar_string_internal.h b/cpp/src/arrow/compute/kernels/scalar_string_internal.h index 5c621e8d53f5..7ef17838ec73 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_internal.h +++ b/cpp/src/arrow/compute/kernels/scalar_string_internal.h @@ -381,7 +381,7 @@ struct StringSplitExec { if (max_splits > -1) { parts.reserve(max_splits + 1); } - while (max_splits != 0) { + while (max_splits != 0 && i > begin) { const uint8_t *separator_begin, *separator_end; // find with whatever algo the part we will 'cut out' if (finder->FindReverse(begin, i, &separator_begin, &separator_end, options)) { diff --git a/cpp/src/arrow/compute/kernels/scalar_string_test.cc b/cpp/src/arrow/compute/kernels/scalar_string_test.cc index c6023bdab71b..82e01132608b 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_test.cc @@ -2008,11 +2008,12 @@ TYPED_TEST(TestStringKernels, SplitWhitespaceAsciiReverse) { TYPED_TEST(TestStringKernels, SplitWhitespaceAsciiStaysInBounds) { SplitOptions options; SplitOptions options_reverse{-1, /*reverse=*/true}; - auto input = MakeArrayInExactBuffer({" foo ", " bar "}); + auto input = MakeArrayInExactBuffer({" foo ", " ", "", " bar "}); this->CheckUnary("ascii_split_whitespace", input, list(this->type()), - R"([["", "foo", ""], ["", "bar", ""]])", &options); + R"([["", "foo", ""], ["", ""], [""], ["", "bar", ""]])", &options); this->CheckUnary("ascii_split_whitespace", input, list(this->type()), - R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse); + R"([["", "foo", ""], ["", ""], [""], ["", "bar", ""]])", + &options_reverse); } #ifdef ARROW_WITH_UTF8PROC @@ -2076,7 +2077,9 @@ TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8StaysInBounds) { R"([["", "foo", ""], ["", "bar", ""]])", &options); this->CheckUnary("utf8_split_whitespace", input, list(this->type()), R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse); - input = MakeArrayInExactBuffer({" foo ", " bar "}); + input = MakeArrayInExactBuffer({" foo ", + "\xe2\x80\x88" + "bar\xe2\x80\x88"}); this->CheckUnary("utf8_split_whitespace", input, list(this->type()), R"([["", "foo", ""], ["", "bar", ""]])", &options); this->CheckUnary("utf8_split_whitespace", input, list(this->type()),