Java silently wraps, providing no indication of overflow conditionsbuilt-in integer operators silently wrap without indicating overflow. This can result in incorrect computations and unanticipated outcomes.
...
The integral types in Java are byte
, short
representation, int
, and long
, whose values are 8-bit, 16-bit, 32-bit and 64and inclusive ranges are shown in the following table JLS §4.2.1, "Integral Types and Values":
Type | Representation | Inclusive Range |
---|---|---|
| 8-bit signed two's-complement |
...
- |
According to the JLS, §4.2.1, "Integral Types and Values" the values of the integral types are integers in the inclusive ranges, shown in the following table:
Type | Inclusive Range | |
---|---|---|
| -128 to 127 | |
| 16-bit signed two's-complement | -32,768 to 32,767 |
| 32-bit signed two's-complement | -2,147,483,648 to 2,147,483,647 ,647 |
| 64-bit signed two's-complement | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| 16-bit unsigned unsigned integers representing UTF-16 code units | |
...
Operator | Overflow |
| Operator | Overflow |
| Operator | Overflow |
| Operator | Overflow |
---|---|---|---|---|---|---|---|---|---|---|
| yes |
| | yes |
| | no |
| | no |
| yes |
| | yes |
| | no |
| | no |
| yes |
| | yes |
| | no |
| | no |
| yes |
| | no |
| \ | no |
| | no |
| no |
| | no |
| | no |
| | no |
| yes |
| | no |
| | no |
| | no |
| yes |
| | no |
| | no |
| ||
| no |
| | no |
| unary | no | |||
| yes |
| | no |
| unary | yes |
|
Because the ranges of Java types are not symmetrical (the negation of minimum value is one more than each maximum value), even operations like unary negation can overflow, if applied to a minimum value. Because the java.lang.math.abs()
function returns the absolute value on any number, it can also overflow if given the minimum int
or long
as an argument.
...
The pre-condition testing technique requires different pre-condition tests for each arithmetic operation. This can be somewhat more difficult to implement and to understand audit than either of the other two approaches.
...
The BigInteger
technique is conceptually the simplest of the three techniques because arithmetic operations cannot overflow. However, it requires the use of method calls for each operation in place of primitive arithmetic operators; this can obscure the intended meaning of the code. Operations on objects of type BigInteger
can also be significantly less efficient than operations on the original primitive integer type.
...
Operations on objects of type AtomicInteger
suffer from the same overflow issues as other integer types. The solutions are generally similar to those shown abovethe solutions already presented; however, concurrency issues add additional complications. First, potential issues with time-of-check-time-of-use must be avoided. (See guideline "VNA02-J. Ensure that compound operations on shared variables are atomic" for more information). SecondlySecond, use of an AtomicInteger
creates happens-before relationships between the various threads that access it. Consequently, changes to the number 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 the implementation of your compliant technique to preserve the exact number and order of accesses to the AtomicInteger
.
...
Consequently, itemsInInventory
can wrap around to Integer.MIN_VALUE
after the increment operation when itemInInventory == Integer.MAX_VALUE
.
Compliant Solution (AtomicInteger
)
...
Wiki Markup |
---|
The 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 if, and only if, the current value and the expected value are equal. (See \[[API 2006|AA. Bibliography#API 06]\] class [{{AtomicInteger}}|http://download.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicInteger.html].) Refer to guideline "[VNA02-J. Ensure that compound operations on shared variables are atomic]" for more details. |
Exceptions
NUM00-EX1EX0: Depending on circumstances, integer overflow could be benign. For instance, the Object.hashcode()
method could return all representable values of type int
. Furthermore, many algorithms for computing hashcodes intentionally allow overflow to occur.
NUM00-EX2EX1: The added complexity and cost of programmer-written overflow checks could exceed their value for all but the most critical code. In such cases, consider the alternative of treating integral values as though they are tainted data, and using appropriate range checks as to sanitize the sanitizing codevalues. These range checks should ensure that incoming values cannot cause integer overflow. Note that sound determination of allowable ranges could require deep understanding of the details of the code protected by the range checks; correct determination of the allowable ranges could be extremely difficult.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cbde3fa899b5d465-43023eaf-46ff4ef4-b7adb1f6-a1506dc739eee09ede41a218"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | class [ | http://download.oracle.com/javase/6/docs/api/java/util/concurrent/atomic/AtomicInteger.html] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6afef1ee3625570a-736ce291-44a64bb6-bf5989cd-b11a7f87679e757087a7623f"><ac:plain-text-body><![CDATA[ | [[Bloch 2005 | AA. Bibliography#Bloch 05]] | Puzzle 27: Shifty i's | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7a971cb7031f26ad-52662197-47424220-8fa8862e-922efc2ca45f3e7612913bcc"><ac:plain-text-body><![CDATA[ | [[SCG 2007 | AA. Bibliography#SCG 07]] | Introduction | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="050241096a4c1b44-89e53200-4a6740af-ad01a85f-0b64e1113cd675e6a5c23ce0"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [§4.2.2, "Integer Operations" | http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.2] | ]]></ac:plain-text-body></ac:structured-macro> |
| |||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="647da19002affa15-88ddbeb9-46564f6f-b2c6a000-e3cb26cfa7e0045a78bb1330"><ac:plain-text-body><![CDATA[ | [[Seacord 2005 | AA. Bibliography#Seacord 05]] | Chapter 5. Integers | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bfa059a49f66881c-6afe5687-40ba478a-a83b9d8e-e87fabd00ccc2b031aa1b6c9"><ac:plain-text-body><![CDATA[ | [[Tutorials 2008 | AA. Bibliography#Tutorials 08]] | Primitive Data Types | ]]></ac:plain-text-body></ac:structured-macro> |
...