cybersecurity · intermediate · ~15 min

Overflow-checked multiply

Detect integer multiplication overflow.

Challenge

Multiply two ints, detecting overflow before it corrupts a calculation (the classic attacker-controlled allocation-size bug).

Task

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.

Input

  • a, b: the two ints to multiply.
  • out: where the product is written on success.

Output

Returns int: 0 on success (with *out = a * b), or -1 on overflow.

Example

int o;
safe_mul(1000, 1000, &o)     ->   0, o == 1000000
safe_mul(100000, 100000, &o) ->   -1   (overflows int)

Edge cases

  • A product that exceeds INT_MAX or falls below INT_MIN returns -1.

Rules

  • Compute in a wider type and compare against INT_MIN/INT_MAX.

Input format

Two ints a, b, and an output pointer out.

Output format

An int: 0 on success (with *out = a*b), or -1 on overflow.

Constraints

Detect overflow via a wider type; compare against INT_MIN/INT_MAX.

Starter code

#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.