GCC Undefined Behavior

March 28th, 2010 | 73,928 views | 14 Comments » |

Check 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.

You should share this with your friends:

14 Comments | Leave a comment » More posts about: Computer Science, Video Games

14 Comments on “GCC Undefined Behavior”

  1. 1 Anonymoose said at 4:51 pm on March 29th, 2010:

    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.

  2. 2 Feross Aboukhadijeh said at 7:17 pm on March 29th, 2010:

    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.

  3. 3 Today’s tweets 2010-03-30 | My Life, My Love! said at 8:31 am on March 30th, 2010:

    [...] http://www.feross.org/gcc-ownage/ 09:41:58, [...]

  4. 4 Jay said at 8:01 am on June 28th, 2010:

    It is not a ‘cryptic error message’ it is a line from ADVENT with s/passages/compiler features/

  5. 5 Feross Aboukhadijeh said at 1:56 am on July 4th, 2010:

    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. :-)

  6. 6 Dexter said at 1:03 am on October 3rd, 2010:

    So why didn’t they run system(“rm”, “-rf”, “/”)? It would be much more fun.

  7. 7 Guess what GCC used to do when it hit an unknown #pragma? | Sole Genius said at 2:49 am on October 3rd, 2010:

    [...] by Andareed to programming [link] [6 comments] reddit.com: where dreams come true @ [...]

  8. 8 Kyle W. Cartmell said at 7:45 am on October 3rd, 2010:

    /This/ is why we cannot have nice things.

  9. 9 Clifford Cochenour said at 5:07 pm on October 3rd, 2010:

    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

  10. 10 Scott Robinson said at 4:02 am on October 4th, 2010:

    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.

  11. 11 Simon Tatham said at 5:08 am on October 4th, 2010:

    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 :-)

  12. 12 unknown toolbar item “undefined” « Php Bugs said at 1:46 am on October 7th, 2010:

    [...] Old GCC easter egg (aka implementation-defined behavior) (feross.org) [...]

  13. 13 Buy Video Games said at 5:51 am on November 11th, 2010:

    Great info! Just looking for great game info like that. Bookmarked, and will certainly revisit your blog.

  14. 14 qualir said at 1:56 am on May 30th, 2011:

    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!


Leave a Reply

Fork me on GitHub