Skip to content

Commit d88cc79

Browse files
authored
QL: Add the ql/primary-ql-class-consistency query
1 parent 93fa56f commit d88cc79

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

ql/src/codeql_ql/ast/Ast.qll

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,11 @@ class String extends Literal {
630630
override string getAPrimaryQlClass() { result = "String" }
631631

632632
/** Gets the string value of this literal. */
633-
string getValue() { result = lit.getChild().(Generated::String).getValue() }
633+
string getValue() {
634+
exists(string raw | raw = lit.getChild().(Generated::String).getValue() |
635+
result = raw.substring(1, raw.length() - 1)
636+
)
637+
}
634638
}
635639

636640
/** An integer literal. */
@@ -1198,10 +1202,8 @@ class ModuleExpr extends TModuleExpr, ModuleRef {
11981202

11991203
override AstNode getParent() {
12001204
result = super.getParent() or
1201-
result.(PredicateCall).getQualifier() = this
1202-
or
1203-
result.(PredicateExpr).getQualifier() = this
1204-
or
1205+
result.(PredicateCall).getQualifier() = this or
1206+
result.(PredicateExpr).getQualifier() = this or
12051207
result.(Module).getAlias() = this
12061208
}
12071209
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @name Inconsistent getAPrimaryQlClass predicate
3+
* @description A getAPrimaryQlClass predicate should result in the name of the class.
4+
* @kind problem
5+
* @problem.severity error
6+
* @id ql/primary-ql-class-consistency
7+
* @tags correctness
8+
* @precision low
9+
*/
10+
11+
import ql
12+
13+
from ClassPredicate pred, String constant
14+
where
15+
exists(string className, string constantName |
16+
pred.getParent().getName() = className and
17+
pred.getName() = "getAPrimaryQlClass" and
18+
constant = pred.getBody().(ComparisonFormula).getRightOperand() and
19+
constant.(String).getValue() = constantName and
20+
// might be "Foo::classname", detect by matching with a regexp
21+
not constantName.regexpMatch(".*\\b" + className + "$") and
22+
// ignore constants with "?" in them
23+
not constantName.regexpMatch(".*\\?.*")
24+
)
25+
select pred,
26+
"The getAPrimaryQlClass predicate $@ instead of the class name \"" + pred.getParent().getName() +
27+
"\".", constant, "results in \"" + constant.getValue() + " \""

0 commit comments

Comments
 (0)