Content deleted Content added
→Origin: Maximum representable numbers for 32- and 64-bit were incorrectly halved, which would only be true if we were assuming signed values (which that paragraph is not). |
Hans Bauer (talk | contribs) Add table about overflow handling |
||
Line 20:
==Security ramifications==
{| class="wikitable" style="float:right; margin-left:1em; margin-right:0; width:30%;"
|+ Integer overflow handling in various programming languages
|-
! Language
! Unsigned integer
! Signed integer
|-
| [[Ada (programming language)|Ada]] || colspan="2" | <tt>'''raise''' NUMERIC_ERROR</tt>
|-
| [[C (programming language)|C]] || modulo power of two || undefined behavior
|-
| [[C++]] || modulo power of two || undefined behavior
|-
| [[C Sharp (programming language)|C#]] || colspan="2" | ignored until the calculation is checked
|-
| [[Java (programming language)|Java]] || NA || ignored
|-
| [[Python (programming language)|Python]] || NA || convert to long
|-
| [[Seed7]] || NA || <tt>'''raise''' OVERFLOW_ERROR</tt>
|-
| [[Swift (Apple programming language)|Swift]] || colspan="2" | causes an error until overflow operators are used
|}
In some situations, a program may make the assumption that a variable always contains a positive value. If the variable has a signed integer type, an overflow can cause its value to wrap and become negative. This overflow violates the program's assumption and may lead to unintended behavior. Similarly, subtracting from a small unsigned value may cause it to wrap to a large positive value which may also be an unexpected behavior. Multiplying or adding two integers may result in a value that is non-negative, but unexpectedly small. If this number is used as the number of bytes to allocate for a buffer, the buffer will be allocated unexpectedly small, leading to a potential buffer overflow.
Some languages, such as [[Ada (programming language)|Ada]], [[Seed7]] and [[Swift (Apple programming language)|Swift]] (and certain variants of functional languages), provide mechanisms to make accidental overflows trigger an exception condition. In contrast, [[Python (programming language)|Python]] seamlessly converts a number that becomes too large for an integer to a long.<ref>[http://www.python.org/doc/1.4/ref/ref5.html Python documentation], section 5.1 Arithmetic conversions.</ref> (This occurred in Python 2.4.)<ref>[http://www.python.org/dev/peps/pep-0237/ Python Enhancement Proposal 237]</ref>
==Techniques for mitigating integer overflow problems==
List of techniques and methods that might be used to mitigate the consequences of integer overflow:
|