VLA and goto -- diagnostic required?

  • Thread starter Man with Oscilloscope
  • Start date
M

Man with Oscilloscope

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<---------------------------------------------------------------
 
J

jacob navia

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
 
J

jacob navia

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
 
R

Robert Gamble

Man said:
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
 
J

jacob navia

CBFalconer said:
jacob said:
(e-mail address removed) wrote:

if(foo)
goto bar;

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top