Skip to content

Commit 2d6d8aa

Browse files
committed
Java: Account for additional constants in ArrayIndexOutOfBounds query.
1 parent a1aeb99 commit 2d6d8aa

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

java/ql/src/Likely Bugs/Collections/ArrayIndexOutOfBounds.ql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ predicate boundedArrayAccess(ArrayAccess aa, int k) {
4444
)
4545
)
4646
)
47+
or
48+
exists(Field arr, Expr index, int delta, int arrlen |
49+
aa.getIndexExpr() = index and
50+
aa.getArray() = arr.getAnAccess() and
51+
bounded(index, any(ZeroBound z), delta, true, _) and
52+
arr.isFinal() and
53+
arr.getInitializer().(ArrayCreationExpr).getFirstDimensionSize() = arrlen and
54+
k = delta - arrlen
55+
)
4756
}
4857

4958
/**

java/ql/test/query-tests/RangeAnalysis/A.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,11 @@ static int m16() {
204204
A.arr1[RandomUtils.nextInt(0, arr1.length + 1)] + // BAD: random int may be out of range
205205
A.arr1[RandomUtils.nextInt(0, arr1.length)]; // GOOD: random int must be in range
206206
}
207+
208+
int m17() {
209+
return this.arr2[(new Random()).nextInt(arr2.length + 1)] + // BAD: random int may be out of range
210+
this.arr2[(new Random()).nextInt(arr2.length)] + // GOOD: random int must be in range
211+
this.arr2[RandomUtils.nextInt(0, arr2.length + 1)] + // BAD: random int may be out of range
212+
this.arr2[RandomUtils.nextInt(0, arr2.length)]; // GOOD: random int must be in range
213+
}
207214
}

java/ql/test/query-tests/RangeAnalysis/ArrayIndexOutOfBounds.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
| A.java:195:9:195:13 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
1515
| A.java:202:12:202:58 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
1616
| A.java:204:7:204:53 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
17+
| A.java:209:12:209:61 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
18+
| A.java:211:7:211:56 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |

0 commit comments

Comments
 (0)