The GitHub Actions job "Java CI" on 
commons-math.git/MATH-1696-int-distance-overflow has failed.
Run started by GitHub user simpleqt (triggered by aherbert).

Head commit for run:
37e1259c3fbc06379aeb1ddf42799d8da4c82f31 / simpleqt <[email protected]>
MathArrays: fix int overflow in the int[] L1 and Linf distance

distance1(int[], int[]) and distanceInf(int[], int[]) computed the
offset of each pair as JdkMath.abs(p1[i] - p2[i]) in int. The difference
of two int values does not always fit in an int, and Math.abs does not
make it fit: abs(Integer.MAX_VALUE - -1) overflows to Integer.MIN_VALUE,
and abs(Integer.MIN_VALUE) is Integer.MIN_VALUE itself. A distance that
should be large was therefore returned as a negative value, or as zero
when the overflow was then "absolute-valued" a second time by max().

Widen the subtraction to long before taking the absolute value, and
accumulate the whole result in a long: distance1 sums the offsets and
distanceInf takes their maximum, and both clamp to Integer.MAX_VALUE only
on return. The accumulator cannot overflow a long — two maximum-length
arrays with Integer.MIN_VALUE in one and Integer.MAX_VALUE in the other
give (2^31-1) * (2^32-1) < 2^63. Distances that fit in an int are
unchanged, and the result is now monotonic in the offsets and independent
of the order of the arguments, which it was not before.

Adds regression tests covering both methods. The doubles overloads are
not affected.

Assisted-By: DeepSeek-V4.1 (deepseek-ai) via DeepSeek Harness
Signed-off-by: simpleqt <[email protected]>

Report URL: https://github.com/apache/commons-math/actions/runs/41708940973

With regards,
GitHub Actions via GitBox

Reply via email to