Repository navigation
GH-55842: [C++] Fix hang in split_pattern_regex with patterns matchin… - #55843
Open
arunsahabytes wants to merge 1 commit into
Open
arunsahabytes wants to merge 1 commit into
arunsahabytes wants to merge 1 commit into
Conversation
…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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
split_pattern_regexloops 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 againstre.splitwith 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.