Integer overflow occurs when the result of an operation is larger than the maximal value that can be represented by the underlying integer type.
242
votes
29answers
93k views
Best way to detect integer overflow in C/C++
I was writing a program in C++ to find all solutions of ab = c, where a, b and c together use all the digits 0-9 exactly once. The program looped over values of a and b, and ran a digit-counting ...
23
votes
8answers
2k views
Why don't languages raise errors on integer overflow by default?
In several modern programming languages (including C++, Java, and C#), the language allows integer overflow to occur at runtime without raising any kind of error condition.
For example, consider this ...
7
votes
6answers
6k views
Force PHP integer overflow
We have some integer arithmetic which for historical reasons has to work the same on PHP as it does in a few statically typed languages. Since we last upgraded PHP the behavior for overflowing ...
16
votes
3answers
3k views
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 ...
10
votes
9answers
9k views
How to handle arbitrarily large integers
I'm working on a programming language, and today I got the point where I could compile the factorial function(recursive), however due to the maximum size of an integer the largest I can get is ...
10
votes
6answers
4k views
How do I get real integer overflows in MATLAB/Octave?
I'm working on a verification-tool for some VHDL-Code in MATLAB/Octave. Therefore I need data types which generate "real" overflows:
intmax('int32') + 1
ans = -2147483648
Later on, it would be ...
9
votes
3answers
5k views
Question about C behaviour for unsigned integer underflow
I have read in many places that 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 ...
5
votes
5answers
6k views
Is the size of an array constrained by the upper limit of int (2147483647)?
I'm doing some Project Euler exercises and I've run into a scenario where I have want arrays which are larger than 2,147,483,647 (the upper limit of int in C#).
Sure these are large arrays, but for ...
8
votes
4answers
5k views
Checking for underflow/overflow in C++?
Is there a general way to check for an overflow or an underflow of a given data type (uint32, int etc.)?
I am doing something like this:
uint32 a,b,c;
... //initialize a,b,c
if(b < c) {
a -= ...
48
votes
4answers
3k views
Why is unsigned integer overflow defined behavior but signed integer overflow isn't?
Unsigned integer overflow is well defined by both the C and C++ standards. For example, the C99 standard (§6.2.5/9) states
A computation involving unsigned operands can never overflow,
because ...
20
votes
3answers
8k views
What happens when auto_increment on integer column reaches the max_value in databases?
I am implementing a database application and I will use both JavaDB and MySQL as database. I have an ID column in my tables that has integer as type and I use the databases auto_increment-function for ...
14
votes
3answers
2k views
Is signed integer overflow still undefined behavior in C++?
As we know, signed integer overflow is undefined behavior. But there is something interesting in C++11 cstdint documentation:
signed integer type with width of exactly 8, 16, 32 and 64 bits ...
15
votes
3answers
1k views
How disastrous is integer overflow in C++?
I was just wondering how disastrous integer overflow really is. Take the following example program:
#include <iostream>
int main()
{
int a = 46341;
int b = a * a;
std::cout ...
9
votes
3answers
5k views
What does BigInteger having no limit mean?
I looked into this stackoverflow question relating to Big Integer and specifically I do not understand this line (the words in italics):
In the BigInteger class, I have no limits and there are ...
4
votes
2answers
220 views
Why do integer datatypes overflow silently rather than throwing exception
I have learnt(atleast in java) that integer/long values overflow silently and their values start over from minimum value on overflow rather than throwing any exception.
I was using an external api ...
165
votes
36answers
25k views
Unexpected results when working with very big integers on interpreted languages
I am trying to get the sum of 1 + 2 + ... + 1000000000, but I'm getting funny results in PHP and Node.js.
PHP
$sum = 0;
for($i = 0; $i <= 1000000000 ; $i++) {
$sum += $i;
}
printf("%s", ...
3
votes
2answers
1k views
BCrypt says long, similar passwords are equivalent - problem with me, the gem, or the field of cryptography?
I've been experimenting with BCrypt, and found the following. If it matters, I'm running ruby 1.9.2dev (2010-04-30 trunk 27557) [i686-linux]
require 'bcrypt' # bcrypt-ruby gem, version 2.1.2
...
3
votes
8answers
3k views
C++ Template for safe integer casts
I am trying to write a C++ template function that will throw a runtime exception on integer overflow in casts between different integral types, with different widths, and possible signed/unsigned ...
2
votes
5answers
8k views
Modular Exponentiation for high numbers in C++
So I've been working recently on an implementation of the Miller-Rabin primality test. I am limiting it to a scope of all 32-bit numbers, because this is a just-for-fun project that I am doing to ...
5
votes
1answer
547 views
Overflow when multiplying Integers and assigning to Long
If I type the following into the immediate window I get Runtime error '6': Overflow.
MsgBox 24 * 60 * 60
Why is this?
This also fails:
Dim giveTime As Long
giveTime = 24 * 60 * 60
Why is ...
37
votes
9answers
6k views
Detecting signed overflow in C/C++
At first glance, this question may seem like a duplicate of http://stackoverflow.com/questions/199333/best-way-to-detect-integer-overflow-in-c-c, however it is actually significantly different.
I've ...
6
votes
4answers
2k views
C integer overflow behaviour when assigning to larger-width integers
If I execute the following code in C:
#include <stdint.h>
uint16_t a = 4000;
uint16_t b = 8000;
int32_t c = a - b;
printf("%d", c);
It correctly prints '-4000' as the result. However, I'm ...
10
votes
9answers
3k views
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?
7
votes
3answers
809 views
Clojure - Calculate with big numbers
I want to calculate !1000 in clojure, how can I do this without getting a integer-overflow exception?
My factorial code is right now: (reduce * (range 1 1001)).
7
votes
5answers
18k views
Warning : overflow in implicit constant conversion
In the following program, the line 5 does give overflow warning as expected, but surprisingly the line 4 doesn't give any warning in GCC: http://www.ideone.com/U0BXn
int main()
{
int i = 256;
...
2
votes
7answers
2k views
Allowing signed integer overflows in C/C++
I want signed integers to overflow when they become too big. How do I achieve that without using the next biggest datatype (or when I am already at int128_t)?
For example using 8bit integers 19*12 is ...
65
votes
5answers
3k views
Is this a JVM bug or “expected behavior”?
I noticed some unexpected behavior (unexpected relative to my personal expectations), and I'm wondering if something if there is a bug in the JVM or if perhaps this is a fringe case where I don't ...
10
votes
6answers
1k views
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 <T> = class
private
FCount : Cardinal;
public
property Count : Cardinal read FCount;
...
10
votes
5answers
4k views
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 ...
15
votes
1answer
410 views
Can a non-empty string have a hashcode of zero?
By "non-empty", I mean in this question a string which contains at least one non-zero character.
For reference, here's the hashCode implementation :
1493 public int hashCode() {
1494 int h ...
11
votes
3answers
448 views
Why doesn't compound assignment in Java catch overflow problems?
To my shock, it turns out that the following code will compile without even warnings:
public void test()
{
int value = 2000000000;
long increment = 1000000000;
value += increment;
}
...
7
votes
4answers
2k views
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 = ...
4
votes
1answer
2k views
Delphi: How do i use $OVERFLOWCHECKS OFF to disable overflow checks?
i have bit of code that causes an underflow:
var
t1, t2, delta: DWORD:
begin
t1 := 0xffffff00;
t2 := 0x00000037;
delta := (t2 - t1);
The subtraction itself does generate an overflow ...
15
votes
5answers
6k views
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?
14
votes
5answers
2k views
detecting multiplication of uint64_t integers overflow with C
Is there any efficient and portable way to check when multiplication operations with int64_t or uint64_t operands overflow in C?
For instance, for addition of uint64_t I can do:
if (UINT64_MAX - a ...
7
votes
1answer
175 views
Metafunction to compute x^n and return the integer limit without overflow if not possible?
Consider the following code:
template <std::intmax_t Base, std::intmax_t Exponent>
struct integer_power_bounded
{
static_assert(Exponent >= 0, "Error in \"integer_power_bounded\": ...
6
votes
10answers
4k views
Java multiply operation behavior
I wrote a method to convert a given number from days to milliseconds:
private long expireTimeInMilliseconds;
...
public void setExpireTimeInDays(int expireTimeInDays)
{
expireTimeInMilliseconds = ...
5
votes
2answers
147 views
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 ...
5
votes
3answers
2k views
Incrementing an integer value beyond its integer limit - C#
I've a for loop which keeps incrementing an integer value till the loop completes. So if the limit n is a double variable and the incremented variable 'i' is an integer, i gets incremented beyond its ...
4
votes
5answers
189 views
Is it undefined behavior if the intermediate result of an expression overflows?
This question is a result of another SO question.
Example Code
#include <iostream>
int main()
{
unsigned long b = 35000000;
int i = 100;
int j = 30000000;
unsigned long n = ( ...
2
votes
1answer
3k views
double precision integer subtraction with 32-bit registers(MIPS)
I am learning computer arithmetic. The book I use(Patterson and Hennessey) lists the below question.
Write mips code to conduct double
precision integer subtraction for
64-bit data. Assume the ...
1
vote
2answers
166 views
VB6 how to get C-like integer overflow
I want to port this simple hash algorithm to VB6.
I have come up with:
Public Function simpleHash(hashString As String) As Long
Dim hash As Long
Dim c As Long
Dim i As Integer
On Local Error ...
15
votes
3answers
646 views
Integer Overflow - Why not [duplicate]
Possible Duplicate:
Addition of two chars produces int
Given the following C++ code:
unsigned char a = 200;
unsigned char b = 100;
unsigned char c = (a + b) / 2;
The output is 150 as ...
7
votes
4answers
534 views
Permutation with Repetition: Avoiding Overflow
Background:
Given n balls such that:
'a' balls are of colour GREEN
'b' balls are of colour BLUE
'c' balls are of colour RED
...
(of course a + b + c + ... = n)
The number of permutations in which ...
4
votes
2answers
286 views
How to calculate (n!)%1000000009
I need to find n!%1000000009.
n is of type 2^k for k in range 1 to 20.
The function I'm using is:
#define llu unsigned long long
#define MOD 1000000009
llu mulmod(llu a,llu b) // This function ...
3
votes
2answers
195 views
Do C99 signed integer types defined in stdint.h exhibit well-defined behaviour in case of an overflow?
All operations on "standard" signed integer types in C (short, int, long, etc) exhibit undefined behaviour if they yield a result outside of the [TYPE_MIN, TYPE_MAX] interval (where TYPE_MIN, TYPE_MAX ...
3
votes
4answers
1k views
java arithmetic
why this code returns wrong value?
int i=Integer.MAX_VALUE+1;
long l=Integer.MAX_VALUE+1;
System.out.println(l);
System.out.println(i);
2
votes
1answer
60 views
Precision loss when solving nonlinear equations with long integer parameters by mpreal.h
I have a numerical computation problem which requires solving nonlinear equations (with long integers) in multiple precision.
I tried an MPFR C++ wrapper from this link by Pavel:
mpfr C++ wrapper by ...
2
votes
2answers
117 views
Is INT_MIN subtracted from any integer considered undefined behavior?
What if I have something like this:
int a = 20;
int min = INT_MIN;
if(-a - min)
//do something
Assume that INT_MIN if positive is more than INT_MAX. Would min ever be converted by the compiler to ...
2
votes
4answers
232 views
Overflow issues when implementing math formulas
I heard that, when computing mean value, start+(end-start)/2 differs from (start+end)/2 because the latter can cause overflow. I do not quite understand why this second one can cause overflow while ...