Skip to content

Java: FP in java/index-out-of-bounds when modulo checked #986

@s0

Description

@s0

When the length of the array is confirmed to be 0 (mod n) where n is the increment value within the for loop, and this check is done immediately before the loop starts. As long as:

  • the loop variable isn't modified anywhere else within the loop body
  • the length of the array does not change (which will not happen for primitives unless reassigned).

we can be fairly certain that array accesses arr[i] .. arr[i + n - 1] will not be out-of bounds.

see: https://discuss.lgtm.com/t/false-positive-java-index-out-of-bounds-with-preconditions-check/1787

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions