typetools / typetools/checker-framework

Index Checker cannot estimate value of a variable (which is declared outside the loop) inside the loop

Open
#2,507 3 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

Consider the following code:

class tryit{
  public static void check_it(int[] a) {
 		int index = 0;
 		for(int x = 0 ; x < a.length ; x++){
 			a[index++] = 0;
 		}
    }
}

Run it by typing in the terminal:
javac -processor index tryit.java

Output:

tryit.java:6: error: [array.access.unsafe.high.range] Potentially unsafe array access: the index could be larger than the array's bound
 			a[index++] = 0;
 			       ^
  index type found: @IntRange(from=-2147483648, to=9223372036854775807) int
  array type found: @UnknownVal int @UnknownVal []
  required        : index of type @IndexFor("a") or @LTLengthOf("a"), or array of type @MinLen(-9223372036854775808)
1 error

Firstly, the checker is imprecise in estimating the value of index inside the loop.
Second, the error message @MinLen(-9223372036854775808) seem absurd.

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

Reproduce the report using tryit.java and javac -processor index tryit.java, then trace the Index Checker handling of the variable declared before the loop. Investigate why the loop does not refine index and why the diagnostic reports @MinLen(-9223372036854775808). Done means the example is analyzed correctly and the reported range is sensible.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.