4 ms·
> Naïve overflow checks, which are often security-critical, often get eliminated by compilers. This leads to exploitable code when the intent was clearly not to
by raverbashing 8y ago
> Naïve overflow checks, which are often security-critical, often get eliminated by compilers. This leads to exploitable code when the intent was clearly not to and the code, while naïve, was correctly performing security checks for two’s complement integers.
This is the most critical aspect. We have enough trouble already without the compiler actually fighting against security because this would fail in a machine from the 70s
- sanxiyn 8y agoThis got rejected in the next revision of this proposal. Naive overflow checks are still undefined.
- gpvos 8y agoOkay, then the proposal is already mostly useless.
- dzdt 8y agoIf a signed operation would naturally produce a value that is not within the range of the result type, the behavior is undefined. The author had hoped to make this well-defined as wrapping (the operations produce the same value bits as for the corresponding unsigned type), but WG21 had strong resistance against this. Who are the standards committee people having strong resistance against this, and what in the world is their argument?
- kazinator 8y agoUndefined doesn't mean incorrect; it's just the absence of a requirement. A compiler writer can add requirements locally (like "signed integer overflows have wrapping behavior") that are missing in the standard. The standard is not a suitable babysitting tool for GCC maintainers, which is what I suspect is the motivation here.
- yoklov 8y agoThis isn’t entirely untrue in theory, but in practice it is. Compilers optimize under the assumption that undefined behavior never occurs, and the standard is written with this assumption. Any conforming compiler is free to break code that relies on this. Typically, behavior that reasonably may vary from compiler/machine is considered implementation defined, and not undefined.
- kazinator 8y ago> Any conforming compiler is free to break code that relies on this. Ah, but, for example, __attribute__((packed)) is undefined behavior; is the compiler free to break that? This "free to break" is a juvenile fiction based on the idea that the only document that applies is ISO C; there is no other contract or promise between user and implementor.
- sanxiyn 8y agoGCC documentation is another such contract, which provides -fwrapv. If you want -fwrapv, use -fwrapv.
- sanxiyn 8y agoI am not sure why people pick GCC in particular for this issue. LLVM does exactly the same.
- fulafel 8y agoIsn't the quoted part 180 degrees wrong? Such code was not "correctly performing security checks", since it was undefined behaviour - two's complement or not. Which was the whole problem.
- iainmerrick 8y agoThe checks were not correct for all possible representations. If the only representation were two’s complement, there could be fewer areas of undefined behavior, so simple and straightforward security checks would be much more likely to be correct. (Unfortunately it sounds like this proposal has been revised to leave overflow behavior as undefined. That’s a biggie. Oh well)
- mannykannot 8y ago> The checks were not correct for all possible representations. This raises the question of whether the optimization was valid, given the target architecture. I am assuming that, technically speaking, it was, because that is what the standard allowed, but that view leaves unexamined the question of whether it is contrary to the purposes for having and using the C language in the first place. I can imagine an argument pointing out that this optimization is applied at an abstract level of representation prior to code generation, and that it would be a violation of modularity to take into account the target architecture at that point. This, however, would be a point about the compiler architecture, which I think should, where practical, yield to concerns about the overall purpose and use of the compiler, where the principle of 'no (or minimal) surprises' is important.
- gpvos 8y agoDon't leave out the last part: ...was correctly performing security checks for two’s complement integers. More pedantically, they could have written: ...was correctly performing security checks if the standard had been limited to two’s complement integers. But that was clearly the intent.
- fulafel 8y ago
- virgilp 8y agoWhy are you assuming "machine from the 70s"? I know modern processors (DSPs) that need to saturate on integer arithmetic in order to maintain correctness. If you want your naive overflow checks to work on your x86 project, why not just use a compiler option like -fwrapv? IMO, this is just plain ignorance - people arguing against the standard, while believing only their favorite platform is significant. C code is still big in embedded, you can't just trash the standard like that.
- raverbashing 8y agoI've used DSPs and embedded systems, those compilers have sufficient quirks already and usually don't follow the standard to the letter. I'm not worried about "signal" values, I'm worried about pointer arithmetic values. x86 SIMD is saturating as well > If you want your naive overflow checks to work on your x86 project, why not just use a compiler option like -fwrapv? Fair enough. Or people can stop pretending that UB is just an excuse to throw your hands in the air and do whatever they want with the code. Including null checks. Because when things blow up it's on the major platforms.
- Bromskloss 8y agoBy all means, make the change for the new version of the language. In the meantime, though, why are people assuming two's complement?!
- gpvos 8y agoGeneral knowledge. Have you read the entire standard? I haven't, but I know my machine uses two's complement internally, and C and C++ are generally supposed to be close to the bare metal.