8 ms·
Integer overflow should have been implementation-defined behavior from day one, because that's what it actually is.
by formerly_proven 6y ago
Integer overflow should have been implementation-defined behavior from day one, because that's what it actually is.
- saagarjha 6y agoImplementation defined means that the behavior is defined. This is patently not what the existing behavior of compilers in the face of integer overflow is.
- formerly_proven 6y agoI'm trying to say that integer overflow is architecture-specific, so it _should_ have been standardized as implementation defined.
- rocqua 6y agoYou probably know this, but by making it UB, compilers don't need to have the same behavior on every case of integer overflow. This allows compilers to apply much more optimizations.
- ChrisLomont 6y agoEvery implementation (compiler and arch combo) defines behavior as far as I know, and I’ve tested dozens of combos. What combo does not have implementation defined behavior for C/C++ signed overflow? Are there any?
- saagarjha 6y agoGCC? Clang? Neither compiler defines it unless you give it flags to specify a certain behavior.
- ChrisLomont 6y agoNo, both define it by default on every platform, with no flags needed. Flags allow you to force behavior one way or another. Here [1], for example, is the output from godbolt demonstrating exactly this. Note there are no flags specified, it produces valid platform specific code, and it does so for every platform listed. You can then set flags if you desire, but there is a default, as I stated. https://godbolt.org/z/jbY6PM https://godbolt.org/z/jbY6PM
- saagarjha 6y agoAs you can see, the compiler has chosen an implementation that overflows in this case. It is not required to.
- ChrisLomont 6y agoYes, which is exactly what I stated. My response was to “ Implementation defined means that the behavior is defined. This is patently not what the existing behavior of compilers in the face of integer overflow is.”, implying compilers didn’t have implementation defined behavior. For integer overflow, they do. You just noted this compiler chose an implementation defined behavior. And you don’t need flags for it to choose. You just stated “ Neither compiler defines it unless you give it flags to specify a certain behavior”. It does define it, exactly as I said, per compiler per platform. No flags made any of them produce implementation defined output. Flags make them produce specific, cross platform behavior. What exactly are you disputing?
- saagarjha 6y agoThe compiler has not committed to a behavior, it is free to compile that code to trap the next time you try if it so wishes. "Implementation defined" means that on this particular implementation the compiler will always do the same thing.
- ChrisLomont 6y agoAre you claiming if I compile the same code with the same version of the compiler on the same platform the behavior changes from compile to compile? Demonstrate that. If not, then it is as I stated. Certainly compilers can change behavior between versions, listed as breaking changes in the documentation.
- formerly_proven 6y agoSigned integer overflow is decidedly not implementation-defined behavior in GCC, clang, recent MSVC, ... I think you misunderstand the meaning of "implementation-defined behavior". It means the implementation chooses a particular behavior and documents it does so. For example, a compiler might choose to always wrap on signed overflow and put that in their manual. That's not what's happening.
- deleted 6y ago[deleted]
- secondcoming 6y agoIt means that the behaviour is defined for a particular compiler and architecture target.
- olliej 6y agowhat you're after is specified behavior. C/C++ essentially has specified, unspecified, and undefined behavior. I can't recall correct terminology. Basically (in order), universally defined behavior, architecturally defined (under/overflow semantics, argument evaluation order, etc), things that cannot be defined (UaF, buffer under/overflows, etc). The problem is that there is a depressing amount of undefined behavior is unnecessarily undefined. UB should be specifically limited to things that cannot have a predictable/defined behavior on any arbitrary platform - e.g. memory errors, etc.