Integer overflow: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 4:
[[Image:Odometer rollover.jpg|thumb|250px|Integer overflow can be demonstrated through an [[odometer]] overflowing, a mechanical version of the phenomenon. All digits are set to the maximum 9 and the next increment of the white digit causes a cascade of carry-over additions setting all digits to 0, but there is no higher digit to change to a 1, so the counter resets to zero. This is ''wrapping'' in contrast to ''saturating''.]]
 
ow can compromise a program's reliability and [[software security|security]].
In [[computer programming]], an '''integer overflow''' occurs when an [[arithmetic]] operation attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either larger than the maximum or lower than the minimum representable value.
 
InFor some applications, such as timers and clocks, wrapping on overflow can be desirable. The [[C (programming language)|C11]] standard states that for unsigned integers modulo wrapping is the defined behavior and the term overflow never applies: "a computation involving unsigned operands can never overflow."<ref name="auto">{{cite web|url=https://webstore.ansi.org/RecordDetail.aspx?sku=ISO/IECIn [[computer programming]], an '''integer overflow''' occurs when an [[arithmetic]] operation attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either larger than the maximum or lower than the minimum representable value.
The most common result of an overflow is that the least significant representable digits of the result are stored; the result is said to ''wrap'' around the maximum (i.e. [[Modular arithmetic|modulo]] a power of the [[radix]], usually two in modern computers, but sometimes ten or another radix).
 
The most common result of an overflow is that the least significant representable digits of the result are stored; the result is said to ''wrap'' around the maximum (i.e. [[Modular arithmetic|modulo]] a power of the [[radix]], usually two in modern computers, but sometimes ten or another radix).
An overflow condition may give results leading to unintended behavior. In particular, if the possibility has not been anticipated, overflow can compromise a program's reliability and [[software security|security]].
 
ForAn someoverflow applications,condition suchmay asgive timersresults andleading clocks,to wrappingunintended on overflow can be desirablebehavior. TheIn [[Cparticular, (programming language)|C11]] standard states that for unsigned integers modulo wrapping isif the definedpossibility behaviorhas andnot thebeen termanticipated, overflow never applies: "a computation involving unsigned operands can never overflow."<ref name="auto">{{cite web|url=https://webstore.ansi.org/RecordDetail.aspx?sku=ISO/IECoverfl+9899:2011&msclkid=2f0af3a2b5ca143c9285a9f8e8f6b3e1|title=ISO/IEC 9899:2011 Information technology - Programming languages - C|first=|last=ISO|date=|website=webstore.ansi.org}}</re>
 
On some processors like [[graphics processing unit]]s (GPUs) and [[digital signal processor]]s (DSPs) which support [[saturation arithmetic]], overflowed results would be "clamped", i.e. set to the minimum or the maximum value in the representable range, rather than wrapped around.