...
Wiki Markup |
---|
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. This 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 can result in integer overflow, consequently violating the {{compareTo()}} contract \[[Bloch 2008|AA. Bibliography#BlochReferences#Bloch 08], Item 12\]. |
Comparison of Compliant Techniques
...
Wiki Markup |
---|
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|AA. Bibliography#APIReferences#API 06]\]. Refer to rule [VNA02-J. Ensure that compound operations on shared variables are atomic] for more details. |
...
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="3acbcf3678c44bad-8ae44e46-42df49fb-a600b6b6-6f6b6fe1842adbd807a794bf"><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 calculation | ||||
| CWE-190. Integer overflow or wraparound | |||
| CWE-191. Integer underflow (wrap or wraparound) |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bb841e348ccb0b88-c4199b34-460b4e31-ae40b27e-7810c0f1adb4a7968c72521d"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API References#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="c3629d9be5ee4317-efc913b7-43974585-9f6f890b-0835e4b20033a5d9490106c7"><ac:plain-text-body><![CDATA[ | [[Bloch 2005 | AA. Bibliography#Bloch References#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="e094f272d56dbac8-682076c8-496c4dbb-b505b3a1-023a9595947be8f3c18b0264"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS References#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="fafa12fcd6bb349c-cab8c94f-4bc44338-8018a394-7558f89a9c206cd2c1f5bef2"><ac:plain-text-body><![CDATA[ | [[Seacord 2005 | AA. Bibliography#Seacord References#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="abe8ad42e1208515-520d21ad-49d74fd3-9d65af4d-9426f73f01214096627fcf3c"><ac:plain-text-body><![CDATA[ | [[Tutorials 2008 | AA. Bibliography#Tutorials References#Tutorials 08]] | Primitive Data Types | ]]></ac:plain-text-body></ac:structured-macro> |
...