VLA and goto -- diagnostic required?

Discussion in 'C Programming' started by Man with Oscilloscope, Aug 24, 2006.

  1. goto jumping over vla -- diagnostic required?

    This is a question about C99, 6.8.6.1, example 2 (see test below).
    I'm currently working on updating an older compiler up to C99. The
    standard is very clear about jumping into and out of the middle of
    a block declaring variably modified types. The example below,
    however, just jumps over the actual VLA declaration, within the
    /same/ block. A quick test with two different compilers claiming
    (at least partial) C99 conformance reveals...

    The lcc-win32 compiler (tested with version 3.8) accepts it without
    any diagnostics, but generates unusable code (more specifically,
    the `goto' jumps past the instructions necessary to set up space
    for `vla[]').

    Gcc (all version with (partial) C99 support), on the other hand,
    reject the example with a hard error.

    What is the best course of action here? Is this an example of
    "everone who writes such code deserved what they get", or is a
    strictly conforming compiler required to reject it?

    --8<---------------------------------------------------------------
    /*
    * vlatest.c
    *
    * gcc -Wall -W -O2 -std=c99 vlatest.c -o vlatest
    * ("vlatest.c:26: error: label `bar' used before \
    * containing binding contour")
    *
    * lc -A -O -ansi -unused vlatest.c
    * (no diagnostic printed, invalid executable generated, BOOM!)
    */

    #include <stdio.h>
    #include <string.h>

    int foo = 1;

    void vlatest(size_t size)
    {
    printf("vlatest...\n");

    if(foo)
    goto bar;

    int vla[size];

    bar:
    memset(vla, 0xCC, sizeof vla);
    printf("BAMM!!!\n");
    }


    int main(void)
    {
    vlatest(1024);
    return 0;
    }
    --8<---------------------------------------------------------------

    --
    mwo, Researcher
     
    Man with Oscilloscope, Aug 24, 2006
    #1
    1. Advertising

  2. Man with Oscilloscope

    jacob navia Guest

    What the lcc-win32 compiler is concerned there is no BAAM at all.

    It crashes without ever arriving at your printf("BAMM");
    statement :)

    The reason is that I store the size of the array together with its
    initialization in a local hidden variable that I setup at the moment
    of the allocation of the stack space. Since you have skipped
    the initialization of the sizeof variable, you are pushing a semi
    random value into the memset... what makes probably for a big
    stack overflow. Since stack overflow is not catched in the
    runtime (I can't even make some space for the printing of some
    abort message) the program ends abruptly...

    I hope this is correct :)

    I am not a language lawyer, and in lcc-win32 implementation not all
    possible errors are catched.

    Now that you have done this, maybe I will find time I will try to catch
    this. I have to:

    1) In all functions that use VLA
    2) Test if there is a goto statement that could potentially
    skip a VLA initialization routine.

    That point (2) is not at all evident to catch without a full blown
    flow control analysis, what lcc-win32 doesn't have.

    I have tried till now to keep the compiler simple. I will see how can
    I do this in some evident cases as the one you posted but the general
    case would be too expensive (in developing time and in compiler
    complexity). Please compare the gcc team (more than 30-40 people
    full time, with big ncompanies like IBM behind it) and the
    lcc-win32 team of just two people.

    jacob
     
    jacob navia, Aug 25, 2006
    #2
    1. Advertising

  3. Man with Oscilloscope

    jacob navia Guest

    The standard does not specify that a diagnostic is required in this
    case.

    The wording is:

    EXAMPLE 2 A goto statement is not allowed to jump past any declarations
    of objects with variably modified types. A jump within the scope,
    however, is permitted.


    goto lab3; // invalid: going INTO scope of VLA.
    {
    double a[n];
    a[j] = 4.4;
    lab3:
    a[j] = 3.3;
    goto lab4; // valid: going WITHIN scope of VLA.
    a[j] = 5.5;
    lab4:
    a[j] = 6.6;
    }
    goto lab4; // invalid: going INTO scope of VLA.

    Maybe a language lawyer could help us here. If a diagnostic is required
    I will issue a diagnostic of course and this is a bug in lcc-win32 that
    will be corrected no matter what.

    jacob
     
    jacob navia, Aug 25, 2006
    #3
  4. Man with Oscilloscope wrote:
    > goto jumping over vla -- diagnostic required?
    >
    > This is a question about C99, 6.8.6.1, example 2 (see test below).
    > I'm currently working on updating an older compiler up to C99. The
    > standard is very clear about jumping into and out of the middle of
    > a block declaring variably modified types. The example below,
    > however, just jumps over the actual VLA declaration, within the
    > /same/ block. A quick test with two different compilers claiming
    > (at least partial) C99 conformance reveals...
    >
    > The lcc-win32 compiler (tested with version 3.8) accepts it without
    > any diagnostics, but generates unusable code (more specifically,
    > the `goto' jumps past the instructions necessary to set up space
    > for `vla[]').
    >
    > Gcc (all version with (partial) C99 support), on the other hand,
    > reject the example with a hard error.
    >
    > What is the best course of action here? Is this an example of
    > "everone who writes such code deserved what they get", or is a
    > strictly conforming compiler required to reject it?
    >
    > --8<---------------------------------------------------------------
    > /*
    > * vlatest.c
    > *
    > * gcc -Wall -W -O2 -std=c99 vlatest.c -o vlatest
    > * ("vlatest.c:26: error: label `bar' used before \
    > * containing binding contour")
    > *
    > * lc -A -O -ansi -unused vlatest.c
    > * (no diagnostic printed, invalid executable generated, BOOM!)
    > */
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int foo = 1;
    >
    > void vlatest(size_t size)
    > {
    > printf("vlatest...\n");
    >
    > if(foo)
    > goto bar;
    >
    > int vla[size];
    >
    > bar:
    > memset(vla, 0xCC, sizeof vla);
    > printf("BAMM!!!\n");
    > }
    >
    >
    > int main(void)
    > {
    > vlatest(1024);
    > return 0;
    > }


    Here is the relevant constraint from the Standard:
    "A goto statement shall not jump from outside the scope of an
    identifier having a variably modified type to inside the scope of that
    identifier."

    Note carefully the wording: "from outside the scope ... to inside the
    scope". The scope of vla begins immediately after its declaration and
    ends at the end of the function vlatest. Your example jumps from
    outside the scope of vla to inside the scope of vla. That is a
    constraint violation requiring the issue of a diagnostic.

    Robert Gamble
     
    Robert Gamble, Aug 25, 2006
    #4
  5. Man with Oscilloscope

    Guest

    LOL!!!
     
    , Aug 30, 2006
    #5
  6. Man with Oscilloscope

    jacob navia Guest

    wrote:
    >
    >
    > LOL!!!
    >


    I have always wondered what LOL stands for.
    And what does it do in this context.

    Look Out Lanius... :)
     
    jacob navia, Aug 30, 2006
    #6
  7. Man with Oscilloscope

    CBFalconer Guest

    jacob navia wrote:
    > wrote:
    >
    >>
    >>
    >> LOL!!!

    >
    > I have always wondered what LOL stands for.
    > And what does it do in this context.


    Laughing out loud.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE maineline address!
     
    CBFalconer, Aug 30, 2006
    #7
  8. Man with Oscilloscope

    jacob navia Guest

    CBFalconer wrote:
    > jacob navia wrote:
    >
    >> wrote:
    >>
    >>
    >>>
    >>>
    >>> LOL!!!

    >>
    >>I have always wondered what LOL stands for.
    >>And what does it do in this context.

    >
    >
    > Laughing out loud.
    >


    Ahhhh


    Thanks Chuck, that WAS helpful.

    My daughter told me she uses it but she could not tell me
    what that stands for.

    Nor my son, nor several other teens I asked. Most of them
    know what it "means" and know how to use it but none
    could tell me where that came from or what that means.

    One of the teens told me it is better to use the french
    MDR (Mort De Rire or "laughing to death" but those sentences are
    untranslateable anyway)

    jacob
     
    jacob navia, Aug 30, 2006
    #8
  9. jacob navia <> wrote:

    > Thanks Chuck, that WAS helpful.


    Perhaps you might also find this useful:

    http://www.answers.com/topic/lol-internet-slang

    I would claim to be LMAO, but that's not realistic in terms of office
    decorum.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Aug 30, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher Benson-Manica

    Diagnostic required?

    Christopher Benson-Manica, Oct 10, 2004, in forum: C++
    Replies:
    2
    Views:
    403
    Christopher Benson-Manica
    Oct 11, 2004
  2. MackS
    Replies:
    15
    Views:
    621
    William Ahern
    Feb 21, 2005
  3. William Ahern

    Compound literals and VLA's

    William Ahern, Aug 24, 2005, in forum: C Programming
    Replies:
    6
    Views:
    725
    Robert Gamble
    Aug 24, 2005
  4. Ben Hinkle

    support of C99 VLA in compilers

    Ben Hinkle, Dec 13, 2005, in forum: C Programming
    Replies:
    6
    Views:
    720
    Jordan Abel
    Dec 15, 2005
  5. Old Wolf
    Replies:
    11
    Views:
    541
    Dik T. Winter
    Mar 6, 2007
Loading...

Share This Page