Skip to content

Fix zero minimum propagation when reducing nested regex loops - #135362

Draft
MihaZupan wants to merge 1 commit into
dotnet:mainfrom
MihaZupan:regex-empty-alternation
Draft

MihaZupan wants to merge 1 commit into
dotnet:mainfrom
MihaZupan:regex-empty-alternation

Conversation

@MihaZupan

@MihaZupan MihaZupan commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

.NET 9: https://mihubot.xyz/regex?pattern=%28%3F%3Aa%2B%7C%29%7B2%7D&version=9.0
.NET 10: https://mihubot.xyz/regex?pattern=%28%3F%3Aa%2B%7C%29%7B2%7D&version=10.0

Repeated empty alternatives such as (?:a+|){2} incorrectly reject "a". When collapsing nested loops, ReduceLoops skips updating its accumulated minimum for a zero-minimum child. A subsequent merge then incorrectly reduces the pattern to a{2,} instead of a*.

Reset the accumulated minimum to zero when crossing an optional loop. Maximum counts, overflow handling, and the guards determining whether loops can safely be combined remain unchanged. The correction applies to interpreted, compiled, source-generated, and nonbacktracking regexes through their shared optimizer.

The empty-alternation optimization in #118087 exposed this preexisting accumulator defect.

Resolves #135329

Note

This change and description were prepared with GitHub Copilot.

Reset the accumulated minimum when collapsing an optional loop so repeated empty alternatives retain their zero minimum. Add reduction and multi-engine functional coverage.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d76b2ba7-1921-4e29-b0b3-77ad73e9c582
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

This branch has not been deployed

No deployments
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.

Regex empty string alternate does not match correctly

1 participant