Skip to content

Commit 3e5324e

Browse files
committed
More precise Nullness tracking by taking correlated instanceof expressions into account.
Fixes #2238.
1 parent d8aae1c commit 3e5324e

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ Expr enumConstEquality(Expr e, boolean polarity, EnumConstant c) {
2424
)
2525
}
2626

27+
/** Gets an instanceof expression of `v` with type `type` */
28+
InstanceOfExpr instanceofExpr(SsaVariable v, Expr type) {
29+
result.getTypeName() = type and
30+
result.getExpr() = v.getAUse()
31+
}
32+
2733
/** Gets an expression that is provably not `null`. */
2834
Expr clearlyNotNullExpr(Expr reason) {
2935
result instanceof ClassInstanceExpr and reason = result

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,13 @@ private predicate correlatedConditions(
515515
cond2.getCondition() = enumConstEquality(v.getAUse(), pol2, c) and
516516
inverted = pol1.booleanXor(pol2)
517517
)
518+
or
519+
exists(SsaVariable v, Expr t1, Expr t2 |
520+
cond1.getCondition() = instanceofExpr(v, t1) and
521+
cond2.getCondition() = instanceofExpr(v, t2) and
522+
t1.getType() = t2.getType() and
523+
inverted = false
524+
)
518525
)
519526
}
520527

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +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:334:7:334:7 | x | Variable $@ may be null here because of $@ assignment. | B.java:329:5:329:20 | Object x | x | B.java:329:12:329:19 | x | this |
21-
| B.java:344:7:344:7 | x | Variable $@ may be null here because of $@ assignment. | B.java:339:5:339:20 | Object x | x | B.java:339:12:339:19 | x | this |
2220
| 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 |
2321
| 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 |
2422
| 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)