Skip to content

Java: Add a few qltest cases for nullness and range analysis FPs. #3526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions java/ql/test/query-tests/Nullness/C.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,28 @@ public void ex15(Object o1, Object o2) {
return;
}
}

private Object foo16;

private Object getFoo16() {
return this.foo16;
}

public static void ex16(C c) {
int[] xs = c.getFoo16() != null ? new int[5] : null;
if (c.getFoo16() != null) {
xs[0]++; // NPE - false positive
}
}

public static final int MAXLEN = 1024;

public void ex17() {
int[] xs = null;
// loop executes at least once
for (int i = 32; i <= MAXLEN; i *= 2) {
xs = new int[5];
}
xs[0]++; // OK
}
}
1 change: 1 addition & 0 deletions java/ql/test/query-tests/Nullness/NullMaybe.expected
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
| C.java:188:9:188:11 | obj | Variable $@ may be null here because of $@ assignment. | C.java:181:5:181:22 | Object obj | obj | C.java:181:12:181:21 | obj | this |
| C.java:207:9:207:11 | obj | Variable $@ may be null here because of $@ assignment. | C.java:201:5:201:22 | Object obj | obj | C.java:201:12:201:21 | obj | this |
| C.java:219:9:219:10 | o1 | Variable $@ may be null here as suggested by $@ null guard. | C.java:212:20:212:28 | o1 | o1 | C.java:213:9:213:18 | ... == ... | this |
| C.java:233:7:233:8 | xs | Variable $@ may be null here because of $@ assignment. | C.java:231:5:231:56 | int[] xs | xs | C.java:231:11:231:55 | xs | this |
| F.java:11:5:11:7 | obj | Variable $@ may be null here as suggested by $@ null guard. | F.java:8:18:8:27 | obj | obj | F.java:9:9:9:19 | ... == ... | this |
| F.java:17:5:17:7 | obj | Variable $@ may be null here as suggested by $@ null guard. | F.java:14:18:14:27 | obj | obj | F.java:15:9:15:19 | ... == ... | this |
19 changes: 19 additions & 0 deletions java/ql/test/query-tests/RangeAnalysis/A.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,23 @@ void m13(int n) {
}
}
}

void m14(int[] xs) {
for (int i = 0; i < xs.length + 1; i++) {
if (i == 0 && xs.length > 0) {
xs[i]++; // OK - FP
}
}
}

void m15(int[] xs) {
for (int i = 0; i < xs.length; i++) {
int x = ++i;
int y = ++i;
if (y < xs.length) {
xs[x]++; // OK - FP
xs[y]++; // OK
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
| A.java:111:14:111:21 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length + 1. |
| A.java:122:16:122:23 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length + 3. |
| A.java:134:16:134:23 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
| A.java:182:9:182:13 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
| A.java:192:9:192:13 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |