An old car odometer has six digits. Drive it to 999,999 miles, add one more, and it shows 000,000. The car didn't lose its miles; the display just ran out of digits. Computers do exactly the same thing with numbers, and when a program doesn't expect it, the results range from a glitch to a full security compromise. That's an integer overflow, and it's behind some of the most severe vulnerabilities ever found.
This guide explains what an integer overflow is, why it happens, two real cases (one in an airliner, one in GitLab), how it turns into a security hole, and how to prevent it.
What an integer overflow is
MITRE's weakness catalog (CWE-190) describes a calculation that "can produce an integer overflow or wraparound when the logic assumes that the resulting value will always be larger than the original value." When it happens, "the value may become a very small or negative number."
Most programming languages store integers in a fixed number of bits:
| Type | Smallest | Largest |
|---|---|---|
| unsigned 8-bit | 0 | 255 |
| signed 32-bit | −2,147,483,648 | 2,147,483,647 |
| unsigned 32-bit | 0 | 4,294,967,295 |
| signed 64-bit | about −9.2 × 10^18 | about 9.2 × 10^18 |
Add 1 to the largest unsigned value and you get 0. Add 1 to the largest signed value and, on almost every machine, you get the most negative value. In C and C++, unsigned wraparound is defined behaviour; signed overflow is formally undefined behaviour, which is even worse, because the compiler may assume it never happens. Languages like Python use unlimited integers and don't overflow, but they call C libraries that do.
An everyday analogy
Think of a clock. Three hours after 11 o'clock isn't 14 o'clock; it's 2 o'clock. Nobody is confused, because we know clocks wrap. Programs get into trouble when the code was written as if the number were a ruler that keeps going, but it's actually a clock.
How it works: our test with real 32-bit integers
Python's own integers never overflow, so our lab uses ctypes to get real C fixed-width types:
import ctypes
ctypes.c_int32(2_147_483_647 + 1).value # -2147483648
ctypes.c_uint32(268_435_457 * 16).value # 16
Running the lab:
$ python3 odometer.py
1) uptime counter, signed 32-bit, hundredths of a second
2,147,483,646
2,147,483,647
-2,147,483,648
wraps after 248.55 days of continuous running
2) size = count x item_size, computed in 32 bits
268,435,457 x 16 = 4,294,967,312 bytes really needed
32-bit result: 16 bytes allocated <- wrapped
3) checked multiply
refused: 268435457 x 16 does not fit in 32 bits
Two things to notice. A counter that grows by one never "fails"; it just suddenly becomes negative. And a multiplication that should give four billion gives sixteen.
A real case: the Boeing 787 that needed powering down
In 2015 the US Federal Aviation Administration issued Airworthiness Directive 2015-09-07 for every Boeing 787. Boeing had found in laboratory testing that "a software counter internal to the GCUs" (the generator control units) "will overflow after 248 days of continuous power", sending each unit into failsafe mode. If the four main units had been powered up at the same time, all four would fail together, "resulting in a loss of all AC electrical power regardless of flight phase", which the FAA said "can result in loss of control of the airplane."
The interim fix wasn't code. It was a maintenance task: power the airplane down at intervals of no more than 120 days, so the counter never gets near its limit, while Boeing developed a software update.
Boeing didn't publish the counter's exact type. But here's a piece of arithmetic: a signed 32-bit counter counting hundredths of a second reaches its maximum after 2^31 / 100 seconds, which is 248.55 days. That matches the directive closely; treat it as our calculation, not Boeing's statement.
Why security cares: sizes that wrap
The dangerous version in software security isn't a counter; it's a size. Programs constantly compute how much memory to allocate:
uint32_t size = count * item_size; // may wrap
char *buf = malloc(size); // small buffer
for (uint32_t i = 0; i < count; i++) // ...but count items are written
memcpy(buf + i * item_size, src[i], item_size);
If an attacker controls count, they can pick a value where count * item_size wraps to something tiny. The program
allocates a few bytes and then writes megabytes or gigabytes past the end, over whatever lives next to it in memory.
That's a heap buffer overflow, and with care it can lead to code execution.
GitLab CVE-2026-97229 is a recent example of the class: an "integer overflow in the size of a compiled pattern" in the regular-expression compiler GitLab uses, reachable without an account and rated CVSS 10.0. Our breakdown: the video and the write-up.
Where integer overflows show up
- Memory sizes:
count * size,length + header, image width × height × bytes per pixel. - Counters and timers: uptime counters, sequence numbers, IDs that increment forever.
- Loops and indexes: a loop counter that wraps, so a "bounded" loop never ends or reads out of range.
- Money and quantities: totals and balances that wrap from large positive to negative.
- Dates and times: 32-bit Unix time (seconds since 1970) runs out on 19 January 2038.
- Conversions: a 64-bit value squeezed into a 16- or 32-bit field silently loses its top bits.
How to prevent it
1. Use checked arithmetic. Most languages can tell you that an operation overflowed instead of wrapping:
size_t size;
if (__builtin_mul_overflow(count, item_size, &size)) return -1; // GCC and Clang
let size = count.checked_mul(item_size).ok_or(Error::TooBig)?; // Rust
long size = Math.multiplyExact(count, itemSize); // Java: throws ArithmeticException
2. Put limits on inputs that become sizes. A request for 268 million items isn't normal. Validate counts, lengths and dimensions against sensible maximums before doing arithmetic with them.
3. Use wider types for counters that never stop. A 64-bit counter of nanoseconds lasts about 292 years. For counters that must wrap (sequence numbers), compare differences, not raw values.
4. Let tools find them. Compiler sanitizers (-fsanitize=undefined, -fsanitize=integer in Clang), fuzzing, and
static analysis catch many overflows before release. Rust panics on overflow in debug builds by default.
5. Prefer memory-safe languages for new parsers. Overflows still happen, but they can't silently turn into out-of-bounds writes.
Myths vs reality
- "Overflows only matter in C." They matter wherever fixed-width integers exist: Java, C#, Go, Rust in release mode, databases, and every C library a Python or JavaScript program calls.
- "A 64-bit number never overflows." It can, when it's multiplied, converted to a smaller type, or fed values an attacker chose.
- "It's just a wrong number." In a size calculation, a wrong number becomes a too-small buffer, and that's a memory corruption bug.
Getting started: a checklist
- Search your code for arithmetic that feeds
malloc,new[], buffer lengths or array indexes. - Replace it with checked operations, or check limits first.
- Validate every externally supplied count, length and dimension.
- Make long-running counters 64-bit, and test what happens at the limit.
- Turn on sanitizers in CI and fuzz parsers that read untrusted input.
- In code review, ask: "what's the biggest value this can be, and what happens one past it?"
Try it yourself
The lab is in SpiderCob/cve-labs: concepts/integer-overflow:
odometer.py: a signed 32-bit counter that wraps after 248.55 days, a 32-bit size that wraps to 16 bytes, and a checked multiply that refuses.tests/: proves each of those behaviours.
It runs locally and sends nothing over the network.
Key takeaways
- Fixed-size integers wrap around, like an odometer or a clock.
- A counter that wraps can take down systems (the 787's generator control units); a size that wraps can hand an attacker a memory corruption bug (GitLab CVE-2026-97229).
- Check before you add or multiply, limit inputs that become sizes, and use 64-bit for counters that never stop.
Glossary
- Integer overflow / wraparound (CWE-190): a result too big for its type wraps to a small or negative value.
- Signed / unsigned: whether a type can hold negative numbers.
- Checked arithmetic: operations that report overflow instead of wrapping.
- Heap buffer overflow: writing past the end of a memory allocation, often the consequence of a wrapped size.
- Undefined behaviour: in C/C++, code whose result the language doesn't define, such as signed overflow.
Sources
- MITRE CWE-190, Integer Overflow or Wraparound: https://cwe.mitre.org/data/definitions/190.html
- FAA Airworthiness Directive 2015-09-07, The Boeing Company Model 787 (Federal Register, 1 May 2015): https://www.federalregister.gov/documents/2015/05/01/2015-10066/airworthiness-directives-the-boeing-company-airplanes
- SpiderCob, GitLab CVE-2026-97229: https://spidercob.com/cve/2026-97229/