Skip to content

Commit d406646

Browse files
author
Max Schaefer
committed
Java: Add tests for comparison-with-wider-type.
1 parent eb9c734 commit d406646

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| ComparisonWithWiderType.java:4:25:4:29 | ... < ... | Comparison between $@ of type int and $@ of wider type long. | ComparisonWithWiderType.java:4:25:4:25 | i | expression | ComparisonWithWiderType.java:4:29:4:29 | l | expression |
2+
| ComparisonWithWiderType.java:16:26:16:30 | ... > ... | Comparison between $@ of type byte and $@ of wider type short. | ComparisonWithWiderType.java:16:30:16:30 | b | expression | ComparisonWithWiderType.java:16:26:16:26 | c | expression |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class ComparisonWithWiderType {
2+
public void testLt(long l) {
3+
// BAD: loop variable is an int, but the upper bound is a long
4+
for (int i = 0; i < l; i++) {
5+
System.out.println(i);
6+
}
7+
8+
// GOOD: loop variable is a long
9+
for (long i = 0; i < l; i++) {
10+
System.out.println(i);
11+
}
12+
}
13+
14+
public void testGt(short c) {
15+
// BAD: loop variable is a byte, but the upper bound is a short
16+
for (byte b = 0; c > b; b++) {
17+
System.out.println(b);
18+
}
19+
}
20+
21+
public void testLe(int i) {
22+
// GOOD: loop variable is a long, and the upper bound is an int
23+
for (long l = 0; l <= i; l++) {
24+
System.out.println(l);
25+
}
26+
}
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-190/ComparisonWithWiderType.ql

0 commit comments

Comments
 (0)