Integer overflow is what happens when a calculation or input produces a whole number too large (or too small) for the data type meant to hold it. Instead of failing cleanly, the value often wraps around to the opposite end of the range, so adding 1 to the maximum signed 32-bit integer, 2,147,483,647, gives -2,147,483,648.
Why it matters: Overflow bugs are rarely announced. A counter, total or ID can silently turn negative or reset to zero, and the system carries on with wrong data. Because the limits of common integer types are fixed and well known, overflow is one of the most predictable classes of numeric bug, which makes it cheap to test for once you know where to look.
In practice: Testers work out which integer type sits behind a field, then test just below, at and just above each limit. Common boundaries include:
- 127 and 128 for signed 8-bit values.
- 255 and 256 for unsigned 8-bit values, such as colour channels.
- 32,767 and 32,768 for signed 16-bit values.
- 65,535 and 65,536 for unsigned 16-bit values, such as port numbers.
- 2,147,483,647 and 2,147,483,648 for signed 32-bit values.
It is also worth checking that different layers of a system agree on the type, since a value stored safely as unsigned can become negative when read as signed.
Examples:
- Adding 1 to an RGB channel at 255 wraps it to 0 and inverts the colour.
- A running total in a database smallint column turns negative after passing 32,767.
- Negating -2,147,483,648 overflows back to itself because the positive value cannot be stored.
Common pitfalls:
- Testing the value just above a boundary but never the valid value exactly at it.
- Assuming the language will throw an error, when many simply wrap the value without warning.
Does integer overflow happen in every programming language? No. Many languages wrap silently, some raise an exception, and Python handles large integers transparently, so the expected behaviour needs checking for each system.