cybersecurity · intermediate · ~15 min
Detect integer multiplication overflow.
Multiply two ints, detecting overflow before it corrupts a calculation (the classic attacker-controlled allocation-size bug).
Implement int safe_mul(int a, int b, int *out) that stores a * b in *out and returns 0 when it fits in an int, or returns -1 on overflow. Compute the product in a wider type (long) to detect it.
a, b: the two ints to multiply.out: where the product is written on success.Returns int: 0 on success (with *out = a * b), or -1 on overflow.
int o;
safe_mul(1000, 1000, &o) -> 0, o == 1000000
safe_mul(100000, 100000, &o) -> -1 (overflows int)
INT_MAX or falls below INT_MIN returns -1.INT_MIN/INT_MAX.Two ints a, b, and an output pointer out.
An int: 0 on success (with *out = a*b), or -1 on overflow.
Detect overflow via a wider type; compare against INT_MIN/INT_MAX.
#include <limits.h>
int safe_mul(int a, int b, int *out) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.