From ce98c20160aeef76d63f44a68a02c655e8db0fe7 Mon Sep 17 00:00:00 2001 From: Arun Saha <96629404+arunsahabytes@users.noreply.github.com> Date: Mon, 30 Nov 2026 06:39:34 +0530 Subject: [PATCH] GH-55842: [C++] Fix hang in split_pattern_regex with patterns matching the empty string A regex like "" or "a*" can match an empty separator at the current position. The split loop then continued from the same position and looped forever. Follow Python's re.split: an empty separator is not allowed right after another empty separator, so search again from the next character (codepoint for UTF-8, byte for binary). --- .../compute/kernels/scalar_string_internal.h | 32 +++++++++++++++++-- .../compute/kernels/scalar_string_test.cc | 30 +++++++++++++++++ python/pyarrow/tests/test_compute.py | 12 +++++++ 3 files changed, 72 insertions(+), 2 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..f685121d0104 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_internal.h +++ b/cpp/src/arrow/compute/kernels/scalar_string_internal.h @@ -321,15 +321,27 @@ struct StringSplitExec { // Keep the temporary storage across individual values, to minimize reallocations std::vector parts; Options options; + bool is_utf8 = false; explicit StringSplitExec(const Options& options) : options(options) {} + // Advance by one codepoint for UTF-8 input, by one byte otherwise. + const uint8_t* NextCharacter(const uint8_t* p, const uint8_t* end) const { + ++p; + if (is_utf8) { + while (p < end && (*p & 0xC0) == 0x80) { + ++p; + } + } + return p; + } + static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { return StringSplitExec{State::Get(ctx)}.Execute(ctx, batch, out); } Status Execute(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) { - const bool is_utf8 = is_string_or_string_view(batch[0].type()->id()); + is_utf8 = is_string_or_string_view(batch[0].type()->id()); SplitFinder finder; RETURN_NOT_OK(finder.PreExec(options, is_utf8)); // TODO(wesm): refactor to not require creating ArrayData @@ -401,14 +413,30 @@ struct StringSplitExec { } } else { const uint8_t* i = begin; + // Where to search for the next separator. This is ahead of `i` only after + // an empty separator was skipped (only possible with regex patterns). + const uint8_t* search_from = begin; + bool last_separator_empty = false; while (max_splits != 0) { const uint8_t *separator_begin, *separator_end; // find with whatever algo the part we will 'cut out' - if (finder->Find(i, end, &separator_begin, &separator_end, options)) { + if (finder->Find(search_from, end, &separator_begin, &separator_end, options)) { + if (separator_begin == separator_end && separator_begin == i && + last_separator_empty) { + // Like Python's re.split, an empty separator is not allowed right + // after another empty separator: search again from the next character. + if (search_from == end) { + break; + } + search_from = NextCharacter(search_from, end); + continue; + } // the part till the beginning of the 'cut' RETURN_NOT_OK( builder->Append(i, static_cast(separator_begin - i))); i = separator_end; + search_from = separator_end; + last_separator_empty = separator_begin == separator_end; max_splits--; } else { // if we cannot find a separator, we're done diff --git a/cpp/src/arrow/compute/kernels/scalar_string_test.cc b/cpp/src/arrow/compute/kernels/scalar_string_test.cc index 74650c1d76c5..348203c13497 100644 --- a/cpp/src/arrow/compute/kernels/scalar_string_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_string_test.cc @@ -2031,6 +2031,36 @@ TYPED_TEST(TestBaseBinaryKernels, SplitRegex) { &options); } +TYPED_TEST(TestBaseBinaryKernels, SplitRegexEmptyMatch) { + // GH-55842: patterns that can match the empty string used to loop forever + SplitPatternOptions options{""}; + this->CheckUnary("split_pattern_regex", R"(["abc", "", null])", list(this->type()), + R"([["", "a", "b", "c", ""], ["", ""], null])", &options); + + options.pattern = "a*"; + this->CheckUnary("split_pattern_regex", R"(["bbb", "axbc", "baab"])", + list(this->type()), + R"([["", "b", "b", "b", ""], ["", "", "x", "b", "c", ""], + ["", "b", "", "b", ""]])", + &options); + + options.pattern = "x*"; + this->CheckUnary("split_pattern_regex", R"(["axbc"])", list(this->type()), + R"([["", "a", "", "b", "c", ""]])", &options); + + options.pattern = ""; + options.max_splits = 2; + this->CheckUnary("split_pattern_regex", R"(["abc"])", list(this->type()), + R"([["", "a", "bc"]])", &options); +} + +TYPED_TEST(TestStringKernels, SplitRegexEmptyMatchUtf8) { + // An empty match must advance by a whole codepoint, not a single byte + SplitPatternOptions options{""}; + this->CheckUnary("split_pattern_regex", R"(["πaθ"])", list(this->type()), + R"([["", "π", "a", "θ", ""]])", &options); +} + TYPED_TEST(TestBaseBinaryKernels, SplitRegexReverse) { SplitPatternOptions options{"a+|b", /*max_splits=*/1, /*reverse=*/true}; Datum input = ArrayFromJSON(this->type(), R"(["a"])"); diff --git a/python/pyarrow/tests/test_compute.py b/python/pyarrow/tests/test_compute.py index 0e0260808d6d..66f2d28098a3 100644 --- a/python/pyarrow/tests/test_compute.py +++ b/python/pyarrow/tests/test_compute.py @@ -768,6 +768,18 @@ def test_split_pattern_regex(): result = pc.split_pattern_regex( arr, pattern="---", max_splits=1, reverse=True) + # GH-55842: patterns matching the empty string used to hang + arr = pa.array(["abc", "axbc", ""]) + result = pc.split_pattern_regex(arr, pattern="") + expected = pa.array([["", "a", "b", "c", ""], + ["", "a", "x", "b", "c", ""], ["", ""]]) + assert expected.equals(result) + + result = pc.split_pattern_regex(arr, pattern="x*") + expected = pa.array([["", "a", "b", "c", ""], + ["", "a", "", "b", "c", ""], ["", ""]]) + assert expected.equals(result) + def test_min_max(): # An example generated function wrapper with possible options