4 ms·
[EDIT: more clearly distinguished between undefined and unspecified behavior.] > Question: why can't existing C compilers clearly indicate when they encounter
by JoshTriplett 11y ago
[EDIT: more clearly distinguished between undefined and unspecified behavior.]
> Question: why can't existing C compilers clearly indicate when they encounter undefined behavior?
Why can't existing programs clearly indicate when they encounter unexpected input?
Not every instance of undefined behavior has explicit handling in a compiler; sometimes, undefined behavior occurs as part of the implicit functioning of the compiler implementation. The compiler does not need to consider undefined behavior at all. It doesn't necessarily have an explicit case for "undefined, choose a behavior"; it can simply act as though undefined behavior does not exist, and have the behavior in that case depend on whatever the code written to handle defined behavior happens to do in the undefined case.
[EDIT: This example is unspecified behavior, not undefined behavior. The example does still illustrate what I meant by behavior that arises out of the compiler implementation without explicit notice by the compiler.]
For instance, if you write f(g(), h()), C doesn't specify whether g or h gets called first. The compiler doesn't explicitly parse that whole expression and internally note "oh, you have unspecified behavior here, I'll silently choose an interpretation and not tell you about it". The compiler just parses the expression and breaks it down into sequential operations, implicitly choosing an interpretation based on its parsing, evaluation, and optimization. The compiler might evaluate g() first because it parses left-to-right, or h() first because it parses right-to-left, or h() first because h() was an inline function and it expands those first...
A compiler explicitly designed to flag undefined behaviors would need to have additional logic to notice undefined behaviors; you couldn't just take an existing compiler and find all the places where it explicitly notices undefined behavior and chooses a way to handle it.
Existing compilers have started to add warnings for cases where they explicitly exploit undefined behavior for optimization, such as assuming a lack of signed overflow, or assuming that you never dereference a NULL pointer. But retrofitting a compiler to flag cases where it currently just doesn't even consider the undefined behavior would require far more work.
- GFK_of_xmaspast 11y ago> For instance, if you write f(g(), h()), C doesn't define whether g or h gets called first. That's unSPECIFIED behavior, not unDEFINED behavior. There's a big difference.
- JoshTriplett 11y agoYou're right, sorry. I should have looked up that specific example before using it, and used an example that fell under "undefined" behavior. My point, though, was that the compiler doesn't have to explicitly notice the program using undefined behavior and choose a behavior. Rather, the compiler may simply not consider undefined behavior at all, act as though only defined behaviors can occur, and have undefined behavior be whatever happens due to the lack of explicit handling. The compiler can emit code that handles the defined behavior, and whatever that code happens to do in other cases will be the compiler's "handling" of undefined behavior. Thus, making the compiler explicitly flag undefined behavior isn't just a matter of finding all the places where the compiler explicitly handles undefined behavior and emitting a warning there. It would require noticing everywhere the compiler simply doesn't consider cases the spec doesn't require it to consider.
- twoodfin 11y agoEverything not defined in the spec represents undefined behavior, not just the things explicitly disclaimed as undefined. Not every instance of undefined behavior has explicit handling in a compiler; sometimes, undefined behavior occurs as part of the implicit functioning of the compiler implementation. The C and C++ standards make clear separations between "undefined behavior", "unspecified behavior", and "implementation defined behavior". Order of argument evaluation is not undefined behavior, it's unspecified behavior. A standard-compliant implementation can choose any order it wants in any instance, it doesn't have to document this behavior, but it does have to produce output consistent with some ordering. It can't erase your hard disk or launch the nuclear missiles, which it could do if it encountered undefined behavior.