473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

VLA and goto -- diagnostic required?


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

Aug 24 '06 #1
8 2459
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

Aug 24 '06 #2
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
Aug 24 '06 #3
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

Aug 24 '06 #4
if(foo)
goto bar;
LOL!!!

Aug 30 '06 #5
la****@linuxmail.org wrote:
if(foo)
goto bar;

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

Look Out Lanius... :-)


Aug 30 '06 #6
jacob navia wrote:
la****@linuxmail.org 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.

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!

Aug 30 '06 #7
CBFalconer wrote:
jacob navia wrote:
>>la****@linuxmail.org 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
Aug 30 '06 #8
jacob navia <ja***@jacob.remcomp.frwrote:
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.
Aug 30 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christopher Benson-Manica | last post by:
Is a conforming C++ implementation required to issue a diagnostic when invoked on the following code? #include <cstdlib> #include <iostream> #include <string> int main() { printf( "Hello,...
45
by: Debashish Chakravarty | last post by:
K&R pg.66 describes two situations when using goto makes sense. Has anyone here come across situations where using goto provided the most elegant solution. --...
17
by: Mike Hofer | last post by:
While I'd toyed with C, C++, and Java over the last 20 years or so, my principal language has been BASIC, QBASIC, then Visual Basic, and finally Visual Basic .NET. But lately, I've been using C#...
34
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
2
by: miaohua1982 | last post by:
under the same warning level, the code is: class X{ public: X(){} virtual void g(){} }; void f(int i){ if(i<10) goto jump1;
41
by: p_cricket_guy | last post by:
Please see this test program: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <limits.h> 4 5 int main (void) 6 { 7 int y = -2147483648; 8 int x = INT_MIN;
11
by: Old Wolf | last post by:
Does the following program require a diagnostic? Which section of the Standard deals with this? (I read the section on function calls and didn't see anything). void f(void); int main(void) {...
11
by: =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= | last post by:
Hello, I have a question about the infamous GOTO statement and the way to return a result from a sub: I have a sub that has to make some calls to external COM methods, and because these...
5
by: santosh | last post by:
We are given the definition of a "diagnostic message" in 3.10 of the Standard. To quote: 3.10 1 diagnostic message message belonging to an implementation-defined subset of the implementation's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.