Re: VLA and goto -- diagnostic required?



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

.



Relevant Pages

  • Re: no declaration inside for loop header
    ... No, it's fine in C99. ... that you are invoking your compiler in a non-C99 mode. ... The scope of your 'counter' object is the loop header and loop body: ...
    (comp.lang.c)
  • Re: C# equivalent to a smart pointer???
    ... The basic idea is that the compiler may detect ... It showed up in some sample code in the documentation. ... The issue is that the lifetime of a local variable is NOT determined by ... within the same scope if it's not used in any code past a certain point. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Implementation of control structures
    ... the innermost lexical scope for is destroyed, ... Also, if you have an interpreter instead of a compiler, you may have ... you must unwind the scope up to the block ...
    (comp.lang.lisp)
  • compile help please
    ... I'm trying to compile tesseract from svn so I can do ocr. ... checking for C++ compiler default output file name... ... checking for ranlib... ... scrollview.cpp:63: error: 'strncpy' was not declared in this scope ...
    (Debian-User)
  • Re: derangement: coding review request
    ... good tmp in scope, the compiler will complain, just as it complains if ... there is no good buys_for in scope. ... Eliminating the tmp parameter requires a very handy GNU C extension, ... The only valid objection to the original form is that C99 deprecates it. ...
    (comp.lang.c)