Integer overflow occurs when the result of an operation is larger than the maximal value that can be represented by the underlying integer type.
Questions tagged [integer-overflow]
1056 questions
6
votes
4 answers
Bit-shifting left and discarding bits
Let's consider the function (one of possible implementations of it) which would zero out right N bits of an unsigned short value (or any other unsigned integral type). The possible implementation could look like following:
template

SergeyA
- 61,605
- 5
- 78
- 137
6
votes
4 answers
Creating a custom int that would enforce always being in a specified range; how to get past integer overflows?
As in the title. As an exercise, I wanted to create an int that would enforce constraints on its value and would disallow being set to values outside its specified range.
Here is how I tried to approach this:
#include
#include…
user4385532
6
votes
4 answers
C++ integer overflow
I'm just starting to teach myself C++, and have begun learning about integer overflow. Out of curiosity I wrote some tests just to see what occurs with certain integer values.
Here's my program:
#include
int main()
{
int x(0);
…

Zack Downs
- 157
- 1
- 2
- 9
6
votes
2 answers
Why is checked arithmetic in .NET sometimes faster than unckecked?
Why is it when I turn on "check for arithmetic underflow/overflow" under C# Project Properties > Build > Advanced, that the following code runs faster (138 ms) than if I turn the option off (141 ms)?
Test Run #1: 138ms with checked arithmetic, 141ms…

Edward Brey
- 40,302
- 20
- 199
- 253
6
votes
3 answers
Python prevent overflow errors while handling large floating point numbers and integers
I am working on a python program to calculate numbers in the Fibonacci sequence. Here is my code:
import math
def F(n):
return ((1+math.sqrt(5))**n-(1-math.sqrt(5))**n)/(2**n*math.sqrt(5))
def fib(n):
for i in range(n):
print…

Progo
- 3,452
- 5
- 27
- 44
6
votes
3 answers
How can both (i + 1) < ii and (i + 1) > ii both be true?
I'm currently learning C program But I came across some weird behavior
I expected one result but two results is printed like this
$ ./a.out
yes1 0x80000000
yes3 0x80000000
How could possible that?
I can't understand the result.
OS : x86_64 Ubuntu…

mug896
- 1,777
- 1
- 19
- 17
6
votes
3 answers
Does the C standard make an guarantees about the relationship between INT_MIN and INT_MAX?
For example, in both 2's and 1's complement, INT_MAX*-1 is a valid number. Is this guaranteed? Are there any other guarantees? Could INT_MIN be -1 or 0 on any architecture, for example? Is there anything we can know about (INT_MAX + INT_MIN)? Can we…

Drew
- 12,578
- 11
- 58
- 98
6
votes
2 answers
What would happen if I were to make more references to Objects than 32 bits can account for?
So I just learned when you declare a variable of type Object ( i.e. Object a; ), a 32-bit space is allocated for that variable. Inside this variable/reference, there is a memory address to an actual Object.
Now let's pretend I have a large enough…

Kacy Raye
- 1,312
- 1
- 11
- 14
6
votes
1 answer
Can I detect integer overflow flaws with valgrind?
Can I detect integer overflow flaws with valgrind? and which tool in it can do that?

Syrena
- 95
- 4
6
votes
3 answers
Is overflow defined for VHDL numeric_std signed/unsigned
If I have an unsigned(MAX downto 0) containing the value 2**MAX - 1, do the VHDL (87|93|200X) standards define what happens when I increment it by one? (Or, similarly, when I decrement it by one from zero?)

detly
- 29,332
- 18
- 93
- 152
6
votes
6 answers
Can I prevent a integer overflow in C# using an unsigned right shift?
I want alwaysPositive to be assigned a positive number with all possible values for lareValue1 and largeValue2 (these are at least 1).
The following statement causes a buffer overflow:
int alwaysPositive = (largeValue1 + largeValue2) / 2;
I know I…

Paco
- 8,335
- 3
- 30
- 41
5
votes
2 answers
Why does initializing a negatively-sized array cause an overflow exception?
I was unit testing in C#, and I found the following code gives an overflow exception:
using System;
public class Program
{
public static void Main()
{
int i = 0;
Console.WriteLine(new float[i - 1]);
…

ADAMJR
- 1,880
- 1
- 14
- 34
5
votes
2 answers
128 bit (a * b) / c in Rust with phantom overflow protection
Let a, b, and c be 128 bit numbers. A phantom overflow happens when the intermediary value a * b overflows 128 bits. However the final product after division numerator / c fits inside 128 bits.
For 64 bit and smaller numbers, one can cast to 128…

secretshardul
- 1,635
- 19
- 31
5
votes
1 answer
Stopping integer overflow in ASP.NET
We use Acunetix at work to do security scans on our applications. Recently we came across the following Integer Vulnerabilities error below:
From what I can tell, it looks like the report is telling us that we are not stopping integer overflow…

ryanulit
- 4,983
- 6
- 42
- 66
5
votes
1 answer
Can `unsigned int x; scanf("%u",&x);` really cause undefined behavior?
For once I thought I found a good use for sscanf() but after reading about how it handles integers, it appears not. Having a string that should look like this: 123,456,678 I thought I could safely and concisely parse it with this code:
unsigned int…

pipe
- 657
- 10
- 27