Skip to content

Commit ab296d4

Browse files
authored
Merge pull request #10396 from asgerf/js/regexp-always-matches-fp
JS: Fix FP in js/regexp/always-matches
2 parents 13d4c4a + d3d47a2 commit ab296d4

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

javascript/ql/src/RegExp/RegExpAlwaysMatches.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ predicate isUniversalRegExp(RegExpTerm term) {
5454
or
5555
child.(RegExpCharacterClass).isUniversalClass()
5656
)
57+
or
58+
term.(RegExpSequence).getNumChild() = 0
5759
}
5860

5961
/**
@@ -116,6 +118,6 @@ where
116118
call instanceof RegExpSearchCall and
117119
not term.getAChild*() instanceof RegExpDollar and
118120
message =
119-
"This regular expression always the matches at index 0 when used $@, as it matches the empty substring."
121+
"This regular expression always matches at index 0 when used $@, as it matches the empty substring."
120122
)
121123
select term, message, call, "here"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
5+
- The `js/regexp/always-matches` query will no longer report an empty regular expression as always
6+
matching, as this is often the intended behavior.

javascript/ql/test/query-tests/RegExp/RegExpAlwaysMatches/RegExpAlwaysMatches.expected

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
| tst.js:22:11:22:34 | ^(?:https?:\|ftp:\|file:)? | This regular expression always matches when used in a test $@, as it can match an empty substring. | tst.js:22:10:22:43 | /^(?:ht ... test(x) | here |
44
| tst.js:30:11:30:20 | (foo\|bar)? | This regular expression always matches when used in a test $@, as it can match an empty substring. | tst.js:30:10:30:29 | /(foo\|bar)?/.test(x) | here |
55
| tst.js:34:21:34:26 | (baz)? | This regular expression always matches when used in a test $@, as it can match an empty substring. | tst.js:34:10:34:35 | /^foo\|b ... test(x) | here |
6-
| tst.js:58:20:58:25 | [a-z]* | This regular expression always the matches at index 0 when used $@, as it matches the empty substring. | tst.js:58:10:58:27 | x.search(/[a-z]*/) | here |
7-
| tst.js:70:20:70:26 | ^(foo)? | This regular expression always the matches at index 0 when used $@, as it matches the empty substring. | tst.js:70:10:70:28 | x.search(/^(foo)?/) | here |
8-
| tst.js:86:22:86:21 | | This regular expression always matches when used in a test $@, as it can match an empty substring. | tst.js:86:10:86:31 | new Reg ... test(x) | here |
6+
| tst.js:58:20:58:25 | [a-z]* | This regular expression always matches at index 0 when used $@, as it matches the empty substring. | tst.js:58:10:58:27 | x.search(/[a-z]*/) | here |
7+
| tst.js:70:20:70:26 | ^(foo)? | This regular expression always matches at index 0 when used $@, as it matches the empty substring. | tst.js:70:10:70:28 | x.search(/^(foo)?/) | here |

javascript/ql/test/query-tests/RegExp/RegExpAlwaysMatches/tst.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ function nonWordBoundary(x) {
8383
}
8484

8585
function emptyRegex(x) {
86-
return new RegExp("").test(x); // NOT OK
86+
return new RegExp("").test(x); // OK
8787
}
8888

8989
function parserTest(x) {
9090
/(\w\s*:\s*[^:}]+|#){|@import[^\n]+(?:url|,)/.test(x); // OK
91-
/^((?:a{0,2}|-)|\w\{\d,\d\})+X$/.text(x); // ok
92-
}
91+
/^((?:a{0,2}|-)|\w\{\d,\d\})+X$/.text(x); // ok
92+
}

0 commit comments

Comments
 (0)