Skip to content

Commit 1d7bab9

Browse files
committed
Java: Add some more nullness tests.
1 parent 80b79f7 commit 1d7bab9

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,46 @@ public void trackTest(Object o, int n) {
515515
if (c == 100) { return; }
516516
o.hashCode(); // NPE
517517
}
518+
519+
public void testFinally(int[] xs, int[] ys) {
520+
if (xs.length == 0) return;
521+
if (ys.length == 0) return;
522+
String s1 = null;
523+
String s2 = null;
524+
for (int x : xs) {
525+
try {
526+
if (x == 0) { break; }
527+
s1 = "1";
528+
for (int y : ys) {
529+
if (y == 0) { break; }
530+
s2 = "2";
531+
}
532+
} finally {
533+
}
534+
s1.hashCode(); // OK
535+
s2.hashCode(); // NPE
536+
}
537+
s1.hashCode(); // NPE - false negative, Java CFG lacks proper edge label
538+
}
539+
540+
public void lenCheck(int[] xs, int n, int t) {
541+
if (n > 42) return;
542+
if (maybe) {}
543+
if (n > 0 && (xs == null || xs.length < n)) {
544+
return;
545+
}
546+
if (maybe) {}
547+
if (n > 21) return;
548+
if (maybe) {}
549+
for (int i = 0; i < n; ++i) {
550+
xs[i]++; // Spurious NPE - false positive
551+
}
552+
}
553+
554+
public void rangetest(int n) {
555+
String s = null;
556+
if (n < 0 || n > 10) s = "A";
557+
if (n > 100) s.hashCode(); // Spurious NPE - false positive
558+
if (n == 42) s.hashCode(); // Spurious NPE - false positive
559+
}
518560
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
| 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 |
2323
| B.java:487:9:487:9 | x | Variable $@ may be null at this access because of $@ assignment. | B.java:476:5:476:20 | Object x | x | B.java:476:12:476:19 | x | this |
2424
| B.java:516:5:516:5 | o | Variable $@ may be null at this access as suggested by $@ null guard. | B.java:511:25:511:32 | o | o | B.java:512:22:512:30 | ... == ... | this |
25+
| B.java:535:7:535:8 | s2 | Variable $@ may be null at this access because of $@ assignment. | B.java:523:5:523:21 | String s2 | s2 | B.java:523:12:523:20 | s2 | this |
26+
| B.java:550:7:550:8 | xs | Variable $@ may be null at this access as suggested by $@ null guard. | B.java:540:24:540:31 | xs | xs | B.java:543:19:543:28 | ... == ... | this |
27+
| B.java:557:18:557:18 | s | Variable $@ may be null at this access because of $@ assignment. | B.java:555:5:555:20 | String s | s | B.java:555:12:555:19 | s | this |
28+
| B.java:558:18:558:18 | s | Variable $@ may be null at this access because of $@ assignment. | B.java:555:5:555:20 | String s | s | B.java:555:12:555:19 | s | this |
2529
| 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 |
2630
| 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 |
2731
| 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)