cybersecurity · intermediate · ~15 min

Strict integer parsing

Distinguish "permissive" parsing from strict input validation — a foundational security mindset.

Challenge

Implement int parse_safe_int(const char *s, int *out) that accepts a string consisting only of an optional leading - and one or more decimal digits, and stores the parsed value in *out. Return 0 on success, -1 otherwise. Empty strings, leading spaces, trailing junk, and + signs all fail.

Starter code

int parse_safe_int(const char *s, int *out) {
    /* TODO */
    return -1;
}

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