cybersecurity · intermediate · ~15 min

Overflow-checked multiply

Detect integer multiplication overflow.

Challenge

Implement int safe_mul(int a, int b, int *out) storing a*b in *out and returning 0 if it fits in an int, or returning -1 on overflow (compute in a wider type to detect it).

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.