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
23
votes
3 answers
Addition of two chars produces int
I've made a simple program and compiled it with GCC 4.4/4.5 as follows:
int main ()
{
char u = 10;
char x = 'x';
char i = u + x;
return 0;
}
g++ -c -Wconversion a.cpp
And I've got the following:
a.cpp: In function ‘int main()’:
a.cpp:5:16:…

Rom098
- 2,445
- 4
- 35
- 52
23
votes
5 answers
How is integer overflow exploitable?
Does anyone have a detailed explanation on how integers can be exploited? I have been reading a lot about the concept, and I understand what an it is, and I understand buffer overflows, but I dont understand how one could modify memory reliably, or…

wuntee
- 12,170
- 26
- 77
- 106
23
votes
3 answers
Question about C behaviour for unsigned integer underflow
I have read in many places that unsigned integer overflow is well-defined in C unlike the signed counterpart.
Is underflow the same?
For example:
unsigned int x = -1; // Does x == UINT_MAX?
Thanks.
I can't recall where, but i read somewhere that…

snap
- 711
- 3
- 11
- 25
23
votes
7 answers
why left+(right-left)/2 will not overflow?
In this article: http://googleresearch.blogspot.sg/2006/06/extra-extra-read-all-about-it-nearly.html, it mentioned most quick sort algorithm had a bug (left+right)/2, and it pointed out that the solution was using left+(right-left)/2 instead of…

sinsin
- 381
- 1
- 4
- 13
22
votes
3 answers
How disastrous is integer overflow in C++?
I was just wondering how disastrous integer overflow really is. Take the following example program:
#include
int main()
{
int a = 46341;
int b = a * a;
std::cout << "hello world\n";
}
Since a * a overflows on 32 bit…

fredoverflow
- 256,549
- 94
- 388
- 662
21
votes
3 answers
Overflowing of Unsigned Int
What will the unsigned int contain when I overflow it? To be specific, I want to do a multiplication with two unsigned ints: what will be in the unsigned int after the multiplication is finished?
unsigned int someint = 253473829*13482018273;

GILGAMESH
- 1,816
- 3
- 23
- 33
21
votes
2 answers
Detect signed int overflow in Go
I'm building a Lisp, and I want 32 bit integers to automatically switch to 64 bit integers if a computation would cause them to otherwise overflow. And likewise, for 64 bit overflows, switch to arbitrarily sized integers.
The problem I have is that…

d11wtq
- 34,788
- 19
- 120
- 195
21
votes
8 answers
What is the right way to find the average of two values?
I recently learned that integer overflow is an undefined behavior in C (side question - is it also UB in C++?)
Often in C programming you need to find the average of two values a and b. However doing (a+b)/2 can result in overflow and undefined…

bodacydo
- 75,521
- 93
- 229
- 319
21
votes
6 answers
Why don't C and C++ have built in ways to check for integer overflows?
Why do C and C++ not provide a set of implementation provided operations to perform each of the basic integer operations with overflow checking provided (e.g. a bool safeAdd(int *out, int a, int b)).
As I understand it, most instruction sets have…

Fire Lancer
- 29,364
- 31
- 116
- 182
20
votes
2 answers
In Rust, Why does integer overflow sometimes cause compilation error or runtime error?
fn main() {
let num: u8 = 255;
let num2: u8 = num + 1;
println!("{}, {}", num, num2);
}
When $ cargo build --release, this code doesn't make compile error.
And $ cargo run, make runtime error.
thread 'main' panicked at 'attempt to add…

Junhee
- 203
- 1
- 5
20
votes
4 answers
How to compare two nano time values? [javadoc confusion]
I have read the javadoc for System.nanoTime() and it all seems clear. Until I reached the final paragraph:
To compare two nanoTime values
long t0 = System.nanoTime();
...
long t1 = System.nanoTime();
one should use t1 - t0 < 0, not t1 < t0, because…

Evgeni
- 331
- 2
- 6
20
votes
3 answers
Can doubles or BigDecimal overflow?
Java 8 gave us Math.addExact() for integers but not decimals.
Is it possible for double and BigDecimal to overflow? Judging by Double.MAX_VALUE and How to get biggest BigDecimal value I'd say the answer is yes.
As such, why don't we have…

Gili
- 86,244
- 97
- 390
- 689
20
votes
7 answers
Avoid overflow when adding numpy arrays
I want to add numpy arrays with datatyp uint8. I know that the values in these arrays may be large enough for an overflow to happen. So I get something like:
a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
a…

Thomas
- 1,277
- 1
- 12
- 20
20
votes
6 answers
How do I detect overflow while multiplying two 2's complement integers?
I want to multiply two numbers, and detect if there was an overflow. What is the simplest way to do that?

Lazer
- 90,700
- 113
- 281
- 364
20
votes
10 answers
What is an integer overflow error?
What is an integer overflow error?
Why do i care about such an error?
What are some methods of avoiding or preventing it?

David
- 14,569
- 34
- 78
- 107