Skip to content

Commit 41d2942

Browse files
committed
Java: Add support for bitwise compound assignments in Guards.
1 parent b02f1c8 commit 41d2942

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

java/ql/lib/semmle/code/java/controlflow/internal/GuardsLogic.qll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ predicate implies_v1(Guard g1, boolean b1, Guard g2, boolean b2) {
2323
or
2424
g1.(OrBitwiseExpr).getAnOperand() = g2 and b1 = false and b2 = false
2525
or
26+
g1.(AssignAndExpr).getSource() = g2 and b1 = true and b2 = true
27+
or
28+
g1.(AssignOrExpr).getSource() = g2 and b1 = false and b2 = false
29+
or
2630
g1.(AndLogicalExpr).getAnOperand() = g2 and b1 = true and b2 = true
2731
or
2832
g1.(OrLogicalExpr).getAnOperand() = g2 and b1 = false and b2 = false
@@ -61,11 +65,15 @@ predicate implies_v1(Guard g1, boolean b1, Guard g2, boolean b2) {
6165
)
6266
or
6367
exists(BaseSsaUpdate vbool |
68+
vbool.getDefiningExpr().(VariableAssign).getSource() = g2 or
69+
vbool.getDefiningExpr().(AssignOp) = g2
70+
|
6471
vbool.getAUse() = g1 and
65-
vbool.getDefiningExpr().(VariableAssign).getSource() = g2 and
6672
(b1 = true or b1 = false) and
6773
b2 = b1
6874
)
75+
or
76+
g1.(AssignExpr).getSource() = g2 and b2 = b1 and b1 = [true, false]
6977
}
7078

7179
/**
@@ -79,8 +87,10 @@ predicate implies_v2(Guard g1, boolean b1, Guard g2, boolean b2) {
7987
implies_v1(g1, b1, g2, b2)
8088
or
8189
exists(SsaExplicitUpdate vbool |
90+
vbool.getDefiningExpr().(VariableAssign).getSource() = g2 or
91+
vbool.getDefiningExpr().(AssignOp) = g2
92+
|
8293
vbool.getAUse() = g1 and
83-
vbool.getDefiningExpr().(VariableAssign).getSource() = g2 and
8494
(b1 = true or b1 = false) and
8595
b2 = b1
8696
)

java/ql/test/query-tests/Nullness/B.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,41 @@ public void corrConds5(Object y, Object z) {
371371
}
372372
}
373373

374+
public void bitwise(Object x, boolean b) {
375+
boolean notnull = x != null;
376+
377+
boolean g1 = notnull;
378+
g1 &= b;
379+
if (g1) {
380+
x.hashCode(); // OK
381+
}
382+
383+
boolean g2 = b;
384+
g2 &= notnull;
385+
if (g2) {
386+
x.hashCode(); // OK
387+
}
388+
389+
boolean g3 = !notnull;
390+
g3 |= b;
391+
if (!g3) {
392+
x.hashCode(); // OK
393+
}
394+
395+
boolean g4 = b;
396+
g4 |= !notnull;
397+
if (!g4) {
398+
x.hashCode(); // OK
399+
}
400+
401+
boolean g5 = g1 = b & notnull;
402+
if (g5) {
403+
x.hashCode(); // OK
404+
}
405+
406+
g5 |= b;
407+
if (g5) {
408+
x.hashCode(); // NPE
409+
}
410+
}
374411
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
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:408:7:408:7 | x | Variable $@ may be null here as suggested by $@ null guard. | B.java:374:23:374:30 | x | x | B.java:375:23:375:31 | ... != ... | this |
2021
| 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 |
2122
| 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 |
2223
| 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)