Integer overflow: Difference between revisions

Content deleted Content added
Line 16:
* 128 bits: maximum representable value 2<sup>128</sup> − 1 = 340,282,366,920,938,463,463,374,607,431,768,211,455
 
When an arithmetic operation produces a result larger than the maximum above, an integer overflow reduces the result to [[modulo operation|modulo]] of the maximum possible value, effectively causing a ''wrap around'' (for example, 8-bit integer addition 255 + 1 results in 0, which is {{math|256 mod 255}}, and similarly subtraction 0 - 1 results in 255, a [[two's complement]] representation of -1).
 
{{anchor|Security ramifications}}
If the variable has a [[Signed number representations|signed integer]] type, a program may make the assumption that a variable always contains a positive value. An integer overflow can cause the value to wrap and become negative, which violates the program's assumption and may lead to unintended behavior (for example, 8-bit integer addition of 127 + 1 results in -127128, a [[two's complement]] of 128 for 8-bit integers).
 
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 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.