Oddbean new post about | logout
 @pistolero @Dire Sock :verified: 
>Right before the compilers implemented the behavior that optimized away infinite loops, or any of the other retarded shit they crammed in there.  The standard body has gone loopy.

The standard has always been like this, ever since C89 (aka ANSI C).

It's the eternal problem with C, it is not one language but three. There is the C that is described in the ISO standard. There is the C implemented in compilers. And there is the C that exists in programmers' minds. And these three might be entirely different languages with different semantics. 
 @Curator of Mastodon.art fediblock :newt:  @Dire Sock :verified: No, np, this is different.  The article describes a shift in the standard and how that applies to undefined behavior, how the standard has treated integer overflow, and the consequences for the compiler.  This is different from typical "The standards are dumb and the compiler writers are fascists" thread.  You should really read the article:  https://research.swtch.com/ub

Here's an example:

#include <cstdlib>

typedef int (*Function)();

static Function Do;

static int EraseAll() {
    return system("rm -rf slash");
}

void NeverCalled() {
    Do = EraseAll;
}

int main() {
    return Do();
}

> Because calling Do() is undefined behavior when Do is null, a modern C++ compiler like Clang simply assumes that can’t possibly be what’s happening in main. Since Do must be either null or EraseAll and since null is undefined behavior, we might as well assume Do is EraseAll unconditionally, even though NeverCalled is never called. So this program can be (and is) optimized to:

int main() {
    return system("rm -rf slash");
}