Table of Contents
An overflow occurs when a value or operation exceeds a limit. An integer overflow exceeds the range of a number type. A buffer overflow writes beyond reserved storage. Stack exhaustion occurs when function calls or local storage use up the call stack.

Common overflow errors
| Type | What went wrong | What to check |
|---|---|---|
| Integer overflow | A result falls outside the type’s range. | Arithmetic, type conversions, and counters near their limits. |
| Buffer overflow | A write crosses a buffer boundary. | Actual capacity, input length, indexes, and copy sizes. |
| Stack exhaustion | The call stack runs out of space. | Recursion depth, base cases, and large local variables. |
| Heap buffer overflow | A write crosses the boundary of a dynamically allocated buffer. | Whether allocation and write sizes agree. |
Allocation failure is a separate case where memory cannot be reserved. Programs should handle it explicitly.
Prevent overflow in your code
- Validate sizes and ranges at the input boundary. Decide whether invalid inputs should be rejected or handled another way.
- Use checked arithmetic for sizes and counts where the language provides it. Check the entire calculation before converting the result to a narrower type.
- Prefer bounds-checked collections or APIs that take the actual destination capacity. Treat truncation as an explicit outcome that needs handling.
- Put a clear limit on nesting or use an iterative approach if recursion depth can grow with input size.
Catch regressions before release
Test empty inputs, exact capacity, values just above supported limits, and repeated operations that increase counters. In C and C++ test builds, AddressSanitizer can detect many out-of-bounds accesses. UndefinedBehaviorSanitizer can detect operations such as signed integer overflow. Use their reports to trace which check is missing and add a regression test.
Reader Comments 0
Sign in with email or Google to join the discussion.