6 ms·
Upvoted for the signed integer overflow example. I'll admit that I actually don't know the most idiomatic, bulletproof way of testing for signed overflow in C;
by kibwen 8y ago
Upvoted for the signed integer overflow example. I'll admit that I actually don't know the most idiomatic, bulletproof way of testing for signed overflow in C; if you google "how to test signed integer overflow in C", the very first result is essentially equivalent to the buggy example in the blog post ( https://www.geeksforgeeks.org/check-for-integer-overflow/ https://www.geeksforgeeks.org/check-for-integer-overflow/ ), and I'm not keen to repeat the legendary case of signed overflow within the PHP interpreter: https://web.archive.org/web/20120412194929/http://use.perl.org/use.perl.org/_Aristotle/journal/33448.html https://web.archive.org/web/20120412194929/http://use.perl.o...
- 0xcde4c3db 8y agoI'm not sure there is a reliable, generic approach for detecting overflow in conformant C; you probably have to come up with the appropriate proof for the specific operation that you're about to do. OpenBSD does it for multiplication in reallocarray() [1], which was introduced because it was easy to code an integer overflow when doing something like malloc(sizeof(member) * nmembers). [1] https://github.com/openbsd/src/blob/master/lib/libc/stdlib/reallocarray.c https://github.com/openbsd/src/blob/master/lib/libc/stdlib/r...
- derf_ 8y agoIt depends on what operation could potentially overflow. Mozilla has a CheckedInt (C++) type that checks all operations for overflow. You can see examples of how this is actually implemented here: <https://searchfox.org/mozilla-central/source/mfbt/CheckedInt.h#254> https://searchfox.org/mozilla-central/source/mfbt/CheckedInt.... You can derive similar C implementations if you work through all of the template magic. Be warned that the definition of what is undefined, implementation-specific, and well-defined varies depending on the exact version of the C or C++ standard you use.
- raverbashing 8y agoWell, the way you test are usually: 1) you know the ranges your params belong to and 2) you avoid situations where you might overflow. Usually if you catch the overflow you already lost. I'm surprised to see that for unsigned ints modular semantics are guaranteed by the spec "Please tell me again how unsigned ints are broken" It seems this meme seems to be the C version of anti-vaxxers.
- nickpsecurity 8y ago"don't know the most idiomatic, bulletproof way of testing for signed overflow in C" Basically, a great tool for static analysis and/or Frama-C for just the range analysis. Then, a coding style that facilitates such analysis. That should cover analysis. Idiomatic might be a problem because I see so many overflows in C code that it seems idiomatic for it to have them. ;)
- garethrees 8y agoIn C you avoid signed integer overflow either by knowing that the result will be in range before applying an operation, or, as a last resort, by testing that the operation is defined before carrying it out. For example, if a and b are ints, then a + b is defined if (a >= 0 && b <= INT_MAX - a) || (a < 0 && b >= INT_MIN - a). Additionally, some compilers can insert code to detect signed integer overflow at runtime. Clang has the -fsanitize=undefined flag [1]. [1] https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
- rurban 8y agoNope, that's the old way. Nowadays you do have access to the (un)signed add with carry (adc) builtins, which are the best way to provide an alternate method on overflow.
- pjmlp 8y agoThat is not portable.
- rurban 8y agono, gcc and clang do provide portable builtins. previously you had to use inline assembly, now it's trivial.
- pjmlp 8y agoGCC and clang aren't the only C compilers on the planet. Portable means being defined in the ISO C standard.
- rurban 8y agoNope. Practically gcc and clang are available for all platforms, and for the rare cases someone uses another compiler or an old version you ifdef it out with the slow path. even ICC has the add/mult with carry overflow builtins. The ISO C standard is hopelessly behind for decades. They don't even define a proper (unicode) string API (search, norm, utf8, fc) , constexpr, a memory model, ... and leave everything to the implementors.
- ajross 8y agoI'll just say it: that's a clang bug. The assumption being made by the optimizer is wrong, for exactly the reason that overflow exists. I get very tired of people trying to make this "really" about "undefined behavior" sanity when real compilers are real software with real behavior. And here there is a clearly desired behavior (whether or not it got written into a standard somehwere), and clang implemented something else. That's wrong.
- keybuk 8y agoThis particular undefined case is a really interesting one; it seems utterly non-obvious why it exists, until you remember one thing... The most popular architecture today is 64-bit, with ABI specifying that the integer type is 32-bits wide. So when faced with performing an operation on two 32-bit signed integers, on a 64-bit platform, you have to either: 1) perform the operation in 32-bit registers - if available - and even if available, usually far far far fewer in number thus massive performance & resource penalties to code 2) perform the operation in 64-bit registers, then add code to check the result, and if it would overflow in 32-bit, compute what the overflowed result would have looked like in a 32-bit register, and return that instead - again quite a performance hit 3) perform the operation in 64-bit registers, and just return the lower 32-bits, whatever that might be The standard declined to pick a choice, which is why we have UB here. (For unsigned integers, you can just truncate the result and it works every time, which is why the standard defines the result)
- vyodaiken 8y agoWait a second: both ARM and IA-64 have 32 bit operations.
- nayuki 8y ago> 1) ... perform the operation in 32-bit registers - if available - and even if available, usually far far far fewer in number False for x86-64. There are sixteen 32-bit general-purpose registers: eax, ebx, ..., r8d, ..., r16d. There are sixteen 64-bit registers too: rax, rbx, ..., r8, ..., r16.
- pcwalton 8y ago
- pcwalton 8y agoUse the compiler intrinsics (__builtin_add_overflow, etc.): that's what they're there for. They are guaranteed to generate the most efficient code.
- pjmlp 8y agoWith plenty of #ifdef #else #endif, as they aren't portable.
- nkurz 8y agoInstead of obfuscating with ifdef's, what's the downside of just using a non-standard but well-supported intrinsic that works for major compilers on common systems? For closed source, you are distributing a binary anyway, and have control of the compiler you choose. For open source, it may fail to compile, but the error message should make it clear what needs to be done (probably linking in a function written in assembly for the unsupported platform).
- vyodaiken 8y ago-fwrapv gives you the desired C semantics.
- baby 8y agoWith Clang you can use `-fsanitize=undefined` which will try to find out these overflows.