panacekcz / checker-framework

Pluggable type-checking for Java
http://checkerframework.org/
Other
0 stars 0 forks source link

How to write index annotations on index ranges? #5

Open panacekcz opened 7 years ago

panacekcz commented 7 years ago

This applies to both strings and arrays. There are several methods that take a string or array and a range expressed as a pair of integers.

Begin and End

For example, Arrays.sort(char[] a, int fromIndex, int toIndex), or String.substring(int beginIndex, int endIndex).

Offset and Length

For example, String(char[] value, int offset, int count).

Conversion to Begin and End

If a method takes offset and length and calls a method taking begin and end, it calls it like s.substring(offset, offset + length). Using the current annotations, it is not guaranteed that the second index will be valid.

For example in plume-lib, CountingPrintWriter.java, the index checker warns about such call:

src/plume/CountingPrintWriter.java:408: warning: [argument.type.incompatible] incompatible types in argument.
    writtenBytes += countBytes(s.substring(off, off + len));
                                                    ^
  found   : @LTLengthOf(value={"s", "s"}, offset={"-len", "-off - 1"}) int
  required: @LTEqLengthOf("s") int
panacekcz commented 7 years ago

System.arraycopy uses offsets on the length parameter: typetools#1076