The OP detected that INT_MAX - b could overflow, making the remaining code invalid for proper overflow detection. This does not work.
if (INT_MAX - b < a) {
The following describes a method for detecting overflow without UB:
int is_undefined_add1(int a, int b) { return (a < 0) ? (b < INT_MIN - a) : (b > INT_MAX - a); }
why make them int , and not unsigned int in the first place?
Switching to unsigned does not solve the problem at all. The range unsigned: [0...UINT_MAX] can be half the range int: [INT_MIN...INT_MAX] . IOWs: INT_MAX == UINT_MAX . Such systems are rare these days. IAC, mutable types are not needed, as specified in is_undefined_add1() .
chux
source share