Skip to content

Conversation

@muellerj2
Copy link
Contributor

@muellerj2 muellerj2 commented Oct 22, 2025

Fixes #997. Fixes #1528.

This is the final change to make the matcher non-recursive, but it will be followed by a few more PRs to clean up the code and simplify the data stored in _Rx_state_frames.

The new code in _Match_pat() is closer to the structure of the original code than for simple loops, but also quite similar to the non-recursive code for simple loops, so I chose not to split it into several PRs.

Differences compared to simple loops:

  • Since non-simple loops are potentially re-entrant and repeated patterns can branch, the loop variables have to be reset whenever we backtrack. This means we have to add more members to _Rx_state_frame to store the original values of these loop variables and have to add an unwinding opcode just for resetting these loop variables. Moreover, we have to completely restore the match state whenever we backtrack after failure (ECMAScript) or while matching a POSIX regex.
    • In the case of greedy matching, we can restore the loop variables in the same opcode that also sets up matching of the tail when matching another repetition failed. This has the slight advantage that we need to maintain fewer frames for backtracking while matching the tail.
  • I kept the structure of conditions mostly the same between simple and generic loops, except that I exchanged the order of conditions for "empty match" and "minimum number of repetitions not reached yet". This order aligns more closely with _Do_rep() and is a bit simpler for generic loops, I think, because the logic for handling the minimum number of repetitions isn't affected much by whether the last encountered match is empty or not (except for the optimization of increasing _Loop_idx by more than one when a match is empty). This is different from simple loops, where the easy case was an empty match: Because simple loops are branchless, one empty match means all are empty, so the minimum number of repetitions essentially becomes meaningless when an empty match appears.

Most of the test coverage is derived from the new coverage for simple loops, with a minor change to make the loops non-simple by making them branch. I also added another test to make sure we correctly handle an empty match that only appears in the final repetition to reach the minimum number of repetitions.

As for the two tests to verify that stack overflows no longer occur (copied from the original issues), I deliberately set one of them up to fail when matching no longer results in an exception, because the code should be changed to use the regex tester when an exception is no longer thrown. (I can't easily do this for the other test case because it currently succeeds for x86 but fails for x64. It seems out of proportion to add preprocessor logic just to make the test fail on 64-bit systems when it succeeds unexpectedly.)

@muellerj2 muellerj2 requested a review from a team as a code owner October 22, 2025 21:24
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Oct 22, 2025
@StephanTLavavej StephanTLavavej added bug Something isn't working regex meow is a substring of homeowner labels Oct 23, 2025
@StephanTLavavej StephanTLavavej self-assigned this Oct 23, 2025
I made the domain slightly longer, and kept '.' characters in the same relative locations, since they appear to be the one character
that's relevant to the regex.
@StephanTLavavej
Copy link
Member

Thanks, this is incredible! 😻 I pushed some changes to the test, please double-check that I didn't mess anything up.

If the CLA Bot is snoozy I may need to close and reopen this PR.

@github-project-automation github-project-automation bot moved this from Initial Review to Done in STL Code Reviews Oct 25, 2025
@github-project-automation github-project-automation bot moved this from Done to Initial Review in STL Code Reviews Oct 25, 2025
@StephanTLavavej StephanTLavavej removed their assignment Oct 25, 2025
@StephanTLavavej StephanTLavavej moved this from Initial Review to Ready To Merge in STL Code Reviews Oct 25, 2025
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej moved this from Ready To Merge to Merging in STL Code Reviews Oct 28, 2025
@StephanTLavavej StephanTLavavej merged commit 71ebc1b into microsoft:main Oct 29, 2025
39 checks passed
@github-project-automation github-project-automation bot moved this from Merging to Done in STL Code Reviews Oct 29, 2025
@StephanTLavavej
Copy link
Member

An incredible achievement, thank you! 😻 🎉 😸

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

Labels

bug Something isn't working regex meow is a substring of homeowner

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

<regex>: Grouping within repetition causes regex stack error <regex>: regex_match gets caught in recursive loop until stack overflow occurs

2 participants