Skip to content

Commit 393a8c2

Browse files
authored
Merge pull request github#11630 from erik-krogh/useInstanceOf
QL4QL: enable medium precision queries, and make the "suggest instanceof" query louder
2 parents 98c30b8 + 42880f5 commit 393a8c2

File tree

10 files changed

+64
-37
lines changed

10 files changed

+64
-37
lines changed

ql/ql/src/codeql-suites/ql-code-scanning.qls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- alert
88
- path-alert
99
precision:
10+
- medium
1011
- high
1112
- very-high
1213
problem.severity:

ql/ql/src/codeql_ql/style/UseInstanceofExtensionQuery.qll

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,9 @@ predicate instanceofThisInCharPred(Class c, Type type) {
1414
|
1515
instanceOf.getExpr() instanceof ThisAccess and
1616
type = instanceOf.getType().getResolvedType()
17-
)
18-
}
19-
20-
/**
21-
* Holds if `c` uses the casting based range pattern, which could be replaced with `instanceof type`.
22-
*/
23-
predicate usesCastingBasedInstanceof(Class c, Type type) {
24-
instanceofThisInCharPred(c, type) and
25-
// require that there is a call to the range class that matches the name of the enclosing predicate
26-
exists(InlineCast cast, MemberCall call |
27-
cast = getAThisCast(c, type) and
28-
call.getBase() = cast and
29-
cast.getEnclosingPredicate().getName() = call.getMemberName()
30-
)
17+
) and
18+
// no existing super-type corresponds to the instanceof type, that is benign.
19+
not c.getType().getASuperType+() = type
3120
}
3221

3322
/** Gets an inline cast that cases `this` to `type` inside a class predicate for `c`. */
@@ -40,7 +29,7 @@ InlineCast getAThisCast(Class c, Type type) {
4029
)
4130
}
4231

