compiler: Add builtin overflow flag and predicate wrappers
Checking for overflow can be difficult, but doing so may be a good
idea to avoid difficult to debug problems. Compilers that provide
builtins for overflow checking allow the checks to be simple
enough that we can use them more liberally. The idea for this
flag is to wrap a calculation that should have overflow checking,
allowing compilers that support it to give us some extra robustness.
For example,
#ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
bool overflow = __builtin_mul_overflow(x, y, &z);
assert(!overflow);
#else
/* Older compiler, hopefully we don't overflow... */
z = x * y;
#endif
This is a bit ugly though, so when possible we can just use the
predicate wrappers, which have an always-false fallback, e.g.
/* Old compilers won't assert on overflow. Oh, well... */
assert(!check_mul_overflow(x, y));
z = x * y;
Signed-off-by:
Andrew Jones <drjones@redhat.com>
Loading
Please register or sign in to comment