Integer Overflow

Integer overflow is not itself a memory safety violation. It leads to one when the overflowed value is used:

  • For pointer arithmetic
  • As a malloc argument
  • As an array index

An overflowed value used this way often leads to a buffer overflow, or directly to an out-of-bounds read or write.

Integer Representation

Number Representation

Unsigned integer
An nn-bit unsigned integer x=(xn−1,…,x0)x = (x_{n-1}, \dots, x_0) is stored in binary.

x=∑i=0n−12ixix = \sum_{i=0}^{n-1} 2^i x_i

Signed integer
Stored in two’s complement. The most significant bit has a negative weight.

x=−2n−1xn−1+∑i=0n−22ixix = -2^{n-1} x_{n-1} + \sum_{i=0}^{n-2} 2^i x_i

The range is −2n−1≤x<2n−1-2^{n-1} \leq x < 2^{n-1}. Negation is −x=2n−x-x = 2^n - x.

Floating point
A single-precision IEEE 754 float uses 32 bits.

  • Sign
    1 bit, x31x_{31}.
  • Exponent
    8 bits, x30x_{30} to x23x_{23}, stored with bias 127127.
  • Fraction
    23 bits, x22x_{22} to x0x_0.
x=(−1)x31⋅(1+∑i=123x23−i2−i)⋅2[x30:x23]−127x = (-1)^{x_{31}} \cdot \left(1 + \sum_{i=1}^{23} x_{23-i} 2^{-i}\right) \cdot 2^{[x_{30}:x_{23}] - 127}

It encodes values up to about 3.4×10383.4 \times 10^{38}, all integers with at most 6 decimal digits, and all values 2n2^n for −126≤n≤127-126 \leq n \leq 127.

Examples:

  • 3.31253.3125
    Binary 11.01012=1.101012×2111.0101_2 = 1.10101_2 \times 2^1. Sign bit is 00. Exponent is 1+127=128=1000000021 + 127 = 128 = 10000000_2. Fraction is 1010110101 padded with zeros. Result is 01000000010101000000000000000000.

Overflow Behavior

Unsigned Overflow

An nn-bit unsigned integer represents xx with 0≤x<2n0 \leq x < 2^n. On overflow, C++11 requires the value be reduced modulo 2n2^n, so unsigned arithmetic never triggers undefined behaviour.

An nn-bit unsigned integer can overflow in multiple cases, for 0≤a,b<2n0 \leq a,b < 2^n:

  • Addition
    a+b≥2na+b \geq 2^n
  • Subtraction
    a−b<0a-b < 0 if b>ab > a
  • Multiplication
    a⋅b≥2na \cdot b \geq 2^n

Examples:

  • Stagefright
    uint8_t *buffer = malloc(size + chunk_size) overflows if size + chunk_size exceeds the unsigned range, allocating a buffer smaller than intended. Fixed by checking size > UINT_MAX - chunk_size before adding, rejecting the allocation instead of letting the sum wrap.

Signed Overflow

By the C++11 standard, if evaluating an expression produces a result not mathematically defined or not representable by its type, the behaviour is undefined. This applies only to signed integers.

An nn-bit signed integer can overflow in multiple cases, for −2n−1≤a,b<2n−1-2^{n-1} \leq a,b < 2^{n-1}:

  • Addition or subtraction
    a+b≥2n−1a+b \geq 2^{n-1} or a+b<−2n−1a+b < -2^{n-1}.
  • Negation
    a=−2n−1  ⟹  −a=2n−1a = -2^{n-1} \implies -a = 2^{n-1}, an asymmetry of two’s complement since 2n−12^{n-1} is not representable.
  • Multiplication
    a⋅b≥2n−1a \cdot b \geq 2^{n-1} or a⋅b<−2n−1a \cdot b < -2^{n-1}. Multiplication by −1-1 reduces to negation.
  • Division
    −2n−1−1=2n−1\frac{-2^{n-1}}{-1} = 2^{n-1}, reducing to negation.

Examples:

  • Java’s binarySearch computed mid = (low + high) / 2. For large arrays, low + high overflows a signed 32-bit int before the division, producing a negative or invalid index. Fixed by computing mid = low + (high - low) / 2 instead, since high - low stays within the array’s valid index range and cannot overflow.

Conversion and Mitigation

Type Conversion

C automatically converts between types, known as coercion, performed by the compiler and capable of unintended consequences.

  • float to int
    Truncates the fractional part.
  • double to float
    Rounds to the nearest representable value.

Converting a smaller type to a larger one uses:

  • Sign extension
    High bits set to the sign bit, used when the source is signed.
  • Zero extension
    High bits set to 0, used when the source is unsigned.

For an arithmetic operation between 2 operands, the compiler chooses a common type:

  • Same type, same rank
    No conversion.
  • Same type, different rank
    Convert the smaller type to the larger.
  • Unsigned operand with rank at least the signed operand’s rank
    Convert both to unsigned.
  • Signed type can represent every value of the unsigned type
    Convert both to signed.
  • None of the above
    Convert both to unsigned, using the signed operand’s type as the unsigned width.

Fixing Integer Overflow

Checking width * height > UINT_MAX after computing width * height is unsound, since the multiplication has already overflowed by the time the check runs.

A correct check divides instead of multiplying, guarding against division by 00:

void* new_8bit_image(unsigned int width, unsigned int height)
{
  if (!width || (UINT_MAX / width < height)) return NULL;
  unsigned int memory = width * height;
  void* data = malloc(memory);
  return data;
}

GCC and clang also provide built-in overflow-checking arithmetic, e.g. __builtin_umul_overflow, __builtin_add_overflow, __builtin_sub_overflow, for various integer types.

unsigned int memory;
if (__builtin_umul_overflow(width, height, &memory)) {
  return NULL;
}
void* data = malloc(memory);
Written by October 30, 2026 4 min read
Was this helpful?