-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathUseStringComp.ql
More file actions
31 lines (28 loc) · 988 Bytes
/
UseStringComp.ql
File metadata and controls
31 lines (28 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* @name Use of regexp to match a set of constant string
* @description Comparing against constant strings instead of a regexp can improve performance
* @kind problem
* @problem.severity recommendation
* @id ql/use-string-compare
* @tags maintainability
* @precision high
*/
import ql
import codeql_ql.ast.internal.Type
predicate problem(MemberCall call) {
call.getBase().getType().getASuperType*().(PrimitiveType).getName() = "string" and
(
call.getMemberName() = "regexpMatch" and
call.getNumberOfArguments() = 1 and
call.getArgument(0).(String).getValue().regexpMatch("([a-zA-Z0-9]+\\|)*[a-zA-Z0-9]+")
or
exists(string reg | call.getMemberName() = "matches" |
call.getNumberOfArguments() = 1 and
reg = call.getArgument(0).(String).getValue() and
not reg.regexpMatch(".*(%|_).*")
)
)
}
from AstNode node
where problem(node)
select node, "Use string comparison instead of regexp to compare against a constant set of string."