43-
predicate usesFieldBasedInstanceof(Class c, TypeExpr type, FieldDecl field, ComparisonFormula comp) {
32+
predicate usesFieldBasedInstanceof(Class c, Type type, FieldDecl field, ComparisonFormula comp) {
4433
exists(FieldAccess fieldAccess |
4534
c.getCharPred().getBody() = comp or
4635
c.getCharPred().getBody().(Conjunction).getAnOperand() = comp
@@ -50,14 +39,9 @@ predicate usesFieldBasedInstanceof(Class c, TypeExpr type, FieldDecl field, Comp
5039
comp.getAnOperand() instanceof ThisAccess and
5140
comp.getAnOperand() = fieldAccess and
5241
fieldAccess.getDeclaration() = field and
53-
field.getVarDecl().getTypeExpr() = type
42+
field.getVarDecl().getType() = type
5443
) and
55-
// require that there is a call to the range field that matches the name of the enclosing predicate
56-
exists(FieldAccess access, MemberCall call |
57-
access = getARangeFieldAccess(c, field, _) and
58-
call.getBase() = access and
59-
access.getEnclosingPredicate().getName() = call.getMemberName()
60-
)
44+
not c.getType().getASuperType+() = type
6145
}
6246

6347
FieldAccess getARangeFieldAccess(Class c, FieldDecl field, string name) {

ql/ql/src/queries/bugs/PathProblemQuery.ql

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ import codeql_ql.bugs.PathProblemQueryQuery
1414

1515
from Query query, string msg, AstNode pred
1616
where
17-
query.isPathProblem() and
18-
not query.hasEdgesRelation(_) and
19-
pred = any(TopLevel top | top.getLocation().getFile() = query) and // <- dummy value
20-
msg = "A path-problem query should have a edges relation."
21-
or
22-
query.isProblem() and
23-
query.hasEdgesRelation(pred) and
24-
msg = "A problem query should not have a $@."
17+
(
18+
query.isPathProblem() and
19+
not query.hasEdgesRelation(_) and
20+
pred = any(TopLevel top | top.getLocation().getFile() = query) and // <- dummy value
21+
msg = "A path-problem query should have a edges relation."
22+
or
23+
query.isProblem() and
24+
query.hasEdgesRelation(pred) and
25+
msg = "A problem query should not have a $@."
26+
) and
27+
not query.getAbsolutePath().matches("%/test/%")
2528
select query, msg, pred, "edges relation"

ql/ql/src/queries/performance/ClassPredicateDoesntMentionThis.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @problem.severity warning
66
* @id ql/class-predicate-doesnt-use-this
77
* @tags performance
8-
* @precision medium
8+
* @precision low
99
*/
1010

1111
import ql

ql/ql/src/queries/style/OverridingParameterName.ql

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @id ql/override-parameter-name
77
* @tags correctness
88
* maintainability
9-
* @precision medium
9+
* @precision low
1010
*/
1111

1212
import ql
@@ -19,13 +19,18 @@ private predicate getAnOverridingParameter(
1919
parameter = pred.getParameter(index)
2020
}
2121

22+
pragma[nomagic]
23+
string getParameterName(ClassPredicate pred, int index) {
24+
pred.getParameter(index).getName() = result
25+
}
26+
2227
from ClassPredicate pred, ClassPredicate sup, VarDecl parameter, int index
2328
where
2429
getAnOverridingParameter(pred, sup, parameter, index) and
25-
sup.getParameter(index).getName() != pred.getParameter(index).getName() and
30+
getParameterName(sup, index) != getParameterName(pred, index) and
2631
// avoid duplicated alerts with `ql/override-swapped-name`
2732
not exists(int other | other != index |
28-
sup.getParameter(other).getName() = pred.getParameter(index).getName()
33+
getParameterName(sup, other) = getParameterName(pred, index)
2934
)
3035
select parameter, pred.getParameter(index).getName() + " was $@ in the super class.",
3136
sup.getParameter(index), "named " + sup.getParameter(index).getName()

ql/ql/src/queries/style/ToStringInQueryLogic.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @name Using 'toString' in query logic
33
* @description A query should not depend on the output of 'toString'.
44
* @kind problem
5-
* @problem.severity error
5+
* @problem.severity warning
66
* @id ql/to-string-in-logic
77
* @precision medium
88
* @tags maintainability

ql/ql/src/queries/style/UseInstanceofExtension.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import codeql_ql.style.UseInstanceofExtensionQuery
1414
from Class c, Type type, string message
1515
where
1616
(
17-
usesCastingBasedInstanceof(c, type) or
18-
usesFieldBasedInstanceof(c, any(TypeExpr te | te.getResolvedType() = type), _, _)
17+
instanceofThisInCharPred(c, type) or
18+
usesFieldBasedInstanceof(c, type, _, _)
1919
) and
2020
message = "Consider defining this class as non-extending subtype of $@."
2121
select c, message, type.getDeclaration(), type.getName()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Range extends string {
2+
Range() { this = "ql" }
3+
4+
string getAChild() { result = "test" }
5+
}
6+
7+
class Inst extends string {
8+
Range range;
9+
10+
Inst() { this = range }
11+
12+
string getAChild() { result = range.getAChild() }
13+
}
14+
15+
class Inst2 extends string {
16+
Inst2() { this instanceof Range }
17+
18+
string getAChild() { result = this.(Range).getAChild() }
19+
}
20+
21+
class Inst3 extends string {
22+
Range range;
23+
24+
Inst3() { this = range }
25+
}
26+
27+
class Inst4 extends string {
28+
Inst4() { this instanceof Range }
29+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| Foo.qll:7:7:7:10 | Class Inst | Consider defining this class as non-extending subtype of $@. | Foo.qll:1:7:1:11 | Class Range | Range |
2+
| Foo.qll:15:7:15:11 | Class Inst2 | Consider defining this class as non-extending subtype of $@. | Foo.qll:1:7:1:11 | Class Range | Range |
3+
| Foo.qll:21:7:21:11 | Class Inst3 | Consider defining this class as non-extending subtype of $@. | Foo.qll:1:7:1:11 | Class Range | Range |
4+
| Foo.qll:27:7:27:11 | Class Inst4 | Consider defining this class as non-extending subtype of $@. | Foo.qll:1:7:1:11 | Class Range | Range |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/style/UseInstanceofExtension.ql

0 commit comments

Comments
 (0)