Integer overflow: Difference between revisions

Content deleted Content added
Origin: signed integer arithmetic for two's complement representation does not really differ from unsigned arithmetic
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 for a N-bit integer, an integer overflow causesreduces the expected numberresult to be reduced [[Modulomodulo operation|modulo 2<sup>N</sup>]] (of the maximum possible value, effectively causing a ''wrap around'' (for example, 255 + 1 results in 0, which is {{math|256 mod 255}}).
 
{{anchor|Security ramifications}}
In some situations, a program may make the assumption that a variable always contains a positive value. If the variable has a [[Signed number representations|signed integer]] type, oftena representedprogram withmay make the assumption that a [[two'svariable complement]]always ofcontains thea unsignedpositive integer,value. anAn integer overflow can cause itsthe value to wrap and become negative., This overflowwhich violates the program's assumption and may lead to unintended behavior. (for example, 127 + 1 results in -127, a [[two's complement]] of 128).

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.
 
==Methods to mitigate integer overflow problems==