Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > VLA and goto -- diagnostic required?

Reply
Thread Tools

VLA and goto -- diagnostic required?

 
 
Man with Oscilloscope
Guest
Posts: n/a
 
      08-24-2006

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

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      08-24-2006
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

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      08-24-2006
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
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      08-24-2006
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. 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

 
Reply With Quote
 
lanius@linuxmail.org
Guest
Posts: n/a
 
      08-30-2006
Quote:
if(foo)
goto bar;
LOL!!!

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      08-30-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
Quote:
> if(foo)
> goto bar;
>
>
> LOL!!!
>


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

Look Out Lanius...




 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-30-2006
jacob navia wrote:
> (E-Mail Removed) wrote:
>
>>
Quote:
>> if(foo)
>> goto bar;
>>
>>
>> LOL!!!

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


Laughing out loud.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE maineline address!

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      08-30-2006
CBFalconer wrote:
> jacob navia wrote:
>
>>(E-Mail Removed) wrote:
>>
>>
>>>
Quote:
>>> 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
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      08-30-2006
jacob navia <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Routing Between Two VLA Ns Bob Simon Cisco 5 02-06-2007 01:15 AM
VLA Question pemo C Programming 8 02-24-2006 01:04 PM
support of C99 VLA in compilers Ben Hinkle C Programming 6 12-15-2005 02:08 PM
Compound literals and VLA's William Ahern C Programming 6 08-24-2005 05:40 AM
[C99] local VLA with dimension given by a global var? MackS C Programming 15 02-21-2005 07:59 PM



Advertisments