Skip to content

Commit a78a0b5

Browse files
committed
Java: Add test.
1 parent 8659bed commit a78a0b5

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
public class A {
2+
private static final int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
3+
private final int[] arr2;
4+
private final int[] arr3;
5+
6+
public A(int[] arr2, int n) {
7+
if (arr2.length % 2 != 0)
8+
throw new Exception();
9+
this.arr2 = arr2;
10+
this.arr3 = new int[n << 1];
11+
}
12+
13+
void m1(int[] a) {
14+
int sum = 0;
15+
for (int i = 0; i <= a.length; i++) {
16+
sum += a[i]; // Out of bounds
17+
}
18+
}
19+
20+
void m2(int[] a) {
21+
int sum = 0;
22+
for (int i = 0; i < a.length; i += 2) {
23+
sum += a[i] + a[i + 1]; // Out of bounds (unless len%2==0)
24+
}
25+
}
26+
27+
void m3(int[] a) {
28+
if (a.length % 2 != 0)
29+
return;
30+
int sum = 0;
31+
for (int i = 0; i < a.length; ) {
32+
sum += a[i++]; // OK
33+
sum += a[i++]; // OK
34+
}
35+
for (int i = 0; i < arr1.length; ) {
36+
sum += arr1[i++]; // OK
37+
sum += arr1[i++]; // OK - FP
38+
i += 2;
39+
}
40+
for (int i = 0; i < arr2.length; ) {
41+
sum += arr2[i++]; // OK
42+
sum += arr2[i++]; // OK - FP
43+
}
44+
for (int i = 0; i < arr3.length; ) {
45+
sum += arr3[i++]; // OK
46+
sum += arr3[i++]; // OK - FP
47+
}
48+
int[] b;
49+
if (sum > 3)
50+
b = a;
51+
else
52+
b = arr1;
53+
for (int i = 0; i < b.length; i++) {
54+
sum += b[i]; // OK
55+
sum += b[++i]; // OK - FP
56+
}
57+
}
58+
59+
void m4(int[] a, int[] b) {
60+
assert a.length % 2 == 0;
61+
int sum = 0;
62+
for (int i = 0; i < a.length; ) {
63+
sum += a[i++]; // OK
64+
sum += a[i++]; // OK - FP
65+
}
66+
int len = b.length;
67+
if ((len & 1) != 0)
68+
return;
69+
for (int i = 0; i < len; ) {
70+
sum += b[i++]; // OK
71+
sum += b[i++]; // OK
72+
}
73+
}
74+
75+
void m5(int n) {
76+
int[] a = new int[3 * n];
77+
int sum = 0;
78+
for (int i = 0; i < a.length; i += 3) {
79+
sum += a[i] + a[i + 1] + a[i + 2]; // OK - FP
80+
}
81+
}
82+
83+
int m6(int[] a, int ix) {
84+
if (ix < 0 || ix > a.length)
85+
return 0;
86+
return a[ix]; // Out of bounds
87+
}
88+
89+
void m7() {
90+
int[] xs = new int[11];
91+
int sum = 0;
92+
for (int i = 0; i < 11; i++) {
93+
for (int j = 0; j < 11; j++) {
94+
sum += xs[i]; // OK
95+
sum += xs[j]; // OK
96+
if (i < j)
97+
sum += xs[i + 11 - j]; // OK - FP
98+
else
99+
sum += xs[i - j]; // OK
100+
}
101+
}
102+
}
103+
104+
void m8(int[] a) {
105+
if ((a.length - 4) % 3 != 0)
106+
return;
107+
int sum = 0;
108+
for (int i = 4; i < a.length; i += 3) {
109+
sum += a[i]; // OK
110+
sum += a[i + 1]; // OK - FP
111+
sum += a[i + 2]; // OK - FP
112+
}
113+
}
114+
115+
void m9() {
116+
int[] a = new int[] { 1, 2, 3, 4, 5 };
117+
int sum = 0;
118+
for (int i = 0; i < 10; i++) {
119+
if (i < 5)
120+
sum += a[i]; // OK
121+
else
122+
sum += a[9 - i]; // OK - FP
123+
}
124+
}
125+
126+
void m10(int n, int m) {
127+
int len = Math.min(n, m);
128+
int[] a = new int[n];
129+
int sum = 0;
130+
for (int i = n - 1; i >= 0; i--) {
131+
sum += a[i]; // OK
132+
for (int j = i + 1; j < len; j++) {
133+
sum += a[j]; // OK
134+
sum += a[i + 1]; // OK - FP
135+
}
136+
}
137+
}
138+
139+
void m11(int n) {
140+
int len = n*2;
141+
int[] a = new int[len];
142+
int sum = 0;
143+
for (int i = 0; i < len; i = i + 2) {
144+
sum += a[i+1]; // OK
145+
for (int j = i; j < len - 2; j = j + 2) {
146+
sum += a[j]; // OK
147+
sum += a[j+1]; // OK
148+
sum += a[j+2]; // OK
149+
sum += a[j+3]; // OK
150+
}
151+
}
152+
}
153+
154+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
| A.java:16:14:16:17 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
2+
| A.java:23:21:23:28 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
3+
| A.java:37:14:37:22 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
4+
| A.java:42:14:42:22 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
5+
| A.java:46:14:46:22 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
6+
| A.java:55:14:55:19 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
7+
| A.java:64:14:64:19 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
8+
| A.java:79:21:79:28 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
9+
| A.java:79:32:79:39 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length + 1. |
10+
| A.java:86:12:86:16 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
11+
| A.java:97:18:97:31 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length + 8. |
12+
| A.java:110:14:110:21 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
13+
| 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. |
14+
| 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. |
15+
| A.java:134:16:134:23 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Likely Bugs/Collections/ArrayIndexOutOfBounds.ql

0 commit comments

Comments
 (0)