typetools / typetools/checker-framework
IndexChecker treats 10 differently than a.length when a has type @ArrayLen(10)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
Consider this snippet:
import org.checkerframework.checker.index.qual.IndexOrHigh;
import org.checkerframework.common.value.qual.ArrayLen;
import org.checkerframework.common.value.qual.IntRange;
public class Testing {
void method(String @ArrayLen(10) [] array, @IndexOrHigh("#1") int index) {
if (index == 10) {
index = 0;
}
array[index] = "hello"; // Error here
}
void methodWithIntRangeParameter(String @ArrayLen(10) [] array, @IntRange(from = 0, to = 10) int index) {
if (index == 10) {
index = 0;
}
array[index] = "hello";
}
void methodWithNegativeTest(String @ArrayLen(10) [] array, @IndexOrHigh("#1") int index) {
if (index != 10) {
array[index] = "hello"; // Error here
}
}
void methodWithArrayLengthTest(String @ArrayLen(10) [] array, @IndexOrHigh("#1") int index) {
if (index != array.length) {
array[index] = "hello";
}
}
void methodWithUnguardedAssignment(String @ArrayLen(10) [] array, @IndexOrHigh("#1") int index) {
index = 0;
array[index] = "hello";
}
}
Running javac -processor org.checkerframework.checker.index.IndexChecker Testing.java issues 2 errors:
Error:(11, 15) java: [array.access.unsafe.high] Potentially unsafe array access: the index could be larger than the array's bound
found : @LTEqLengthOf("array") int
required: @IndexFor("array") or @LTLengthOf("array") -- an integer less than array's length
Error:(23, 19) java: [array.access.unsafe.high] Potentially unsafe array access: the index could be larger than the array's bound
found : @LTEqLengthOf("array") int
required: @IndexFor("array") or @LTLengthOf("array") -- an integer less than array's length
The code is safe because we never get to access 'array' with an invalid index. The if statements before make sure that the index is within bounds.
Comparing the behavior of the first two methods, it looks like the checker can't see that the length of the array is the same as number 10. The next two methods point out something similar: array.length is treated differently from number 10, even though 'array' has been annotated with @ArrayLen(10).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproduced Testing.java snippet and run javac with org.checkerframework.checker.index.IndexChecker to confirm the two reported errors and the safe cases. Trace how IndexChecker handles @ArrayLen(10), literal 10, and array.length in its index refinement. Done means the safe accesses pass while the negative test remains rejected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100