Clang has a fairly complete family of intrinsics to do integer arithmetic with overflow detection. They include function like __builtin_uadd_overflow, and they are described here: http://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins Please consider supporting these in GCC as well.
There was a recent proof-of-concept patch on the mailinglist to implement that.
It'd be nice if this were higher priority. I've been wanting this for years. We jump through a lot of confusing hoops in GNU applications to test for integer overflow, both signed and unsigned. See, for example, INT_MULTIPLY_OVERFLOW in: http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/intprops.h;h=d0bb7a6f57734e15e535cfc6b287a555dc6ccbb3;hb=HEAD Also, the need for a fast test for unsigned multiplication overflow recently came up in glibc internals discussions when writing a memory allocator. See: https://sourceware.org/ml/libc-alpha/2014-05/msg00480.html
(In reply to Paul Eggert from comment #2) > It'd be nice if this were higher priority. I've been wanting this for years. > We jump through a lot of confusing hoops in GNU applications to test for > integer overflow, both signed and unsigned. See, for example, > INT_MULTIPLY_OVERFLOW in: > > http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/intprops.h; > h=d0bb7a6f57734e15e535cfc6b287a555dc6ccbb3;hb=HEAD > > Also, the need for a fast test for unsigned multiplication overflow recently > came up in glibc internals discussions when writing a memory allocator. See: > > https://sourceware.org/ml/libc-alpha/2014-05/msg00480.html You would still need them to compile with older GCCs anyways. Glibc still needs to compiler with a few year old GCC.
I think this was the last message: https://gcc.gnu.org/ml/gcc/2014-04/msg00194.html
According to https://gcc.gnu.org/gcc-5/changes.html, this is now implemented. Thanks!