This rule raises an issue when an integer calculation always overflows or uses an overflow-prone midpoint formula.
Java does not report integer overflow. When an int or long calculation exceeds the range of its result type, the value
silently wraps around.
The wrapped value can produce an incorrect index, size, limit, midpoint, or security-relevant result.
Widening the destination type does not protect a calculation performed with narrower operands. At least one operand must be widened before the
operation. When overflow must be rejected, use an exact arithmetic method such as Math.addExact.
For ordered non-negative bounds, calculate a midpoint by subtracting the lower bound before adding it back.
int total = Integer.MAX_VALUE + 1; // Noncompliant
long total = (long) Integer.MAX_VALUE + 1;
int delta = Integer.MIN_VALUE - 1; // Noncompliant
long delta = (long) Integer.MIN_VALUE - 1;
int size = 1_000_000 * 10_000; // Noncompliant
long size = 1_000_000L * 10_000;
final int value = Integer.MIN_VALUE; int opposite = -value; // Noncompliant
final int value = Integer.MIN_VALUE; long opposite = -(long) value;
int midpoint = (low + high) / 2; // Noncompliant
int midpoint = low + (high - low) / 2;
final int increment = 1; int next = Integer.MAX_VALUE + increment; // Noncompliant
final int increment = 1; int next = Math.addExact(Integer.MAX_VALUE, increment);
Math.addExact(int,
int)