typetools / typetools/checker-framework
Index Checker does not recognize swapping index and offset
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
In @LTLengthOf, the offset and the index can be swapped (index+offset<length is equivalent to offset+index<length). However, the Index Checker does not recognize this equivalence.
Example of code that is safe:
import org.checkerframework.checker.index.qual.LTLengthOf;
public class SwapOffsetIndex {
public void m(Object[] a, int offset, @LTLengthOf(value="#1", offset="#2") int index) {
@LTLengthOf("a") int i = offset + index; // OK
@LTLengthOf(value="a", offset="offset") int i1 = index; // OK
@LTLengthOf(value="a", offset="index") int i2 = offset; // false positive
}
}
Current output:
SwapOffsetIndex.java:7: error: [assignment.type.incompatible] incompatible types in assignment.
@LTLengthOf(value="a", offset="index") int i2 = offset; // false positive
^
found : @UpperBoundUnknown int
required: @LTLengthOf(value="a", offset="index") int
1 error
In this example from Guava, count is known to be @LTLengthOf(value="array", offset="values.length - 1") because of a postcondition, but the method call requires that values.length is @LTLengthOf(value="array", offset="count - 1").
import org.checkerframework.checker.index.qual.EnsuresLTLengthOf;
import org.checkerframework.checker.index.qual.IndexOrHigh;
import org.checkerframework.checker.index.qual.NonNegative;
public class Arraycopy {
long[] array;
@IndexOrHigh("array") int count;
@SuppressWarnings("contracts.postcondition.not.satisfied")
@EnsuresLTLengthOf(value = "count", targetValue = "array", offset = "#1 - 1")
private void ensureRoomFor(@NonNegative int numberToAdd) {
}
public void addAll(long[] values) {
ensureRoomFor(values.length);
System.arraycopy(values, 0, array, count, values.length); // false positive
}
}
Output:
Arraycopy.java:17: error: [argument.type.incompatible] incompatible types in argument.
System.arraycopy(values, 0, array, count, values.length); // false positive
^
found : @LTEqLengthOf("values") int
required: @LTLengthOf(value={"values", "this.array"}, offset={"-1", "this.count - 1"}) int
1 error
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 by reproducing the SwapOffsetIndex and Arraycopy examples, then trace the Index Checker handling of @LTLengthOf and @EnsuresLTLengthOf. Done means swapped index and offset expressions are recognized consistently and both examples no longer produce false positives, with regression coverage for these cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100