5 ms·
What Every C Programmer Should Know About Undefined Behavior Part 1 (2011)
- ajarmst 12y agoI've long believed that all compliant compilers should implement all undefined behaviour as an attempt to format the hard drive. It makes bugs much easier to detect in testing. Bugs that ship will mask themselves from customers, and many critical security bugs will be self-limiting and reduce the amount of data exposed.
- DSMan195276 12y agoThat's not really how undefined behavior works though. In general, undefined behavior is used to optimize code by assuming it never happens and thus ignore that case. "implementing" undefined behavior to format the hard drive would mean that compilers would have to implement checks for every possible type of undefined behavior whenever you do something that has a undefined case (Ex. Every time you dereference a pointer). The slow-down would be fairly noticeable.
- Dewie 12y agoI think it was a joke, so not something that should be taken literally.
- deleted 12y ago[deleted]
- lomnakkus 12y agoThat's not really viable. Otherwise all compiler vendors would have already implemented a "error-on-all-undefined-behavior" switch. (See other poster.) Compiler vendors are trying to implement sanity checks and such, but there's only so much they can do given that whether something is "undefined behavior" may depend on run-time values. EDIT: Btw, this is what happens if you ever have a language with "undefined behavior". On the upside it can probably perform (given a correct program) like nobody's business on any given architecture (from x86 to DSPs with 10-bit chars).
- nostrademons 12y agoIf all compliant compilers implement undefined behavior as an attempt to format the hard drive, it's not exactly undefined behavior any more, right? The behavior has been defined as "format the hard drive". The HTML5 spec is a good example of the pros and cons of defining everything. On one hand, you can be sure that every compliant parser will parse the document the same way, including incredibly malformed documents. On the other, it significantly increases the complexity of the implementation, and it eliminates certain optimization opportunities. On the third hand - it doesn't seem to have discouraged page authors from writing incredibly malformed HTML. If it had discouraged page authors from writing incredibly malformed HTML, then a significant portion of the web would break.
- kazinator 12y agoThe behavior is still formally undefined; it has been locally defined by the implementor as a documented extension. So to speak, it is "evil-implementor-defined", but not "standard-defined". :)
- dllthomas 12y agoThere was a version of GHC (dev, never released) that would delete your source when there was a type error. Haskell on hardcore mode...
- ghshephard 12y agoIt would have never occurred to me that: for (i = 0; i <= N; ++i) { ... } Has a potential bug in it depending on compiler optimizations (where N=INT_MAX, and the results of ++i may or may not be defined - with the compiler using the "UNDEFINED" behavior to ensure that the above loop runs N+1 times.)
- kazinator 12y agoIf N is INT_MAX, and the loop does not have a break statement in in anywhere, then the ++i will evaluate on the last iteration and overflow past INT_MAX. That is undefined; there is no may or may not about it. Behavior which depends on compiler optimizations isn't a potential bug, it's almost always a bug. (The one exception might be programs whose specification is that they explore behaviors that depend on optimization!) The compiler can help here by diagnosing the suspicious comparison i <= N, if N is a constant known to be INT_MAX. warning: comparison always true The programmer can shut up the diagnostic by removing the expression, if it really is the intent that the loop guard is always true: for (i = 0; ; i++) { /* conditional break; somewhere in the loop prevents i++ overflow. */ }
- kazinator 12y agoThe light side of undefined behavior is not optimizations but extensions. Undefined behavior is necessary so that a language can have certain kinds of conforming extensions. The language standard cannot define every possible program, because programs can do platform-specific things. For instance, a program might need to map some hardware to memory and access registers. "Undefined behavior" in the ISO C sense only means that the ISO C standard doesn't provide requirements about how an implementation should react to a given situation. The implementation can still provide its own requirements, documented and all. ISO C undefined behavior can still be "GNU C defined" or "glibc defined". Using some unknown conversion specifier in printf is undefined behavior according to ISO C, but the one being used could be documented by the given C library as an extension. Using undefined behavior to make assumptions like that X < X + 1 is always true (and can be optimized away) is not a good example of undefined behavior being useful or beneficial. If the programmer wrote that, it was probably with the expectation that X + 1 could be less than X due to particular overflow semantics. Or maybe even X was once unsigned and someone changed it without considering that expression. This deserves a warning diagnostic like "warning: expression always true whenever its behavior is defined". And then, who cares how it is optimized; let the programmer fix it so the diagnostic goes away.
- chc 12y agoI thought this was the purpose of implementation-defined behavior rather than undefined behavior.
- kazinator 12y agoDifferent programming languages can possibly have different terminology for this. In C, "implementation-defined" behavior hinges around standard language features which have implementation-defined characteristics (which must be provided by an implementation). An example of such is the value of the CHAR_BIT macro in <limits.h> (how many bits in a byte) or the range of type int. It does not mean "anything defined by an implementation": that would be too obvious and clear a use of English.
- kruczek 12y agoUndefined behaviour means that anything can happen - also it may happen that particular implementation has such behaviour well defined ;)