4 ms·
GCC has builtins for overflow-detecting arithmetic operations, and has done for a while. These generally compile down to one of the jump instruction flavours on
by nly 11y ago
GCC has builtins for overflow-detecting arithmetic operations, and has done for a while. These generally compile down to one of the jump instruction flavours on x86.
https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins...
- gpvos 11y agoThese look very unknown to me. How portable are these? If only GCC has these, I am not going to use them at all.
- frankzinger 11y agoClang also has them: http://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions http://clang.llvm.org/docs/LanguageExtensions.html#builtin-f..., under the heading "Checked Arithmetic Builtins".
- cnvogel 11y agoYou could implement them as static/inline functions for compilers that lack the builtins (using a bunch of comparisons with SHORT_/INT_MAX, ...) , which would be slower but for things explicitly meant as security check, probably worth the cost. I've used them to replace explicit checks for overflow in some DSP related code which tried to implement saturation. The gcc builtins also were much faster than tedious "manual" checks.
- tobias3 11y agoHere is how to do integer overflow checks from the same author as the OP in the bug report: https://www.fefe.de/intof.html https://www.fefe.de/intof.html