Skip to content

Commit

Permalink
Java: Account for additional constants in ArrayIndexOutOfBounds query.
Browse files Browse the repository at this point in the history
  • Loading branch information
aschackmull committed Feb 3, 2023
1 parent a1aeb99 commit 2d6d8aa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions java/ql/src/Likely Bugs/Collections/ArrayIndexOutOfBounds.ql
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ predicate boundedArrayAccess(ArrayAccess aa, int k) {
)
)
)
or
exists(Field arr, Expr index, int delta, int arrlen |
aa.getIndexExpr() = index and
aa.getArray() = arr.getAnAccess() and
bounded(index, any(ZeroBound z), delta, true, _) and
arr.isFinal() and
arr.getInitializer().(ArrayCreationExpr).getFirstDimensionSize() = arrlen and
k = delta - arrlen
)
}

/**
Expand Down
7 changes: 7 additions & 0 deletions java/ql/test/query-tests/RangeAnalysis/A.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,11 @@ static int m16() {
A.arr1[RandomUtils.nextInt(0, arr1.length + 1)] + // BAD: random int may be out of range
A.arr1[RandomUtils.nextInt(0, arr1.length)]; // GOOD: random int must be in range
}

int m17() {
return this.arr2[(new Random()).nextInt(arr2.length + 1)] + // BAD: random int may be out of range
this.arr2[(new Random()).nextInt(arr2.length)] + // GOOD: random int must be in range
this.arr2[RandomUtils.nextInt(0, arr2.length + 1)] + // BAD: random int may be out of range
this.arr2[RandomUtils.nextInt(0, arr2.length)]; // GOOD: random int must be in range
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
| A.java:195:9:195:13 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
| A.java:202:12:202:58 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
| A.java:204:7:204:53 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
| A.java:209:12:209:61 | ...[...] | This array access might be out of bounds, as the index might be equal to the array length. |
| 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 comments on commit 2d6d8aa

Please sign in to comment.