Skip to content

Commit

Permalink
Fix overaggressive CanBeMadeAtomic check for Set + Notone (#33409)
Browse files Browse the repository at this point in the history
We're erroneously converting a set loop to be atomic when it's followed by a notone where the notone's character is in the set.  But if we for example have `[ab]*[^a]`, we can't make the loop atomic, because the `[ab]*` can actually give back something (a `b`) that the `[^a]` will match.  The fix is simply to delete the erroneous, overaggressive checks.
  • Loading branch information
stephentoub committed Mar 10, 2020
1 parent fb01e2c commit d12f79a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
Expand Up @@ -1541,10 +1541,6 @@ private static bool CanBeMadeAtomic(RegexNode node, RegexNode subsequent, uint m
case Onelazy when subsequent.M > 0 && !RegexCharClass.CharInClass(subsequent.Ch, node.Str!):
case Oneloop when subsequent.M > 0 && !RegexCharClass.CharInClass(subsequent.Ch, node.Str!):
case Oneloopatomic when subsequent.M > 0 && !RegexCharClass.CharInClass(subsequent.Ch, node.Str!):
case Notone when RegexCharClass.CharInClass(subsequent.Ch, node.Str!):
case Notonelazy when subsequent.M > 0 && RegexCharClass.CharInClass(subsequent.Ch, node.Str!):
case Notoneloop when subsequent.M > 0 && RegexCharClass.CharInClass(subsequent.Ch, node.Str!):
case Notoneloopatomic when subsequent.M > 0 && RegexCharClass.CharInClass(subsequent.Ch, node.Str!):
case Multi when !RegexCharClass.CharInClass(subsequent.Str![0], node.Str!):
case Set when !RegexCharClass.MayOverlap(node.Str!, subsequent.Str!):
case Setlazy when subsequent.M > 0 && !RegexCharClass.MayOverlap(node.Str!, subsequent.Str!):
Expand Down
Expand Up @@ -733,6 +733,8 @@ public static IEnumerable<object[]> Groups_Basic_TestData()
yield return new object[] { null, @"(?:a{2}?){3}?", "aaaaaaaaa", RegexOptions.None, new string[] { "aaaaaa" } };
yield return new object[] { null, @"(?:(?:[ab]c[de]f){3}){2}", "acdfbcdfacefbcefbcefbcdfacdef", RegexOptions.None, new string[] { "acdfbcdfacefbcefbcefbcdf" } };
yield return new object[] { null, @"(?:(?:[ab]c[de]f){3}hello){2}", "aaaaaacdfbcdfacefhellobcefbcefbcdfhellooooo", RegexOptions.None, new string[] { "acdfbcdfacefhellobcefbcefbcdfhello" } };
yield return new object[] { null, @"CN=(.*[^,]+).*", "CN=localhost", RegexOptions.Singleline, new string[] { "CN=localhost", "localhost" } };

// Nested atomic
yield return new object[] { null, @"(?>abc[def]gh(i*))", "123abceghiii456", RegexOptions.None, new string[] { "abceghiii", "iii" } };

Expand Down
Expand Up @@ -375,6 +375,10 @@ public void PatternsReduceIdentically(string pattern1, string pattern2)
[InlineData("a*a*?", "a*")]
[InlineData("a*?a*", "a*")]
[InlineData("a*[^a]*", "a*")]
[InlineData("[ab]*[^a]", "(?>[ab]*)[^a]")]
[InlineData("[ab]*[^a]*", "(?>[ab]*)[^a]*")]
[InlineData("[ab]*[^a]*?", "(?>[ab]*)[^a]*?")]
[InlineData("[ab]*(?>[^a]*)", "(?>[ab]*)(?>[^a]*)")]
[InlineData("[^a]*a*", "a*")]
[InlineData("a{2147483646}a", "a{2147483647}")]
[InlineData("a{2147483647}a", "a{2147483647}")]
Expand Down

0 comments on commit d12f79a

Please sign in to comment.