[release/10.0] Fix incorrect atomic loop optimization when body contains backtracking#124287
Merged
jeffhandley merged 1 commit intorelease/10.0from Feb 16, 2026
Merged
Conversation
MakeLoopAtomic wraps a loop in an Atomic group, which discards all backtracking state from the body. However, CanBeMadeAtomic only proves that giving back iterations is unnecessary... it does not account for within-body backtracking. When the body itself contains backtracking constructs, the atomic wrapper incorrectly suppresses that internal backtracking, producing wrong match results. The fix adds a MayContainBacktracking check that walks the loop body tree before allowing MakeLoopAtomic. A loop is only made atomic when the body has no backtracking constructs of its own. The backtracking-construct detection logic is extracted into a reusable IsBacktrackingConstruct property on RegexNode, which RegexTreeAnalyzer now also uses, removing the duplicated switch. Tests are added covering lazy/greedy single-char loops, char-class loops, alternations, and general multi-char loops inside outer loop bodies, as well as cases where the optimization should still apply (non-backtracking bodies).
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions |
This was referenced Feb 12, 2026
MihaZupan
approved these changes
Feb 12, 2026
artl93
approved these changes
Feb 12, 2026
Member
artl93
left a comment
There was a problem hiding this comment.
customer reported. regression in 10. approved.
artl93
approved these changes
Feb 12, 2026
Member
artl93
left a comment
There was a problem hiding this comment.
Customer reported. Regression. Approved.
Member
|
/ba-g Unrelated failure in a FileSystemWatcher test on 1 platform |
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.
Backport of #124254 to release/10.0
/cc @stephentoub
Customer Impact
Regexes with various backtracking constructs nested inside loops can produce the wrong results. The only workaround is to change the pattern.
Regression
.NET 10, P6 or P7
Testing
Added a bunch of new patterns to the test suite.
Risk
Low. It's disabling an optimization in additional cases. The main risk would be if it removed the optimization in cases where it's still ok, but such an optimization was only added in .NET 10.