Skip to content

Commit 14c72de

Browse files
authored
Merge pull request #21983 from owen-mc/java/convert-to-inline-expectation-tests
Java: Improve inline expectations test comments
2 parents 6934aa0 + 4bc083f commit 14c72de

7 files changed

Lines changed: 68 additions & 68 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public void corrConds3(Object y) {
331331
x = new Object();
332332
}
333333
if(y instanceof String) {
334-
x.hashCode(); // $ Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
334+
x.hashCode(); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
335335
}
336336
}
337337

@@ -341,7 +341,7 @@ public void corrConds4(Object y) {
341341
x = new Object();
342342
}
343343
if(!(y instanceof String)) {
344-
x.hashCode(); // $ Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
344+
x.hashCode(); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
345345
}
346346
}
347347

@@ -351,23 +351,23 @@ public void corrConds5(Object y, Object z) {
351351
x = new Object();
352352
}
353353
if(y == z) {
354-
x.hashCode(); // $ Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
354+
x.hashCode(); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
355355
}
356356

357357
Object x2 = null;
358358
if(y != z) {
359359
x2 = new Object();
360360
}
361361
if(y != z) {
362-
x2.hashCode(); // $ Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
362+
x2.hashCode(); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
363363
}
364364

365365
Object x3 = null;
366366
if(y != z) {
367367
x3 = new Object();
368368
}
369369
if(!(y == z)) {
370-
x3.hashCode(); // $ Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
370+
x3.hashCode(); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
371371
}
372372
}
373373

@@ -462,7 +462,7 @@ public void loopCorrTest2(boolean[] a) {
462462
cur = a[i];
463463
if (!prev) {
464464
// correctly guarded by !cur from the _previous_ iteration
465-
x.hashCode(); // $ Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
465+
x.hashCode(); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
466466
} else {
467467
x = new Object();
468468
}
@@ -484,7 +484,7 @@ public void loopCorrTest3(String[] ss) {
484484
t = new Object();
485485
}
486486
// correctly guarded by t: null -> String -> Object
487-
x.hashCode(); // $ Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
487+
x.hashCode(); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
488488
}
489489
}
490490
}
@@ -573,7 +573,7 @@ public void testFinally2(int[] xs) {
573573
} finally {
574574
}
575575
}
576-
s.hashCode(); // $ Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
576+
s.hashCode(); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // Spurious NPE - false positive
577577
// CFG reachability does not distinguish abrupt successors
578578
}
579579
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public void ex1(long[][][] a1, int ix, int len) {
66
long[][] a2 = null;
77
boolean haveA2 = ix < len && (a2 = a1[ix]) != null;
88
long[] a3 = null;
9-
final boolean haveA3 = haveA2 && (a3 = a2[ix]) != null; // $ Alert[java/dereferenced-value-may-be-null] // NPE - false positive
10-
if (haveA3) a3[0] = 0; // $ Alert[java/dereferenced-value-may-be-null] // NPE - false positive
9+
final boolean haveA3 = haveA2 && (a3 = a2[ix]) != null; // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // NPE - false positive
10+
if (haveA3) a3[0] = 0; // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // NPE - false positive
1111
}
1212

1313
public void ex2(boolean x, boolean y) {
@@ -18,7 +18,7 @@ public void ex2(boolean x, boolean y) {
1818
s2 = (s1 == null) ? null : "";
1919
}
2020
if (s2 != null)
21-
s1.hashCode(); // $ Alert[java/dereferenced-value-may-be-null] // NPE - false positive
21+
s1.hashCode(); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // NPE - false positive
2222
}
2323

