From f30a560f682af3c052195f848fbbe046bdccdde2 Mon Sep 17 00:00:00 2001 From: Prashanth Govindarajan Date: Mon, 16 Dec 2019 12:45:45 -0800 Subject: [PATCH 1/4] Unit test to repro --- .../tests/RegexParserTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs b/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs index 42ad4d4ac01..a0d4e0d4e4d 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; using System.Reflection; using Xunit; using Xunit.Sdk; @@ -881,5 +882,13 @@ namespace System.Text.RegularExpressions.Tests throw new XunitException($"Expected RegexParseException with error: ({error}) -> Actual: No exception thrown"); } + + [Fact] + public void TestPattern() + { + Debugger.Launch(); + string pattern = @"[b-\-a]"; + var regex = new Regex(pattern); + } } } From 52872ac36eee15ec5227ad52fa780b4013de6f7f Mon Sep 17 00:00:00 2001 From: Prashanth Govindarajan Date: Tue, 17 Dec 2019 13:15:27 -0800 Subject: [PATCH 2/4] Parse [^] when ecmascript is set --- .../System/Text/RegularExpressions/RegexParser.cs | 4 ++++ .../tests/Regex.MultipleMatches.Tests.cs | 13 +++++++++++++ .../tests/RegexParserTests.cs | 8 -------- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexParser.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexParser.cs index 4f3ae32b231..edfe8bcfc44 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexParser.cs +++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexParser.cs @@ -562,6 +562,10 @@ namespace System.Text.RegularExpressions { charClass!.Negate = true; } + if ((_options & RegexOptions.ECMAScript) != 0 && CharAt(_currentPos) == ']') + { + firstChar = false; + } } for (; CharsRight() > 0; firstChar = false) diff --git a/src/libraries/System.Text.RegularExpressions/tests/Regex.MultipleMatches.Tests.cs b/src/libraries/System.Text.RegularExpressions/tests/Regex.MultipleMatches.Tests.cs index da031415df9..321ffe52732 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/Regex.MultipleMatches.Tests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/Regex.MultipleMatches.Tests.cs @@ -140,6 +140,19 @@ namespace System.Text.RegularExpressions.Tests new CaptureData("C789", 10, 4), } }; + + yield return new object[] + { + "[^]", "every", RegexOptions.ECMAScript, + new CaptureData[] + { + new CaptureData("e", 0, 1), + new CaptureData("v", 1, 1), + new CaptureData("e", 2, 1), + new CaptureData("r", 3, 1), + new CaptureData("y", 4, 1), + } + }; } [Theory] diff --git a/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs b/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs index a0d4e0d4e4d..aa108fdc996 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs @@ -882,13 +882,5 @@ namespace System.Text.RegularExpressions.Tests throw new XunitException($"Expected RegexParseException with error: ({error}) -> Actual: No exception thrown"); } - - [Fact] - public void TestPattern() - { - Debugger.Launch(); - string pattern = @"[b-\-a]"; - var regex = new Regex(pattern); - } } } From ef8a0a278b33c3bb79d915b20070441123407c60 Mon Sep 17 00:00:00 2001 From: Prashanth Govindarajan Date: Tue, 17 Dec 2019 13:21:48 -0800 Subject: [PATCH 3/4] sq --- .../System.Text.RegularExpressions/tests/RegexParserTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs b/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs index aa108fdc996..42ad4d4ac01 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Diagnostics; using System.Reflection; using Xunit; using Xunit.Sdk; From 71f612233c170dac29606b5facafe13891d3caf7 Mon Sep 17 00:00:00 2001 From: Prashanth Govindarajan Date: Wed, 18 Dec 2019 15:18:07 -0800 Subject: [PATCH 4/4] Add a test to throw when Ecmascript is not specified --- .../System.Text.RegularExpressions/tests/RegexParserTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs b/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs index 42ad4d4ac01..61003d2a5d8 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/RegexParserTests.cs @@ -713,6 +713,7 @@ namespace System.Text.RegularExpressions.Tests [InlineData(@"\d{2147483648}", RegexOptions.None, RegexParseError.CaptureGroupOutOfRange)] [InlineData("[a-z-[b][", RegexOptions.None, RegexParseError.UnterminatedBracket)] [InlineData("(?()|||||)", RegexOptions.None, RegexParseError.TooManyAlternates)] + [InlineData("[^]", RegexOptions.None, RegexParseError.UnterminatedBracket)] public void Parse(string pattern, RegexOptions options, object errorObj) { RegexParseError? error = (RegexParseError?)errorObj;