Skip to main content

Practice · 1 of 5

Overflow Guard, Correctly Ordered

CHALLENGE
Difficulty: advanced+25 XP

Implement int safe_add3(int a, int b, int *out): if a + b would overflow, return 0 and leave *out untouched; otherwise store the sum and return 1. The check must happen BEFORE any signed arithmetic that could overflow — use comparisons against INT_MAX/INT_MIN. Then implement int naive_add3(int a, int b, int *out) that just stores a + b (defined for the tested inputs, UB-adjacent by design).

Back to lesson: Practice: UB and Optimizer Drills