2424
public void ex3(List<String> ss) {
@@ -48,7 +48,7 @@ public void ex4(Iterable<String> list, int step) {
4848
slice = new ArrayList<>();
4949
result.add(slice);
5050
}
51-
slice.add(str); // $ Alert[java/dereferenced-value-may-be-null] // NPE - false positive
51+
slice.add(str); // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // NPE - false positive
5252
++index;
5353
iter.remove();
5454
}
@@ -141,7 +141,7 @@ public void ex9(boolean cond, Object obj1) {
141141
public void ex10(int[] a) {
142142
int n = a == null ? 0 : a.length;
143143
for (int i = 0; i < n; i++) {
144-
int x = a[i]; // $ Alert[java/dereferenced-value-may-be-null] // NPE - false positive
144+
int x = a[i]; // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // NPE - false positive
145145
if (x > 7)
146146
a = new int[n];
147147
}
@@ -216,7 +216,7 @@ public void ex15(Object o1, Object o2) {
216216
if (o1 == o2) {
217217
return;
218218
}
219-
if (o1.equals(o2)) { // $ Alert[java/dereferenced-value-may-be-null] // NPE - false positive
219+
if (o1.equals(o2)) { // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // NPE - false positive
220220
return;
221221
}
222222
}
@@ -230,7 +230,7 @@ private Object getFoo16() {
230230
public static void ex16(C c) {
231231
int[] xs = c.getFoo16() != null ? new int[5] : null;
232232
if (c.getFoo16() != null) {
233-
xs[0]++; // $ Alert[java/dereferenced-value-may-be-null] // NPE - false positive
233+
xs[0]++; // $ SPURIOUS: Alert[java/dereferenced-value-may-be-null] // NPE - false positive
234234
}
235235
}
236236

java/ql/test/query-tests/UseBraces/UseBraces.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ void test(boolean bb)
1111
{
1212
int x = 0, y;
1313
int[] branches = new int[10];
14-
14+
1515
// If-then statement
16-
16+
1717
if(1==1)
1818
{
1919
f();
2020
}
2121
g(); // No alert
22-
23-
if(1==1)
22+
23+
if(1==1)
2424
f();
2525
g(); // No alert
26-
26+
2727
if(1==1)
2828
f(); // $ Alert
29-
g(); // Alert
30-
29+
g();
30+
3131
if(1==1)
32-
f(); g(); // $ Alert // Alert
32+
f(); g(); // $ Alert
3333

3434
// If-then-else statement
3535

@@ -41,29 +41,29 @@ void test(boolean bb)
4141
{
4242
g();
4343
}
44-
44+
4545
g(); // No alert
46-
46+
4747
if(1==2)
4848
f();
4949
else
5050
g();
5151
f(); // No alert
52-
52+
5353
if(true)
5454
{
5555
f();
5656
}
5757
else
5858
f(); // $ Alert
59-
g(); // Alert
60-
59+
g();
60+
6161
if(true)
6262
{
6363
f();
6464
}
6565
else
66-
f(); g(); // $ Alert // Alert
66+
f(); g(); // $ Alert
6767

6868
// While statement
6969

@@ -80,44 +80,44 @@ void test(boolean bb)
8080

8181
while(bb )
8282
f(); // $ Alert
83-
g(); // Alert
83+
g();
8484
g(); // No alert
8585

8686
while(bb )
87-
f(); g(); // $ Alert // Alert
87+
f(); g(); // $ Alert
8888

8989

9090
while(bb)
9191
if (x != 0) x = 1;
9292

9393
// Do-while statement
94-
94+
9595
do
9696
f();
9797
while(false);
9898
g(); // No alert
99-
99+
100100
// For statement
101101
for(int i=0; i<10; ++i)
102102
{
103103
f();
104104
}
105105
g();
106-
106+
107107
for(int i=0; i<10; ++i)
108108
f();
109109
g();
110-
110+
111111
for(int i=0; i<10; ++i)
112112
f(); // $ Alert
113-
g(); // Alert
113+
g();
114114

115115
for(int i=0; i<10; ++i)
116-
f(); g(); // $ Alert // Alert
116+
f(); g(); // $ Alert
117+
117118

118-
119119
// Foreach statement
120-
120+
121121
for( int b : branches)
122122
x += b;
123123
f();
@@ -130,42 +130,42 @@ void test(boolean bb)
130130

131131
for( int b : branches)
132132
f(); // $ Alert
133-
g(); // Alert
133+
g();
134134

135135
for( int b : branches)
136-
f(); g(); // $ Alert // Alert
136+
f(); g(); // $ Alert
137137

138138
// Nested ifs
139139
if( true )
140140
if(false)
141141
f();
142142
g(); // No alert
143-
143+
144144
if( true )
145145
if(false) // $ Alert
146146
f();
147-
g(); // Alert
148-
147+
g();
148+
149149
if( true )
150150
;
151-
else
151+
else
152152
if (false)
153153
f();
154154
g(); // No alert
155155

156156
if( true )
157157
;
158-
else
158+
else
159159
if (false)
160160
f();
161-
g(); // false negative
161+
g(); // $ MISSING: Alert // false negative
162162

163163
if( true )
164164
;
165165
else if (false)
166166
f(); // $ Alert
167-
g(); // Alert
168-
167+
g();
168+
169169
// Nested combinations
170170
if (true)
171171
while (x<10)
@@ -175,7 +175,7 @@ else if (false)
175175
if (true)
176176
while (x<10) // $ Alert
177177
f();
178-
g(); // Alert
178+
g();
179179

180180
while (x<10)
181181
if (true)
@@ -185,7 +185,7 @@ else if (false)
185185
while (x<10)
186186
if (true) // $ Alert
187187
f();
188-
g(); // Alert
188+
g();
189189

190190
if (true)
191191
f();

java/ql/test/query-tests/security/CWE-113/semmle/tests/ResponseSplitting.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public void sanitizerTests(HttpServletRequest request, HttpServletResponse respo
6262
response.setHeader("h", t.replace('\n', ' ').replace('\r', ' '));
6363

6464
// FALSE NEGATIVE: replace only some line breaks
65-
response.setHeader("h", t.replace('\n', ' '));
65+
response.setHeader("h", t.replace('\n', ' ')); // $ MISSING: Alert
6666

6767
// FALSE NEGATIVE: replace only some line breaks
68-
response.setHeader("h", t.replaceAll("\r", ""));
68+
response.setHeader("h", t.replaceAll("\r", "")); // $ MISSING: Alert
6969

7070
// GOOD: replace all linebreaks with a simple regex
7171
response.setHeader("h", t.replaceAll("\n", "").replaceAll("\r", ""));

java/ql/test/query-tests/security/CWE-190/semmle/tests/ArithmeticTainted.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void main(String[] args) {
7878
// FALSE NEGATIVE: stillTainted could still be very large, even
7979
// after
8080
// it has had arithmetic done on it
81-
int output = stillTainted + 100;
81+
int output = stillTainted + 100; // $ MISSING: Alert[java/tainted-arithmetic]
8282
}
8383
}
8484

@@ -107,7 +107,7 @@ public void main(String[] args) {
107107
}
108108
int output = data + 1;
109109
}
110-
110+
111111
{
112112
double x= Double.MAX_VALUE;
113113
// OK: CWE-190 only pertains to integer arithmetic

java/ql/test/query-tests/security/CWE-190/semmle/tests/Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static void main(String[] args) {
8484
// FALSE POSITIVE: the query check purely based on the type, it
8585
// can't try to
8686
// determine whether the value may in fact always be in bounds
87-
i += j; // $ Alert[java/implicit-cast-in-compound-assignment]
87+
i += j; // $ SPURIOUS: Alert[java/implicit-cast-in-compound-assignment]
8888
}
8989

9090
// ArithmeticWithExtremeValues
@@ -224,7 +224,7 @@ public static void main(String[] args) {
224224
// FALSE NEGATIVE: stillLarge could still be very large, even
225225
// after
226226
// it has had arithmetic done on it
227-
int output = stillLarge + 100;
227+
int output = stillLarge + 100; // $ MISSING: Alert[java/uncontrolled-arithmetic]
228228
}
229229
}
230230

@@ -263,7 +263,7 @@ public static void main(String[] args) {
263263
// FALSE NEGATIVE: stillLarge could still be very large, even
264264
// after
265265
// it has had arithmetic done on it
266-
int output = stillLarge + 100;
266+
int output = stillLarge + 100; // $ MISSING: Alert[java/uncontrolled-arithmetic]
267267
}
268268
}
269269

0 commit comments

Comments
 (0)