Integer overflow occurs when a calculation produces a value outside the range that a fixed-size integer type can represent. Depending on the language, the result wraps around, saturates, raises an error or is undefined behavior.
Why it matters
A signed 32-bit integer holds values up to 2,147,483,647. Adding 1 to that maximum wraps to -2,147,483,648 in many languages. Overflow causes real bugs and vulnerabilities: wrong totals, negative sizes passed to memory allocation, and bypassed length checks. The Year 2038 problem is an example, since signed 32-bit Unix timestamps run out in January 2038. Python and JavaScript's BigInt use arbitrary precision for integers, while JavaScript's ordinary numbers are floating point and lose exactness above 2^53.
Example
#include <stdint.h>
int32_t x = INT32_MAX; // 2147483647
x = x + 1; // signed overflow: undefined behavior in C
uint8_t b = 255;
b = b + 1; // unsigned wraps to 0
Common mistakes
- Checking for overflow after it already happened, in code that the compiler may optimize away.
- Using a 32-bit type for IDs or byte counts that can grow large.
- Multiplying two sizes without checking before using the result to allocate memory.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.