cybersecurity · intermediate · ~15 min

Overflow-checked size addition

Detect size_t addition wrap-around.

Challenge

Implement int safe_add_size(size_t a, size_t b, size_t *out) storing a+b in *out and returning 0, or -1 if the unsigned addition would wrap past SIZE_MAX.

Starter code

#include <stdint.h>
#include <stddef.h>

int safe_add_size(size_t a, size_t b, size_t *out) {
    /* TODO */
    return -1;
}

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