Programs must not allow mathematical operations to exceed the integer ranges provided by their primitive integer data types. According to the Java Language Specification (JLS), §4.2.2, "Integer Operations," [JLS 2005]:
The built-in integer operators do not indicate overflow or underflow in any way. Integer operators can throw a
NullPointerException
if unboxing conversion of anull
reference is required. Other than that, the only integer operators that can throw an exception are the integer divide operator/
and the integer remainder operator%
, which throw anArithmeticException
if the right-hand operand is zero, and the increment and decrement operators ++ and -- which can throw anOutOfMemoryError
if boxing conversion is required and there is insufficient memory to perform the conversion.
The integral types in Java, representation, and inclusive ranges are shown in the following table taken from the JLS, §4.2.1, "Integral Types and Values" [JLS 2005]:
Type | Representation | Inclusive Range |
---|---|---|
| 8-bit signed two's-complement | -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 |
| 64-bit signed two's-complement | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| 16-bit unsigned integers representing UTF-16 code units | |
...
INT32-C. Ensure that operations on signed integers do not result in overflow | ||||
INT32-CPP. Ensure that operations on signed integers do not result in overflow | ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b4536e596ef989b7-068ac03f-4fcc4c62-9a60b762-b69f0d4f964324224b8ce3cc"><ac:plain-text-body><![CDATA[ | [ISO/IEC TR 24772:2010 | http://www.aitcnet.org/isai/] | Wrap-around Error [XYY] | ]]></ac:plain-text-body></ac:structured-macro> |
CWE-682. Incorrect Calculationcalculation | ||||
| CWE-190. Integer Overflow overflow or Wraparoundwraparound | |||
| CWE-191. Integer Underflow underflow (Wrap wrap or Wraparoundwraparound) |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="59da394ba8a8869d-fcd7942d-4a4747cf-a183a76b-f58aedf71e0c605ae44b562d"><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="91cb163cb0f6e36a-13e81f43-40464cf8-ba86a07d-51cf2dbb8792ae50386bbb8b"><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="67a0504d89719591-d3d30665-4f6a4ced-99b89beb-e8c18a06a3bc70a4c9233df3"><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="b4609adc510089fc-14f4d7be-490a4f03-974d89ac-3ab1fdcdeff41f80810d1f7c"><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="8f3d4a7d0d092b61-8032b8b9-4c7a4a15-a99b84ae-e158b393addae66e654ec894"><ac:plain-text-body><![CDATA[ | [[Tutorials 2008 | AA. Bibliography#Tutorials 08]] | Primitive Data Types | ]]></ac:plain-text-body></ac:structured-macro> |
...