...
Code Block | ||
---|---|---|
| ||
public static int multAccum(int oldAcc, int newVal, int scale) throws ArithmeticException { return safeAdd(oldAcc, safeMultiply(newVal, scale)); } |
Compliant Solution (Java 8, Math.*
...
Exact()
)
This compliant solution uses the addExact()
and multiplyExact()
methods defined in the Math
class. These methods were added to Java as part of the Java 8 release, and they also either return a mathematically correct value or throw ArithmeticException
. The Math
class also provides SubtractExact()
and negateExact()
, but does not provide any methods for safe division or absolute value.
...
INT32-C. Ensure that operations on signed integers do not result in overflow | |
VOID INT32-CPP. Ensure that operations on signed integers do not result in overflow | |
Wrap-around Error [XYY] | |
CWE-682. Incorrect calculation | |
| CWE-190. Integer overflow or wraparound |
| CWE-191. Integer underflow (wrap or wraparound) |
...
[API 2006] | Class |
Puzzle 27. Shifty i's | |
[JLS 2005] | |
| |
Chapter 5, Integers | |
Primitive Data Types |
...
Rule 03. : Numeric Types and Operations (NUM) Rule 03. : Numeric Types and Operations (NUM)