Skip to content

Commit b4d710d

Browse files
authored
QL: QL: Respond to PR reviews.
1 parent f86a827 commit b4d710d

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

ql/src/codeql_ql/ast/internal/Builtins.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
private import codeql_ql.ast.internal.Type
2+
13
predicate isBuiltinClassless(string sig) {
24
sig =
35
[
@@ -58,3 +60,8 @@ predicate isBuiltinMember(string qual, string ret, string name, string args) {
5860

5961
bindingset[args]
6062
string getArgType(string args, int i) { result = args.splitAt(",", i).trim() }
63+
64+
/** The primitive 'string' class in QL. */
65+
class StringClass extends PrimitiveType {
66+
StringClass() { this.getName() = "string" }
67+
}

ql/src/queries/performance/PrefixSuffixEquality.ql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
import ql
1212
import codeql_ql.ast.internal.Predicate
13-
14-
class StringClass extends PrimitiveType {
15-
StringClass() { this.getName() = "string" }
16-
}
13+
import codeql_ql.ast.internal.Builtins
1714

1815
class PrefixPredicate extends BuiltinPredicate {
1916
PrefixPredicate() { this = any(StringClass sc).getClassPredicate("prefix", 1) }
@@ -35,15 +32,18 @@ class EqFormula extends ComparisonFormula {
3532
EqFormula() { this.getSymbol() = "=" }
3633
}
3734

35+
bindingset[s]
36+
string escape(string s) { result = s.replaceAll("_", "\\\\_").replaceAll("%", "\\\\%") }
37+
3838
pragma[inline]
3939
string getMessage(Call call, String literal) {
40-
call instanceof PrefixPredicateCall and result = ".matches(\"" + literal.getValue() + "%\")"
40+
call instanceof PrefixPredicateCall and
41+
result = ".matches(\"" + escape(literal.getValue()) + "%\")"
4142
or
42-
call instanceof SuffixPredicateCall and result = ".matches(\"%" + literal.getValue() + "\")"
43+
call instanceof SuffixPredicateCall and
44+
result = ".matches(\"%" + escape(literal.getValue()) + "\")"
4345
}
4446

4547
from EqFormula eq, PrefixPredicateCall call, String literal
4648
where eq.getAnOperand() = call and eq.getAnOperand() = literal
47-
select eq,
48-
"Use " + getMessage(call, literal) + " instead (but be sure to escape " + literal.getValue() +
49-
")."
49+
select eq, "Use " + getMessage(call, literal) + " instead."

0 commit comments

Comments
 (0)