A comment below reminded me of these: https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins..., which are close, but have an insane API as they replace your arithmetic operations with ugliness.
I think they can be fixed with sensible macros, though:
#define ADDITION_WOULD_OVERFLOW(a, b) \
({ typeof((a) + (b)) __r; __builtin_add_overflow((a), (b), &__r); })
Hopefully the compiler would optimise: if (ADDITION_WOULD_OVERFLOW(a, b))
{
return 0;
}
return a + b;
to only do the addition once.