String IndexOf Invalid Range
ID |
java.string_indexof_invalid_range |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
reliability |
Description
Reports the three-argument String.indexOf(what, beginIndex, endIndex) (Java 21+) when the literal beginIndex/endIndex arguments describe an invalid range: beginIndex negative, or beginIndex greater than endIndex.
Rationale
Both shapes throw StringIndexOutOfBoundsException on every call, unconditionally — there is no input that makes them succeed. The most common cause is transposing the two range arguments.
String reference = "invoice-2026-Q3-EUR";
// Bad - 9..4 is not a valid range, throws every time
int a = reference.indexOf('-', 9, 4);
// Bad - beginIndex is negative
int b = reference.indexOf('-', -1, 9);