Skip to content

Commit 3c7defa

Browse files
committed
Reduce number of negations in some cases.
1 parent 92cfb6e commit 3c7defa

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-691/InsufficientControlFlowManagementAfterRefactoringTheCode.ql

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class UsingWhileAfterWhile extends WhileStmt {
4444
}
4545
}
4646

47+
48+
4749
/**
4850
* Using arithmetic in a condition.
4951
*/
@@ -55,15 +57,15 @@ class UsingArithmeticInComparison extends BinaryArithmeticOperation {
5557
*/
5658
UsingArithmeticInComparison() {
5759
this.getParent*() instanceof IfStmt and
58-
not this.getAChild*().isConstant() and
59-
not this.getParent*() instanceof Call and
60-
not this.getParent*() instanceof AssignExpr and
61-
not this.getParent*() instanceof ArrayExpr and
62-
not this.getParent*() instanceof RemExpr and
63-
not this.getParent*() instanceof AssignBitwiseOperation and
64-
not this.getParent*() instanceof AssignArithmeticOperation and
65-
not this.getParent*() instanceof EqualityOperation and
66-
not this.getParent*() instanceof RelationalOperation
60+
not (this.getAChild*().isConstant() or
61+
this.getParent*() instanceof Call or
62+
this.getParent*() instanceof AssignExpr or
63+
this.getParent*() instanceof ArrayExpr or
64+
this.getParent*() instanceof RemExpr or
65+
this.getParent*() instanceof AssignBitwiseOperation or
66+
this.getParent*() instanceof AssignArithmeticOperation or
67+
this.getParent*() instanceof EqualityOperation or
68+
this.getParent*() instanceof RelationalOperation)
6769
}
6870

6971
/** Holds when the expression is inside the loop body. */

java/ql/consistency-queries/children.ql

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,64 @@ predicate duplicateChildren(Element e, int i) {
1313
not e instanceof Constructor
1414
}
1515

16-
predicate gapInChildren(Element e, int i) {
17-
exists(int left, int right |
18-
left = min(int l | exists(nthChildOf(e, l))) and
19-
right = max(int r | exists(nthChildOf(e, r))) and
20-
i in [left .. right] and
21-
not exists(nthChildOf(e, i))
22-
) and
16+
predicate allowableGap(Element e, int i) {
2317
// Annotations are child 0 upwards, 'implements' are -2 downwards,
2418
// and there may or may not be an 'extends' for child -1.
25-
not (e instanceof ClassOrInterface and i = -1) and
19+
e instanceof ClassOrInterface and i = -1
20+
or
2621
// A class instance creation expression has the type as child -3,
2722
// may or may not have a qualifier as child -2, and will never have
2823
// a child -1.
29-
not (e instanceof ClassInstanceExpr and i = [-2, -1]) and
24+
e instanceof ClassInstanceExpr and i = [-2, -1]
25+
or
3026
// Type access have annotations from -2 down, and type
3127
// arguments from 0 up, but may or may not have a qualifier
3228
// at -1.
33-
not (e instanceof TypeAccess and i = -1) and
29+
e instanceof TypeAccess and i = -1
30+
or
3431
// Try statements have their 'finally' clause as child 2,
3532
// and that may or may not exist.
36-
not (e instanceof TryStmt and i = -2) and
33+
e instanceof TryStmt and i = -2
34+
or
3735
// For statements may or may not declare a new variable (child 0), or
3836
// have a condition (child 1).
39-
not (e instanceof ForStmt and i = [0, 1]) and
40-
// TODO: Clarify situation with Kotlin and MethodCall.
41-
// -1 can be skipped (type arguments from -2 down, no qualifier at -1,
42-
// then arguments from 0).
43-
// Can we also skip arguments, e.g. due to defaults for parameters?
44-
not (e instanceof MethodCall and e.getFile().isKotlinSourceFile()) and
45-
// Kotlin-extracted annotations can have missing children where a default
46-
// value should be, because kotlinc doesn't load annotation defaults and we
47-
// want to leave a space for another extractor to fill in the default if it
48-
// is able.
49-
not e instanceof Annotation and
37+
e instanceof ForStmt and i = [0, 1]
38+
or
5039
// Pattern case statements legitimately have a TypeAccess (-2) and a pattern (0) but not a rule (-1)
51-
not (i = -1 and e instanceof PatternCase and not e.(PatternCase).isRule()) and
40+
i = -1 and e instanceof PatternCase and not e.(PatternCase).isRule()
41+
or
5242
// Pattern case statements can have a gap at -3 when they have more than one pattern but no guard.
53-
not (
54-
i = -3 and count(e.(PatternCase).getAPattern()) > 1 and not exists(e.(PatternCase).getGuard())
43+
i = -3 and count(e.(PatternCase).getAPattern()) > 1 and not exists(e.(PatternCase).getGuard())
44+
or
45+
// Instanceof with a record pattern is not expected to have a type access in position 1
46+
i = 1 and e.(InstanceOfExpr).getPattern() instanceof RecordPatternExpr
47+
}
48+
49+
predicate gapInChildren(Element e, int i) {
50+
exists(int left, int right |
51+
left = min(int l | exists(nthChildOf(e, l))) and
52+
right = max(int r | exists(nthChildOf(e, r))) and
53+
i in [left .. right] and
54+
not exists(nthChildOf(e, i))
5555
) and
56-
// Pattern case statements may have some missing type accesses, depending on the nature of the direct child
56+
not allowableGap(e, i) and
5757
not (
58+
// Pattern case statements may have some missing type accesses, depending on the nature of the direct child
5859
(i = -2 or i < -4) and
5960
e instanceof PatternCase
6061
) and
61-
// Instanceof with a record pattern is not expected to have a type access in position 1
62-
not (i = 1 and e.(InstanceOfExpr).getPattern() instanceof RecordPatternExpr) and
6362
// RecordPatternExpr extracts type-accesses only for its LocalVariableDeclExpr children
64-
not (i < 0 and e instanceof RecordPatternExpr)
63+
not (i < 0 and e instanceof RecordPatternExpr) and
64+
// Kotlin-extracted annotations can have missing children where a default
65+
// value should be, because kotlinc doesn't load annotation defaults and we
66+
// want to leave a space for another extractor to fill in the default if it
67+
// is able.
68+
not e instanceof Annotation and
69+
// TODO: Clarify situation with Kotlin and MethodCall.
70+
// -1 can be skipped (type arguments from -2 down, no qualifier at -1,
71+
// then arguments from 0).
72+
// Can we also skip arguments, e.g. due to defaults for parameters?
73+
not (e instanceof MethodCall and e.getFile().isKotlinSourceFile())
6574
}
6675

6776
predicate lateFirstChild(Element e, int i) {

0 commit comments

Comments
 (0)