<regex>: Fix reentrant loops containing backreferences#6055
Merged
StephanTLavavej merged 1 commit intomicrosoft:mainfrom Feb 2, 2026
Merged
Conversation
StephanTLavavej
approved these changes
Jan 29, 2026
Member
|
I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed. |
Member
|
Thanks for breaking and then fixing this! 🏚️ 🛠️ 🏡 |
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.
I introduced a subtle bug in #6022: I missed that there are loops that get marked as branchless but can match strings of different lengths, so the value of
_Loop_lengthin the loop state can change. This applies to loops with backreferences, since the length of the captured string might have changed when the loop is reentered.The problem is that
_Loop_lengthis used during unwinding of branchless/simple loops, but its value is not restored during backtracking.I see two ways to fix this:
_Loop_lengthlike_Loop_frame_idxand_Loop_idxduring backtracking. But there is no unused memory to store this value on the stack, so we would have to add a new frame to the stack or increase the size of each frame.Given that this is about relatively rare regexes that contain subpatterns like
(prefix(\1)*suffix)*, the first option seems like a waste of memory pessimizing more common regexes. For this reason, I went for the latter option._N_rep,_N_if,_N_assertand_N_class(if collating elements are contained) are other node types that can cause a loop to match strings or capturing groups of different lengths. but they are already handled. The remaining node types always match strings of the same length.While debugging this, I was a bit annoyed that the debugger showed the
_Fl_rep_branchlessflag under the name_Fl_class_cl_all_bits, so I moved the flag to another free bit for the_N_repnode type in this PR. (We could also reuse mask0x0004, since the old parser never set this bit on nodes of type_N_rep, but this is probably unnecessarily subtle while we still have plenty of bits available.)