Skip to content

Commit e32a04f

Browse files
committed
QL: add use-string-compare query
1 parent fcb3b82 commit e32a04f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @name Use of regexp to match a set of constant string
3+
* @description Comparing against constant strings instead of a regexp can improve performance
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @id ql/use-string-compare
7+
* @tags maintainability
8+
* @precision high
9+
*/
10+
11+
import ql
12+
import codeql_ql.ast.internal.Type
13+
14+
predicate problem(MemberCall call) {
15+
call.getBase().getType().getASuperType*().(PrimitiveType).getName() = "string" and
16+
(
17+
call.getMemberName() = "regexpMatch" and
18+
call.getNumberOfArguments() = 1 and
19+
call.getArgument(0).(String).getValue().regexpMatch("([a-zA-Z0-9]+\\|)*[a-zA-Z0-9]+")
20+
or
21+
exists(string reg | call.getMemberName() = "matches" |
22+
call.getNumberOfArguments() = 1 and
23+
reg = call.getArgument(0).(String).getValue() and
24+
not reg.regexpMatch(".*(%|_).*")
25+
)
26+
)
27+
}
28+
29+
from AstNode node
30+
where problem(node)
31+
select node, "Use string comparison instead of regexp to compare against a constant set of string."

0 commit comments

Comments
 (0)