Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

Oh cruel compiler.

Posted by peeterjoot on June 22, 2010

Here’s a code fragment from some code I was debugging today.

{
   sqlzRc rc = 0 ;

   switch ( cfRc )
   {
      RECV_MRB_TIMEOUT:
      SEND_MCB_FAILURE:
      RECV_MCB_FAILURE:
      SEND_MRB_FAILURE:
      RECV_MRB_FAILURE:
      RECV_DATA_FAILURE:
      SEND_DATA_FAILURE:
      {
         rc = COMM_ERROR ;
         break ;
      }

      default:
      {

I expected COMM_ERROR out of this for the input (which based on log output appeared that it should have been cfRc==RECV_MRB_FAILURE), and was totally frigging perplexed.

Faced with the incontrovertible proof in the debugger that the input was what I thought it was, and the output was the result of the default case, I had another look.

Damn. Each of those labels is missing the case keyword, and the damn thing still compiles!

There’s so much here that the compiler could have noticed and didn’t:

  • Labels that didn’t have gotos connecting them to anything.
  • a break from a context that didn’t have a case.
  • labels that were typedef values too (I guess that is allowed … bizzarre).

This was with GCC4.2.1. After I fix this I have the inclination to revert the fix and try with some other compilers to see if they are more sane.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: