How Should You Write a Fast Integer Overflow Check? 12 years ago
From `The CERT Oracle Secure Coding Standard for Java`
public static int safeAdd(int l, int r) throws OverflowException {
if (r > 0
? l > Integer.MAX_VALUE - r
: l < Integer.MIN_VALUE - r)
throw new ArithmeticException(String.format(
"Integer overflow: %d + %d", l, r));
return l + r;
}https://www.securecoding.cert.org/confluence/display/java/NU...