typetools / typetools/checker-framework

Index Checker does not recognize swapping index and offset

Open
#1,975 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement False Positive (false warning or imprecision) Index
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.