Programs must not allow mathematical operations to exceed the integer ranges provided by their primitive integer data types. According to the The Java Language Specification (JLS), §4.2.2, "Integer Operations" [JLS 2005]:
...
Because the ranges of Java types are not symmetric (the negation of each minimum value is one more than each maximum value), even operations such as unary negation can overflow if applied to a minimum value. Because the java.lang.math.abs()
method returns the absolute value of any number, it can also overflow if given the minimum int
or long
as an argument.
When a mathematical operation cannot be represented using the supplied integer types, Java's built-in integer operators silently wrap the result without indicating overflow. The silent wrap can result in incorrect computations and unanticipated outcomes. Failure to account for integer overflow has resulted in failures of real systems, for example, when implementing the compareTo()
method. The meaning of the return value of the compareTo()
method is defined only in terms of its sign and whether it is zero; the magnitude of the return value is irrelevant. Consequently, an apparent but incorrect optimization would be to subtract the operands and return the result. For operands of opposite signs, this approach can result in integer overflow, consequently violating the compareTo()
contract [Bloch 2008
...
].
Comparison of Compliant Techniques
...
Operations on objects of type AtomicInteger
suffer from the same overflow issues as other integer types. The solutions are generally similar to the solutions already presented; however, concurrency issues add additional complications. First, potential issues with time-of-check, time-of-use (TOCTOU) must be avoided (see rule VNA02-J. Ensure that compound operations on shared variables are atomic for more information). Second, use of an AtomicInteger
creates happens-before relationships between the various threads that access it. Consequently, changes to the number of accesses or order of accesses can alter the execution of the overall program. In such cases, you must either choose to accept the altered execution or carefully craft your implementation to preserve the exact number of accesses and order of accesses to the AtomicInteger
.
...
The two arguments to the compareAndSet()
method are the expected value of the variable when the method is invoked and the intended new value. The variable's value is updated only when the current value and the expected value are equal [API 2006] (refer to rule VNA02-J. Ensure that compound operations on shared variables are atomic for more details).
...
NUM00-EX1: Prevention of integer overflow is unnecessary for numeric fields that undergo bitwise operations and not arithmetic operations (see rule NUM01-J. Do not perform bitwise and arithmetic operations on the same data for more information).
...
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 |
...
[API 2006] | Class |
Puzzle 27, "Shifty i's" | |
[Bloch 2008] | Item 12, "Minimize the Accessibility of Classes and Members" |
[JLS 2005] | §4.2.2, "Integer Operations" |
Chapter 5, "Integers" | |
[Seacord 2015] | IDS17-J. Prevent XML External Entity Attacks LiveLesson |
Primitive Data Types |
...