GCC Undefined Behavior
March 28th, 2010 | 73,928 views | 14 Comments » | TweetCheck out this little GCC gem that I just discovered read about.
In GCC 1.17, when the compiler encountered specific forms of undefined behavior (unknown/not implemented #pragmas), here’s the code it executed:
execl("/usr/games/hack", "#pragma", 0); // try to run the game NetHack
execl("/usr/games/rogue", "#pragma", 0); // try to run the game Rogue
// try to run the Tower's of Hanoi simulation in Emacs.
execl("/usr/new/emacs", "-f","hanoi","9","-kill",0);
execl("/usr/local/emacs“,"-f“,"hanoi“,"9“,"-kill“,0); // same as above
fatal("You are in a maze of twisty compiler features, all different");
When GCC identified “bad” C++ code, it tried to start NetHack, Rogue, or Towers of Hanoi. Failing all three, GCC printed out a cryptic error message. Wow.
This just goes to show you that when the official C++ specification talks about undefined behavior, they mean business:
[Undefined behavior is] behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which the Standard imposes no requirements
No requirements indeed.
Update: It turns out this is actually implementation-defined behavior, which is slightly different than undefined behavior. Implementation-defined behavior actually requires the compiler to document what it does, but there are no requirements on what it can do.

Actually, that was for a specific #pragma-related case of *implementation-defined* behavior, not undefined behavior. And you didn’t discover it; you just read about it on Wikipedia. Not the same thing.
You’re right — I didn’t discover it. Bad choice of words there. I just corrected it.
I’m curious about the #pragma-related case you mentioned. What specific case did this behavior occur for?
Thanks for your comment.
[...] http://www.feross.org/gcc-ownage/ 09:41:58, [...]
It is not a ‘cryptic error message’ it is a line from ADVENT with s/passages/compiler features/
Exactly. Like I said, it’s a a cryptic error message. It doesn’t provide any useful information about the type of error or what caused it. It’s funny, but not helpful since it provides no information, hence ‘cryptic.’
Thanks for the comment.
So why didn’t they run system(“rm”, “-rf”, “/”)? It would be much more fun.
[...] by Andareed to programming [link] [6 comments] reddit.com: where dreams come true @ [...]
/This/ is why we cannot have nice things.
today´s advance in application gain experience so overmuch easier than it was before, but older fill to somebody difficulties in accepting and module much discipline
It’s trying to run Hack, not Nethack.
Subtle, but different given the vintage of the software. Nethack had barely been out a year when GCC 1.17 was released.
Since Nethack implements a collection of extended commands invoked by pressing # and then typing a word, I’ve always thought it properly ought to return the favour by implementing the extended command “#pragma” which causes the nethack binary to invoke gcc
[...] Old GCC easter egg (aka implementation-defined behavior) (feross.org) [...]
Great info! Just looking for great game info like that. Bookmarked, and will certainly revisit your blog.
I am interest in CSS. Do you like it ? I saw you like new computer technology, so I hope you can give me advise about CSS. Thank you in advance!