Skip to content

Commit 5d4b6c3

Browse files
committed
Nullness: Track correlated conditions of equality tests of variables.
1 parent 92f32a1 commit 5d4b6c3

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

java/ql/src/semmle/code/java/dataflow/NullGuards.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ InstanceOfExpr instanceofExpr(SsaVariable v, Expr type) {
3030
result.getExpr() = v.getAUse()
3131
}
3232

33+
/**
34+
* Gets an expression of the form `v1` == `v2` or `v1` != `v2`.
35+
* The predicate is symmetric in `v1` and `v2`.
36+
*/
37+
BinaryExpr varComparisonExpr(SsaVariable v1, SsaVariable v2, boolean isEqualExpr) {
38+
(
39+
result.getLeftOperand() = v1.getAUse() and
40+
result.getRightOperand() = v2.getAUse()
41+
or
42+
result.getLeftOperand() = v2.getAUse() and
43+
result.getRightOperand() = v1.getAUse()
44+
) and
45+
(
46+
result instanceof EQExpr and
47+
isEqualExpr = true
48+
or
49+
result instanceof NEExpr and
50+
isEqualExpr = false
51+
)
52+
}
53+
3354
/** Gets an expression that is provably not `null`. */
3455
Expr clearlyNotNullExpr(Expr reason) {
3556
result instanceof ClassInstanceExpr and reason = result

java/ql/src/semmle/code/java/dataflow/Nullness.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ private predicate correlatedConditions(
522522
t1.getType() = t2.getType() and
523523
inverted = false
524524
)
525+
or
526+
exists(SsaVariable v1, SsaVariable v2, boolean branch1, boolean branch2 |
527+
cond1.getCondition() = varComparisonExpr(v1, v2, branch1) and
528+
cond2.getCondition() = varComparisonExpr(v1, v2, branch2) and
529+
inverted = branch1.booleanXor(branch2)
530+
)
525531
)
526532
}
527533

java/ql/test/query-tests/Nullness/NullMaybe.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
| B.java:190:7:190:7 | o | Variable $@ may be null here because of $@ assignment. | B.java:178:5:178:20 | Object o | o | B.java:186:5:186:12 | ...=... | this |
1818
| B.java:279:7:279:7 | a | Variable $@ may be null here because of $@ assignment. | B.java:276:5:276:19 | int[] a | a | B.java:276:11:276:18 | a | this |
1919
| B.java:292:7:292:7 | b | Variable $@ may be null here because of $@ assignment. | B.java:287:5:287:44 | int[] b | b | B.java:287:11:287:43 | b | this |
20-
| B.java:354:7:354:7 | x | Variable $@ may be null here because of $@ assignment. | B.java:349:5:349:20 | Object x | x | B.java:349:12:349:19 | x | this |
21-
| B.java:362:7:362:8 | x2 | Variable $@ may be null here because of $@ assignment. | B.java:357:5:357:21 | Object x2 | x2 | B.java:357:12:357:20 | x2 | this |
22-
| B.java:370:7:370:8 | x3 | Variable $@ may be null here because of $@ assignment. | B.java:365:5:365:21 | Object x3 | x3 | B.java:365:12:365:20 | x3 | this |
2320
| C.java:9:44:9:45 | a2 | Variable $@ may be null here as suggested by $@ null guard. | C.java:6:5:6:23 | long[][] a2 | a2 | C.java:7:34:7:54 | ... != ... | this |
2421
| C.java:9:44:9:45 | a2 | Variable $@ may be null here because of $@ assignment. | C.java:6:5:6:23 | long[][] a2 | a2 | C.java:6:14:6:22 | a2 | this |
2522
| C.java:10:17:10:18 | a3 | Variable $@ may be null here as suggested by $@ null guard. | C.java:8:5:8:21 | long[] a3 | a3 | C.java:9:38:9:58 | ... != ... | this |

0 commit comments

Comments
 (0)