File tree Expand file tree Collapse file tree 2 files changed +27
-7
lines changed
SonarLint.Secrets.DotNet.UnitTests/DotNetOnly
SonarLint.Secrets.DotNet/Rules/Matching Expand file tree Collapse file tree 2 files changed +27
-7
lines changed Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ woijwoj w woijw e
43
43
44
44
var actual = testSubject . FindIn ( input ) ;
45
45
46
- actual . Count ( ) . Should ( ) . Be ( 2 ) ;
46
+ actual . Count . Should ( ) . Be ( 2 ) ;
47
47
48
48
actual . First ( ) . StartIndex . Should ( ) . Be ( 9 ) ;
49
49
actual . First ( ) . Length . Should ( ) . Be ( 3 ) ;
@@ -53,5 +53,19 @@ woijwoj w woijw e
53
53
actual . Last ( ) . Length . Should ( ) . Be ( 6 ) ;
54
54
actual . Last ( ) . Text . Should ( ) . Be ( "abcdef" ) ;
55
55
}
56
+
57
+ [ TestMethod ]
58
+ public void Find_RegexTimeout_DoesNotThrow ( )
59
+ {
60
+ const string pattern = @"([a-z]*)*" ;
61
+ var input = string . Join ( "" , Enumerable . Repeat ( "a" , 10_000_000 ) ) ;
62
+ var testSubject = new RegexMatcher ( pattern ) ;
63
+
64
+ // With this input the timeout is reached and an exception is thrown.
65
+ // This test ensures that the exception is not propagated further.
66
+ var actual = testSubject . FindIn ( input ) ;
67
+
68
+ actual . Count . Should ( ) . Be ( 0 ) ;
69
+ }
56
70
}
57
71
}
Original file line number Diff line number Diff line change @@ -35,13 +35,19 @@ public RegexMatcher(string stringPattern)
35
35
36
36
public List < Match > FindIn ( string input )
37
37
{
38
- List < Match > matches = new List < Match > ( ) ;
39
-
40
- foreach ( MatchResult matchResult in pattern . Matches ( input ) )
38
+ var matches = new List < Match > ( ) ;
39
+ try
40
+ {
41
+ foreach ( MatchResult matchResult in pattern . Matches ( input ) )
42
+ {
43
+ // Note: hard-coded assumption that the text to highlight is in the second group (index = 1)
44
+ var group = matchResult . Groups [ 1 ] ;
45
+ matches . Add ( new Match ( group . Value , group . Index , group . Length ) ) ;
46
+ }
47
+ }
48
+ catch ( RegexMatchTimeoutException )
41
49
{
42
- // Note: hard-coded assumption that the text to highlight is in the second group (index = 1)
43
- var group = matchResult . Groups [ 1 ] ;
44
- matches . Add ( new Match ( group . Value , group . Index , group . Length ) ) ;
50
+ return matches ;
45
51
}
46
52
47
53
return matches ;
You can’t perform that action at this time.
0 commit comments