jnr / jnr-ffi

Java Abstracted Foreign Function Layer
Other
1.23k stars 154 forks source link

Fix integer overflow on bounds check, raise an ArithmeticException if… #306

Closed jsorel closed 1 year ago

jsorel commented 2 years ago

Fixing an undetected integer overflow error causing a misleading ArrayOutOfBounds exception.

Operation : len * Double.SIZE / 8 is made in integer precision executed from left to right ( (len* Double.SIZE) / 8) The first part may overflow on large integer. On way to reduce the errors would have been to write it : len * (Double.SIZE / 8)

To be warned if an overflow happens I've choose : Math.multiplyExact(len, Double.SIZE / 8)

headius commented 1 year ago

Nice use of the "exact" math functions. Thanks!