cybersecurity · intermediate · ~20 min

Add two ints with overflow detection

Pre-check overflow in signed arithmetic to avoid UB.

Challenge

Implement int add_safe(int a, int b, int *out).

Return:

  • 0 on success; *out holds a + b.
  • -1 on overflow; *out is left unchanged.

The check must be done before the addition, not after (a + b < a is undefined behaviour). Use the inequalities:

  • Overflow above INT_MAX: b > 0 && a > INT_MAX - b.
  • Overflow below INT_MIN: b < 0 && a < INT_MIN - b.

Examples

int out;
add_safe(1, 2, &out)            -> 0, out == 3
add_safe(INT_MAX, 0, &out)      -> 0, out == INT_MAX
add_safe(INT_MAX, 1, &out)      -> -1, out unchanged
add_safe(INT_MIN, -1, &out)     -> -1, out unchanged
add_safe(-5, 3, &out)           -> 0, out == -2

Why this matters

Vulnerabilities like CVE-2002-0639 (OpenSSH challenge-response), Bash 'shellshock'-adjacent issues, and dozens of image-decoder CVEs (PNG / JPEG / GIF) all originate from missed integer-overflow checks before allocating or indexing.

Why this matters

Signed integer overflow is undefined behaviour in C — modern compilers will literally delete if (a + b < a) because they assume overflow can't happen. Real-world security bugs (image decoders, malloc-size calculations) come straight from this trap.

Input format

Two ints + output pointer.

Output format

0/-1 and (maybe) *out.

Constraints

No GCC built-ins (__builtin_add_overflow) — do the math by hand.

Starter code

#include <limits.h>
int add_safe(int a, int b, int *out) { /* TODO */ return -1; }

Common mistakes

Adding first and then checking a + b < a — undefined behaviour. Mixing up the sign cases.

Edge cases to handle

a == INT_MAX, b == 0 (success). INT_MAX + 1. INT_MIN + -1. -1 + 1.

Complexity

O(1).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.