Integer overflow: Difference between revisions

Content deleted Content added
Methods to address integer overflow problems: language)|C#]] || colspan="2" | modulo power of 2 in unchecked context; <code>System.OverflowException</code> is raised in checked context<ref>{{cite web |url=http://msdn.microsoft.com/en-us/library/khy08726.aspx |title=Checked and Unchecked (C# Reference) |last=BillWagner |website=msdn.microsoft.com|date=8 April 2023 }}</ref> |- | Java || modulo power
Tags: Reverted references removed Mobile edit Mobile web edit
Restored revision 1227558926 by Zero0000 (talk): Not sure what happened here
Line 4:
[[File: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 (1,000,000s digit) to change to a 1, so the counter resets to zero. This is ''wrapping'' in contrast to ''saturating''.]]
 
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 higher than the maximum or lower than the minimum representable value.
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 other number). On some processors like [[givegraphics processing unit]]s (GPUs) and [[digital signal processor]]s (DSPs) which support [[saturation arithmetic]], overflowed results leadingwould tobe unintended''clamped'', behaviori.e. Inset particular,to the minimum value in the representable range if the possibilityresult hasis notbelow beenthe anticipated,minimum overflowand canset compromiseto athe program'smaximum reliabilityvalue andin [[softwarethe security|security]]representable range if the result is above the maximum, rather than wrapped around.
 
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]].
 
For some applications, such as timers and clocks, wrapping on overflow can be desirable. The [[C11 (C standard revision)|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/IEC+9899:2011 |title=ISO/IEC 9899:2011 Information technology - Programming languages - C |author=ISO staff |website=ANSI.org}}</ref>
Line 38 ⟶ 40:
For an unsigned type, when the ideal result of an operation is outside the type's representable range and the returned result is obtained by wrapping, then this event is commonly defined as an overflow. In contrast, the C11 standard defines that this event is not an overflow and states "a computation involving unsigned operands can never overflow."<ref name="auto"/>
 
When the ideal result of an integer operation is outside the type's representable range and the returned result is obtained by clamping, then this event is commonly defined as a saturation. Use varies as to whether a saturation is or is not an overflow. To eliminate ambiguity, the terms wrapping overflow<ref>{{cite web |url=https://www.mathworks.com/help/simulink/gui/wrap-on-overflow.html?searchHighlight=overflow&s_tid=doc_srchtitle |title=Wrap on overflow - MATLAB & Simulink |website=www.mathworks.com}}</ref> and saturating overflow<ref>{{cite web |url=https://www.mathworks.com/help/simulink/gui/saturate-on-overflow.html?searchHighlight=overflow&s_tid=doc_srchtitle |title=Saturate on overflow - MATLAB & Simulink |website=www.mathworks.com}}</ref> can be used.
 
Many references can be found to integer underflow.<ref>{{cite web |url=https://cwe.mitre.org/data/definitions/191.html |title=CWE - CWE-191: Integer Underflow (Wrap or Wraparound) (3.1) |website=cwe.mitre.org}}</ref><ref>{{cite web |url=https://dzone.com/articles/overflow-and-underflow-data |title=Overflow And Underflow of Data Types in Java - DZone Java |website=dzone.com}}</ref><ref>{{cite web |url=https://medium.com/@taabishm2/integer-overflow-underflow-and-floating-point-imprecision-6ba869a99033 |title=Integer Overflow/Underflow and Floating Point Imprecision |last=Mir |first=Tabish |date=4 April 2017 |website=medium.com}}</ref><ref>{{cite web |url=https://www.mozilla.org/en-US/security/advisories/mfsa2015-147/ |title=Integer underflow and buffer overflow processing MP4 metadata in libstagefright |website=Mozilla}}</ref><ref>{{cite web |url=https://developer.apple.com/library/content/documentation/Security/Conceptual/SecureCodingGuide/Articles/BufferOverflows.html#//apple_ref/doc/uid/TP40002577-SW7 |title=Avoiding Buffer Overflows and Underflows |website=developer.apple.com}}</ref> When the term integer underflow is used, it means the ideal result was closer to negative infinity than the output type's representable value closest to negative infinity. Depending on context, the definition of overflow may include all types including underflows, or it may only include cases where the ideal result was closer to positive infinity than the output type's representable value closest to positive infinity.
Line 59 ⟶ 61:
| [[C (programming language)|C]], [[C++]] || modulo power of two || undefined behavior
|-
| [[C Sharp (programming language)|C#]] || colspan="2" | modulo power of 2 in unchecked context; <code>System.OverflowException</code> is raised in checked context<ref>{{cite web |url=http://msdn.microsoft.com/en-us/library/khy08726.aspx |title=Checked and Unchecked (C# Reference) |last=BillWagner |website=msdn.microsoft.com|date=8 April 2023 }}</ref>
| [[C Sharp (programming of two (char is the only unsigned primitive type in Java) || modulo power of two
|-
| [[C SharpJava (programming language)|Java]] || modulo power of two (char is the only unsigned primitive type in Java) || modulo power of two
|-
| [[JavaScript]] || colspan="2" | all numbers are [[Double-precision floating-point format|double-precision floating-point]] except the new [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt BigInt]