Skip to content

Commit de001ef

Browse files
committed
Prevented incorrect parse for to be escaped characters as literals
1 parent 975ac5b commit de001ef

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Rubberduck.RegexAssistant/Atom.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public override bool Equals(object obj)
142142

143143
class Literal : IAtom
144144
{
145-
public static readonly string Pattern = @"(?<expression>\\(u[\dA-F]{4}|x[\dA-F]{2}|[0-7]{3}|[bB\(\){}\\\[\]\.+*?1-9nftvrdDwWsS])|[^()\[\]{}\\*+?])";
145+
public static readonly string Pattern = @"(?<expression>\\(u[\dA-F]{4}|x[\dA-F]{2}|[0-7]{3}|[bB\(\){}\\\[\]\.+*?1-9nftvrdDwWsS])|[^()\[\]{}\\*+?^$])";
146146
private static readonly Regex Matcher = new Regex("^" + Pattern + "$");
147147
private static readonly ISet<char> EscapeLiterals = new HashSet<char>();
148148
private readonly string _specifier;

Rubberduck.RegexAssistant/Tests/LiteralTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void SimpleLiterals()
5656
public void EverythingElseBlowsUp()
5757
{
5858
char[] allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"§%&/=ß#'°".ToCharArray();
59-
string[] allowedEscapes = { "(", ")", "{", "}", "[", "]", ".", "?", "+", "*", "uFFFF", "u0000", "xFF", "x00", "777", "000" };
59+
string[] allowedEscapes = { "(", ")", "{", "}", "[", "]", ".", "?", "+", "*", "$", "^", "uFFFF", "u0000", "xFF", "x00", "777", "000" };
6060
foreach (string blowup in allowedEscapes.Select(e => "\\"+ e).Concat(allowed.Select(c => ""+c)))
6161
{
6262
try
@@ -71,5 +71,25 @@ public void EverythingElseBlowsUp()
7171
Assert.Fail("Did not blow up when trying to parse {0} as literal", blowup);
7272
}
7373
}
74+
75+
[TestMethod]
76+
public void SingleEscapedCharsAreNotParsedAsLiteral()
77+
{
78+
string[] escapedChars = "(){}[]\\*?+$^".ToCharArray().Select(e => e.ToString()).ToArray();
79+
foreach (var escape in escapedChars)
80+
{
81+
try
82+
{
83+
Literal cut = new Literal(escape);
84+
}
85+
catch (ArgumentException ex)
86+
{
87+
Assert.IsTrue(true); // Assert.Pass();
88+
continue;
89+
}
90+
Assert.Fail("Did not blow up when trying to parse {0} as literal", escape);
91+
}
92+
93+
}
7494
}
7595
}

0 commit comments

Comments
 (0)