6 ms·
Is there a compiler flag that can be set to print a warning about all undefined behaviour? I'm no a C dev but this UB business seems like a little cat and mouse
by ajnin 8y ago
Is there a compiler flag that can be set to print a warning about all undefined behaviour? I'm no a C dev but this UB business seems like a little cat and mouse game between the developer and the compiler which tries to find "tricks" to avoid doing stuff, which seems backwards.
- gpderetta 8y agoIn the vast majority of the cases, undefined behaviour is impossible to detect a compile time.
- aw1621107 8y agoChris Lattner addresses this in part 3 of his “What Every C Programmer Should Know About Undefined Behavior” [0]: “People often ask why the compiler doesn't produce warnings when it is taking advantage of undefined behavior to do an optimization, since any such case might actually be a bug in the user code. The challenges with this approach are that it is 1) likely to generate far too many warnings to be useful - because these optimizations kick in all the time when there is no bug, 2) it is really tricky to generate these warnings only when people want them, and 3) we have no good way to express (to the user) how a series of optimizations combined to expose the opportunity being optimized.” Granted, this was written in 2011, so there’s a chance that something has changed, but the points brought up there seem like it isn’t that easy to fix. [0]: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
- yjftsjthsd-h 8y agoI suspect such an option would be so verbose as to be useless; I believe it is virtually impossible to write any substantial amount of C without encountering undefined-behavior. It still might have some value for writing programs if you're willing to put in the absurd amount of effort required to actually be safe, but now we're getting into the territory of OpenBSD or qmail; possible, but vanishingly rare.
- zielmicha 8y agoAddress/Undefined Sanitizer (-fsanitize=undefined) will detect many undefined behaviours (but at runtime).