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
10
votes
1 answer
Is a char value set to CHAR_MAX guaranteed to wrap around to CHAR_MIN?
My code:
#include
#include
int main()
{
char c = CHAR_MAX;
c += 1;
printf("CHAR_MIN=%d CHAR_MAX=%d c=%d (%c)\n", CHAR_MIN, CHAR_MAX, c, c);
}
Output:
CHAR_MIN=-128 CHAR_MAX=127 c=-128 ()
We see that when we…

Lone Learner
- 18,088
- 20
- 102
- 200
10
votes
4 answers
Is there a C snippet that computes overflow-safe addition efficiently without using compiler builtins?
Here is a C function that adds an int to another, failing if overflow would happen:
int safe_add(int *value, int delta) {
if (*value >= 0) {
if (delta > INT_MAX - *value) {
return -1;
…

Tavian Barnes
- 12,477
- 4
- 45
- 118
10
votes
2 answers
How to do a double-chunk add with no undefined behaviour?
EDIT Public health warning - this question includes a false assumption about undefined behaviour. See accepted answer.
After a reading recent blog post, I've been thinking a lot about the practicality of avoiding all standards-undefined assumptions…
user180247
10
votes
1 answer
Get numpy to warn on integer overflow
Having mostly used python, I've gotten spoiled by not having to worry about integer overflow. Now that I'm using numpy, I have to worry about it again. I would like numpy to error in cases of overflow, but it doesn't seem to work for int64.
import…

Dan
- 9,935
- 15
- 56
- 66
10
votes
3 answers
How do I return a flag on integer overflow in Rust?
Swift has integer overflow arithmetic functions which return a flag whether the number has overflowed or not. Do we have same thing in Rust?

eonil
- 83,476
- 81
- 317
- 516
10
votes
4 answers
Delphi: How to avoid EIntOverflow underflow when subtracting?
Microsoft already says, in the documentation for GetTickCount, that you could never compare tick counts to check if an interval has passed. e.g.:
Incorrect (pseudo-code):
DWORD endTime = GetTickCount + 10000; //10 s from now
...
if (GetTickCount >…

Ian Boyd
- 246,734
- 253
- 869
- 1,219
10
votes
1 answer
Convert INT_MAX to float and then back to integer.
In C programming, I find a weird problem, which counters my intuition. When I declare a integer as the INT_MAX (2147483647, defined in the limits.h) and implicitly convert it to a float value, it works fine, i.e., the float value is same with the…

houtoms
- 103
- 1
- 6
10
votes
4 answers
Why, In Java arithmetic, overflow or underflow will never throw an Exception?
During Java Arithmetic operation , JVM do not throw Underflow or Overflow Exception. So many time we come across unexpected results and wondering what went wrong.
While in case of .NET technology we have Overflow and Undeflow exception.
So my…

Alpesh Gediya
- 3,706
- 1
- 25
- 38
9
votes
4 answers
Common practice how to deal with Integer overflows?
What's the common practice to deal with Integer overflows like 999999*999999 (result > Integer.MAX_VALUE) from an Application Development Team point of view?
One could just make BigInt mandatory and prohibit the use of Integer, but is that a…

IODEV
- 1,706
- 2
- 17
- 20
9
votes
4 answers
Can a conforming compiler break uint32_t -> int16_t -> int32_t conversions?
Recently, we discovered odd behavior in some old code. This code has worked for ages, but broke on some platform (XBox 360, PowerPC) with compiler optimizations turned on max. Usually, I'd suspect undefined behavior.
Code looks roughly like…

Maister
- 4,978
- 1
- 31
- 34
9
votes
6 answers
Should I use unsigned integers for counting members?
Should I use unsigned integers for my count class members?
Answer
For example, assume a class
TList = class
private
FCount : Cardinal;
public
property Count : Cardinal read FCount;
end;
That does make sense, doesn't it? The number of items…

jpfollenius
- 16,456
- 10
- 90
- 156
9
votes
1 answer
Is signed integer overflow in safe Rust in release mode considered as undefined behavior?
Rust treats signed integer overflow differently in debug and release mode. When it happens, Rust panics in debug mode while silently performs two's complement wrapping in release mode.
As far as I know, C/C++ treats signed integer overflow as…

Zhiyao
- 4,152
- 2
- 12
- 21
9
votes
4 answers
How to do arithmetic modulo another number, without overflow?
I'm trying to implement a fast primality test for Rust's u32 and u64 datatypes. As part of it, I need to compute (n*n)%d where n and d are u32 (or u64, respectively).
While the result can easily fit in the datatype, I'm at a loss for how to compute…

Richard Rast
- 1,772
- 1
- 14
- 27
9
votes
3 answers
Why may an overflow occur in the following program?
void main () {
int i;
if (i < 0) { i = -i; };
}
Can anyone help me to understand why an overflow may occur in the above program?

Tom
- 904
- 1
- 7
- 21
9
votes
3 answers
How to check for overflow in duration_cast
I need to convert one kind of std::chrono::duration to another kind but I need to know when such a conversion is not possible because the value would not be representable.
I have not found any facilities in the standard library to check this. The…

Ambroz Bizjak
- 7,809
- 1
- 38
- 49