Skip to content

Commit b4bf7a1

Browse files
authored
Merge pull request github#7698 from aschackmull/java/bitwise-assignop-guards
Java: Add support for bitwise compound assignments in Guards.
2 parents 8e40899 + 5f7ee33 commit b4bf7a1

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@ 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
2933
or
3034
g1.(LogNotExpr).getExpr() = g2 and
3135
b1 = b2.booleanNot() and
32-
(b1 = true or b1 = false)
36+
b1 = [true, false]
3337
or
3438
exists(EqualityTest eqtest, boolean polarity, BooleanLiteral boollit |
3539
eqtest = g1 and
3640
eqtest.hasOperands(g2, boollit) and
3741
eqtest.polarity() = polarity and
38-
(b1 = true or b1 = false) and
42+
b1 = [true, false] and
3943
b2 = b1.booleanXor(polarity).booleanXor(boollit.getBooleanValue())
4044
)
4145
or
@@ -56,16 +60,20 @@ predicate implies_v1(Guard g1, boolean b1, Guard g2, boolean b2) {
5660
exists(MethodAccess check | check = g1 |
5761
conditionCheck(check, _) and
5862
g2 = check.getArgument(0) and
59-
(b1 = true or b1 = false) and
63+
b1 = [true, false] and
6064
b2 = b1
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
66-
(b1 = true or b1 = false) and
72+
b1 = [true, false] and
6773
b2 = b1
6874
)
75+
or
76+
g1.(AssignExpr).getSource() = g2 and b2 = b1 and b1 = [true, false]
6977
}
7078

7179
/**
@@ -79,9 +87,11 @@ 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
84-
(b1 = true or b1 = false) and
94+
b1 = [true, false] and
8595
b2 = b1
8696
)
8797
or

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)