Skip to content

Incorrect behaviour of regex atomic groups #74722

Closed

Description

@Mankarse

Description

In some scenarios, the regex engine can backtrack in an atomic group. This should not be possible

Reproduction Steps

var m = System.Text.RegularExpressions.Regex.Match("aaa", "^(?>a*)a$")

Expected behavior

m.Success == false

The above regex should not match. The atomic group should greedily match "aaa", and then be unable to backtrack to match "aa"; and so the overall match should fail.

Actual behavior

m.Success == true

The regex matches.

Regression?

Behaviour is correct on:

.NET Core 3.1.28
.NET Framework 4.8.4515.0

Behaviour is incorrect on:

.NET 5.0.17
.NET 6.0.8
.NET 7.0.0-preview.7.22375.6

(Version names from System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription)

Known Workarounds

No response

Configuration

Bug exists on
.NET 5.0.17
.NET 6.0.8
.NET 7.0.0-preview.7.22375.6

Windows 10 21H2
x64

Other information

No response

Activity

  1. ghost added
    untriaged
    on Aug 28, 2022
  2. self-assigned this
    on Aug 28, 2022
  3. added and removed
    untriaged
    on Aug 28, 2022
  4. stephentoub commented on Aug 28, 2022

    @stephentoub
    Member

    It's not that it's backtracking into an atomic group; it's that the (?>a*)a is erroneously being reduced to (?>a*). We need to not apply loop concatenations to atomic groups.

  5. ghost added
    in-pr
    on Aug 28, 2022
  6. Mankarse commented on Aug 29, 2022

    @Mankarse
    Author

    It's awesome to see this get fixed so fast. Thankyou.

  7. ghost removed
    in-pr
    on Aug 30, 2022
  8. ghost locked as resolved and limited conversation to collaborators on Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions