Skip to content

GH-55842: [C++] Fix hang in split_pattern_regex with patterns matchin… - #55843

Open
arunsahabytes wants to merge 1 commit into
apache:mainfrom
arunsahabytes:split-regex-zero-width
Open

arunsahabytes wants to merge 1 commit into
apache:mainfrom
arunsahabytes:split-regex-zero-width

Conversation

@arunsahabytes

Copy link
Copy Markdown

Rationale for this change

split_pattern_regex loops forever when the pattern matches the empty string (e.g. "", "a*"), see #55842.

What changes are included in this PR?

In the forward split loop, an empty separator directly after another empty separator is skipped and the search restarts one character later (one codepoint for UTF-8, one byte for binary). This matches Python's re.split, e.g. "" on "abc" gives ["", "a", "b", "c", ""]. Other split kernels never produce empty separators, so they are unaffected.

Are these changes tested?

Yes, new C++ tests (SplitRegexEmptyMatch, SplitRegexEmptyMatchUtf8) and a Python test. I also checked the new loop logic against re.split with a Python simulation over a few thousand pattern/string/max_splits cases. I could not build Arrow C++ locally (Windows), so CI is the first full build.

Are there any user-facing changes?

Yes: these calls now return instead of hanging.

This PR contains a "Critical Fix". It fixes a bug that causes an infinite loop and unbounded memory growth.

I used Claude while writing the fix and tests. I reviewed the change, reproduced the hang myself, and checked the results against Python's re.split.

…atching 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).
Copilot AI balanced review requested due to automatic review settings November 30, 2026 01:22

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants