Skip to content

Commit ba252cb

Browse files
committed
Java: Add a couple of difficult condition correlation tests.
1 parent 2d9470d commit ba252cb

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,39 @@ public void corrCondLoop2(boolean a[]) {
436436
}
437437
}
438438
}
439+
440+
public void loopCorrTest1(int[] a) {
441+
boolean ready = a.length > 7;
442+
Object x = new Object();
443+
for (int i = 0; i < a.length; i++) {
444+
// condition correlates with itself through iterations when ready isn't updated
445+
if (!ready) {
446+
x = null;
447+
} else {
448+
x.hashCode(); // Spurious NPE - false positive
449+
}
450+
if ((a[i] & 1) != 0) {
451+
ready = (a[i] & 2) != 0;
452+
x = new Object();
453+
}
454+
}
455+
}
456+
457+
public void loopCorrTest2(boolean[] a) {
458+
Object x = new Object();
459+
boolean cur = a[0];
460+
for (int i = 1; i < a.length; i++) {
461+
boolean prev = cur;
462+
cur = a[i];
463+
if (!prev) {
464+
// correctly guarded by !cur from the _previous_ iteration
465+
x.hashCode(); // Spurious NPE - false positive
466+
} else {
467+
x = new Object();
468+
}
469+
if (cur) {
470+
x = null;
471+
}
472+
}
473+
}
439474
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
| B.java:279:7:279:7 | a | Variable $@ may be null at this access 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 at this access because of $@ assignment. | B.java:287:5:287:44 | int[] b | b | B.java:287:11:287:43 | b | this |
2020
| B.java:408:7:408:7 | x | Variable $@ may be null at this access as suggested by $@ null guard. | B.java:374:23:374:30 | x | x | B.java:375:23:375:31 | ... != ... | this |
21+
| B.java:448:9:448:9 | x | Variable $@ may be null at this access because of $@ assignment. | B.java:442:5:442:28 | Object x | x | B.java:446:9:446:16 | ...=... | this |
22+
| B.java:465:9:465:9 | x | Variable $@ may be null at this access because of $@ assignment. | B.java:458:5:458:28 | Object x | x | B.java:470:9:470:16 | ...=... | this |
2123
| C.java:9:44:9:45 | a2 | Variable $@ may be null at this access as suggested by $@ null guard. | C.java:6:5:6:23 | long[][] a2 | a2 | C.java:7:34:7:54 | ... != ... | this |
2224
| C.java:9:44:9:45 | a2 | Variable $@ may be null at this access because of $@ assignment. | C.java:6:5:6:23 | long[][] a2 | a2 | C.java:6:14:6:22 | a2 | this |
2325
| C.java:10:17:10:18 | a3 | Variable $@ may be null at this access 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)