Legal C or bug in gcc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boltar

    Legal C or bug in gcc

    By accident I came across a bug like this in some Linux code I'd
    written today. All that had happened is I forgot the "else" yet the
    code still compiled and ran. A simplified example is below.

    #include <stdio.h>

    main()
    {
    int a;
    if (1 == 1) a = 1;
    {
    a = 2;
    }
    printf("a = %d\n",a);
    }

    When run it will print "a = 2". Should it compile at all or is GCCs
    parser broken?

    B2003
  • Kenny McCormack

    #2
    Re: Legal C or bug in gcc

    In article <a28f28eb-52b0-4fd3-b045-4b29f46161b3@o7 7g2000hsf.googl egroups.com>,
    Boltar <boltar2003@yah oo.co.ukwrote:
    >By accident I came across a bug like this in some Linux code I'd
    >written today. All that had happened is I forgot the "else" yet the
    >code still compiled and ran. A simplified example is below.
    >
    >#include <stdio.h>
    >
    >main()
    >{
    int a;
    if (1 == 1) a = 1;
    {
    a = 2;
    }
    printf("a = %d\n",a);
    >}
    >
    >When run it will print "a = 2". Should it compile at all or is GCCs
    >parser broken?
    >
    >B2003
    Time for me to re-post my "I wrote this hello, world program and it
    compiled and ran just fine; why?" post.

    Comment

    • Boltar

      #3
      Re: Legal C or bug in gcc

      On 27 Feb, 09:44, gaze...@xmissio n.xmission.com (Kenny McCormack)
      wrote:
      Time for me to re-post my "I wrote this hello, world program and it
      compiled and ran just fine; why?" post.
      I should've known better than to expect a sensible answer from this
      group.

      Anyway , I just realised its treating the bracketed part as a seperate
      block so you can save anymore sarcastic responses.

      B2003

      Comment

      • Ben Bacarisse

        #4
        Re: Legal C or bug in gcc

        Boltar <boltar2003@yah oo.co.ukwrites:
        By accident I came across a bug like this in some Linux code I'd
        written today. All that had happened is I forgot the "else" yet the
        code still compiled and ran. A simplified example is below.
        >
        #include <stdio.h>
        >
        main()
        {
        int a;
        if (1 == 1) a = 1;
        {
        a = 2;
        }
        printf("a = %d\n",a);
        }
        >
        When run it will print "a = 2". Should it compile at all or is GCCs
        parser broken?
        It is entirely legal. A compound statement (braces containing other
        statements) is just another valid option where a statement is
        required.

        --
        Ben.

        Comment

        • Richard Heathfield

          #5
          Re: Legal C or bug in gcc

          Boltar said:
          By accident I came across a bug like this in some Linux code I'd
          written today. All that had happened is I forgot the "else" yet the
          code still compiled and ran. A simplified example is below.
          >
          #include <stdio.h>
          >
          main()
          {
          int a;
          if (1 == 1) a = 1;
          {
          a = 2;
          }
          printf("a = %d\n",a);
          }
          >
          When run it will print "a = 2". Should it compile at all or is GCCs
          parser broken?
          It's legal.

          {
          a = 2;
          }

          is a compound statement. You can have these pretty much anywhere you can
          have an ordinary statement. If you like, you can do this:

          #include <stdio.h>
          int main(void)
          {
          { printf("Hello, world!\n"); }
          { puts("How are you today?"); }
          { puts("Earthquak e in UK - film at 11"); }
          { return 0; }
          }

          Although the above is a pastiche, this facility is nevertheless useful and
          powerful, but does have the unfortunate consequence you have noted -
          forgetting an "else" doesn't necessarily give you the syntax error you'd
          have hoped for!

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • Richard Heathfield

            #6
            Re: Legal C or bug in gcc

            Boltar said:
            On 27 Feb, 09:44, gaze...@xmissio n.xmission.com (Kenny McCormack)
            wrote:
            >Time for me to re-post my "I wrote this hello, world program and it
            >compiled and ran just fine; why?" post.
            >
            I should've known better than to expect a sensible answer from this
            group.
            Mr McCormack is a troll. You get them in any group.

            I have already posted a sensible answer.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • santosh

              #7
              Re: Legal C or bug in gcc

              Boltar wrote:
              By accident I came across a bug like this in some Linux code I'd
              written today. All that had happened is I forgot the "else" yet the
              code still compiled and ran. A simplified example is below.
              >
              #include <stdio.h>
              >
              main()
              {
              int a;
              if (1 == 1) a = 1;
              {
              a = 2;
              }
              printf("a = %d\n",a);
              }
              >
              When run it will print "a = 2". Should it compile at all or is GCCs
              parser broken?
              No this is legal C. A opening { starts a compound statement or block,
              closed by the next }. It's sometimes useful for data confinement.

              <OT>
              There *is* a related GCC extension which allows a compound statement
              enclosed in parenthesis as an expression, but this is not an
              illustration of one. This is pure Standard C.
              </OT>

              Comment

              • Richard Tobin

                #8
                Re: Legal C or bug in gcc

                In article <a28f28eb-52b0-4fd3-b045-4b29f46161b3@o7 7g2000hsf.googl egroups.com>,
                Boltar <boltar2003@yah oo.co.ukwrote:
                >By accident I came across a bug like this in some Linux code I'd
                >main()
                >{
                int a;
                if (1 == 1) a = 1;
                {
                a = 2;
                }
                printf("a = %d\n",a);
                >}
                Perhaps you should run it through an indenting tool for enlightenment:

                main()
                {
                int a;

                if (1 == 1)
                a = 1;

                {
                a = 2;
                }

                printf("a = %d\n",a);
                }

                -- Richard
                --
                :wq

                Comment

                • Kenneth Brody

                  #9
                  Re: Legal C or bug in gcc

                  Richard Heathfield wrote:
                  >
                  Boltar said:
                  >
                  By accident I came across a bug like this in some Linux code I'd
                  written today. All that had happened is I forgot the "else" yet the
                  code still compiled and ran. A simplified example is below.

                  #include <stdio.h>

                  main()
                  {
                  int a;
                  if (1 == 1) a = 1;
                  {
                  a = 2;
                  }
                  printf("a = %d\n",a);
                  }

                  When run it will print "a = 2". Should it compile at all or is GCCs
                  parser broken?
                  >
                  It's legal.
                  >
                  {
                  a = 2;
                  }
                  >
                  is a compound statement. You can have these pretty much anywhere you can
                  have an ordinary statement. If you like, you can do this:
                  >
                  #include <stdio.h>
                  int main(void)
                  {
                  { printf("Hello, world!\n"); }
                  { puts("How are you today?"); }
                  { puts("Earthquak e in UK - film at 11"); }
                  { return 0; }
                  }
                  >
                  Although the above is a pastiche, this facility is nevertheless useful and
                  powerful, but does have the unfortunate consequence you have noted -
                  forgetting an "else" doesn't necessarily give you the syntax error you'd
                  have hoped for!
                  In case the OP (or anyone else) is wondering where this would be
                  useful, consider:

                  #if ENABLE_SOME_FEA TURE
                  if ( new_feature_is_ enabled() )
                  {
                  do_it_the_new_w ay();
                  do_another_thin g_the_new_way() ;
                  }
                  else
                  #endif
                  {
                  do_it_the_old_w ay();
                  do_the_other_th ing_the_old_way ();
                  }

                  Compare this "clean" version to how you would have to write it if
                  compound statements weren't legal on their own.

                  --
                  +-------------------------+--------------------+-----------------------+
                  | Kenneth J. Brody | www.hvcomputer.com | #include |
                  | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                  +-------------------------+--------------------+-----------------------+
                  Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                  Comment

                  • Boltar

                    #10
                    Re: Legal C or bug in gcc

                    On 27 Feb, 09:56, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    I have already posted a sensible answer.
                    Yes , thanks for that. I realised what it was happening with the code
                    about 10 seconds after I posted. That'll teach me to post when I'm
                    half asleep instead of attempting to think.

                    B2003

                    Comment

                    • CBFalconer

                      #11
                      Re: Legal C or bug in gcc

                      Richard Heathfield wrote:
                      >
                      .... snip ...
                      >
                      Although the above is a pastiche, this facility is nevertheless
                      useful and powerful, but does have the unfortunate consequence
                      you have noted - forgetting an "else" doesn't necessarily give
                      you the syntax error you'd have hoped for!
                      Which is where the Pascal syntax of never preceding an else with a
                      semi is useful.

                      --
                      [mail]: Chuck F (cbfalconer at maineline dot net)
                      [page]: <http://cbfalconer.home .att.net>
                      Try the download section.



                      --
                      Posted via a free Usenet account from http://www.teranews.com

                      Comment

                      • Keith Thompson

                        #12
                        Re: Legal C or bug in gcc

                        Kenneth Brody <kenbrody@spamc op.netwrites:
                        [...]
                        In case the OP (or anyone else) is wondering where this would be
                        useful, consider:
                        >
                        #if ENABLE_SOME_FEA TURE
                        if ( new_feature_is_ enabled() )
                        {
                        do_it_the_new_w ay();
                        do_another_thin g_the_new_way() ;
                        }
                        else
                        #endif
                        {
                        do_it_the_old_w ay();
                        do_the_other_th ing_the_old_way ();
                        }
                        >
                        Compare this "clean" version to how you would have to write it if
                        compound statements weren't legal on their own.
                        It's not *that* bad:

                        #if ENABLE_SOME_FEA TURE
                        if ( new_feature_is_ enabled() )
                        {
                        do_it_the_new_w ay();
                        do_another_thin g_the_new_way() ;
                        }
                        else
                        {
                        #endif
                        do_it_the_old_w ay();
                        do_the_other_th ing_the_old_way ();
                        #if ENABLE_SOME_FEA TURE
                        }
                        #endif

                        It also has the advantage that if you delete all the
                        #if ENABLE_SOME_FEA TURE
                        ...
                        #endif
                        sections, the remaining code still looks ok (apart from the
                        indentation).

                        --
                        Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                        Nokia
                        "We must do something. This is something. Therefore, we must do this."
                        -- Antony Jay and Jonathan Lynn, "Yes Minister"

                        Comment

                        • Keith Thompson

                          #13
                          Re: Legal C or bug in gcc

                          Boltar <boltar2003@yah oo.co.ukwrites:
                          By accident I came across a bug like this in some Linux code I'd
                          written today. All that had happened is I forgot the "else" yet the
                          code still compiled and ran. A simplified example is below.
                          >
                          #include <stdio.h>
                          >
                          main()
                          {
                          int a;
                          if (1 == 1) a = 1;
                          {
                          a = 2;
                          }
                          printf("a = %d\n",a);
                          }
                          >
                          When run it will print "a = 2". Should it compile at all or is GCCs
                          parser broken?
                          Your question has already been answered to death (and BTW "Kenny
                          McCormack" is at least annoying to us as he is to you), but let me
                          offer a suggestion that can avoid such problems.

                          Whenever I write an if, while, for, or do-while statement, I always
                          use blocks for the controlled statements. For example:

                          if (1 == 1) {
                          a = 1;
                          }
                          else {
                          a = 2;
                          }

                          It's a habit I picked up from Perl, which requires it, but I find that
                          it's useful in C as well.

                          Strictly speaking you'll still have the same problem if you drop the
                          "else", but with a more uniform layout I think you're less liketly to
                          do so. This style also avoids problems like:

                          if (condition)
                          statement;
                          another_stateme nt_added_later;

                          Of course, plenty of smart people don't like this style.

                          --
                          Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                          Nokia
                          "We must do something. This is something. Therefore, we must do this."
                          -- Antony Jay and Jonathan Lynn, "Yes Minister"

                          Comment

                          • Kenneth Brody

                            #14
                            Re: Legal C or bug in gcc

                            Keith Thompson wrote:
                            >
                            Kenneth Brody <kenbrody@spamc op.netwrites:
                            [...]
                            In case the OP (or anyone else) is wondering where this would be
                            useful, consider:

                            #if ENABLE_SOME_FEA TURE
                            if ( new_feature_is_ enabled() )
                            {
                            do_it_the_new_w ay();
                            do_another_thin g_the_new_way() ;
                            }
                            else
                            #endif
                            {
                            do_it_the_old_w ay();
                            do_the_other_th ing_the_old_way ();
                            }

                            Compare this "clean" version to how you would have to write it if
                            compound statements weren't legal on their own.
                            >
                            It's not *that* bad:
                            >
                            #if ENABLE_SOME_FEA TURE
                            if ( new_feature_is_ enabled() )
                            {
                            do_it_the_new_w ay();
                            do_another_thin g_the_new_way() ;
                            }
                            else
                            {
                            #endif
                            do_it_the_old_w ay();
                            do_the_other_th ing_the_old_way ();
                            #if ENABLE_SOME_FEA TURE
                            }
                            #endif
                            I've seen code like that, and it looks "ugly" to me. Plus, I think
                            mine is earier to maintain. Consider what happens with you start to
                            #define ENABLE_ANOTHER_ FEATURE and ENABLE_YET_ANOT HER_FEATURE, and
                            start having multiple if/elseif's along the way.

                            It also has the advantage that if you delete all the
                            #if ENABLE_SOME_FEA TURE
                            ...
                            #endif
                            sections, the remaining code still looks ok (apart from the
                            indentation).
                            Then what about debugging code, with local variables?

                            #if DO_MY_LOGGING
                            {
                            int i;
                            for ( i=0 ; i < LengthOfSomethi ng ; i++ )
                            {
                            DoMyLogging(som ething[i]);
                            }
                            }
                            #endif

                            --
                            +-------------------------+--------------------+-----------------------+
                            | Kenneth J. Brody | www.hvcomputer.com | #include |
                            | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                            +-------------------------+--------------------+-----------------------+
                            Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

                            Comment

                            • MisterE

                              #15
                              Re: Legal C or bug in gcc

                              if (1 == 1) a = 1;
                              The semicolon brings the if to an end.


                              Comment

                              Working...