public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* What does zero-length array mean at file scope?
@ 2009-05-24  9:07 Dave Korn
  2009-05-24  9:21 ` Dave Korn
  2009-05-28 22:45 ` Joseph S. Myers
  0 siblings, 2 replies; 16+ messages in thread
From: Dave Korn @ 2009-05-24  9:07 UTC (permalink / raw)
  To: gcc


    Hi everyone,

  I found something rather odd in testcase gcc.c-torture/execute/20030811-1.c:

> /* Origin: PR target/11535 from H. J. Lu <hjl@lucon.org> */
> 
> void vararg (int i, ...)
> {
>   (void) i;
> }
> 
> int i0[0], i1;

  Huh?

> void test1 (void)
> {
>   int a = (int) (long long) __builtin_return_address (0);
>   vararg (0, a);
> }
> 
> void test2 (void)
> {
>   i0[0] = (int) (long long) __builtin_return_address (0);
> }

  Nasal demons?

> void test3 (void)
> {
>   i1 = (int) (long long) __builtin_return_address (0);
> }
> 
> void test4 (void)
> {
>   volatile long long a = (long long) __builtin_return_address (0);
>   i0[0] = (int) a;
> }

  And again.

  ELF GAS/LD seem happy enough when presented with a ".comm foo,0" directive,
but PE does rather literally what you asked, and gives you no data object,
leading to i0 in the above being an undefined reference at link time.

  What is supposed to happen here?  How much space is the compiler meant to
allocate to i0?  Where does the data go when it gets stored to i0[0]?

  I've read http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html about six times
and can't see anywhere it even hints that you can use this syntax anywhere
except as the trailing member of a struct.  When I look at the original
PR11535, the testcases in there are all similar but have array size [1] and
try to assign the builtin_return_address result to array[0] - that's clearly
valid, and quite different from how this testcase ended up.

  So, is the testcase invalid, and should the compiler be warning about this
declaration?  I couldn't get any complaint out of it even with "-W -Wall
-Wextra -pedantic -std=c89"?  Or should the linker be allocating some space
for this zero-sized common?

    cheers,
      DaveK

^ permalink raw reply	[flat|nested] 16+ messages in thread
* Re: What does zero-length array mean at file scope?
@ 2009-05-24  9:47 Uros Bizjak
  0 siblings, 0 replies; 16+ messages in thread
From: Uros Bizjak @ 2009-05-24  9:47 UTC (permalink / raw)
  To: GCC; +Cc: Dave Korn, H.J. Lu

Hello!

>   I found something rather odd in testcase gcc.c-torture/execute/20030811-1.c:
>
> > /* Origin: PR target/11535 from H. J. Lu <hjl@lucon.org> */
> > 
> > void vararg (int i, ...)
> > {
> >   (void) i;
> > }
> > 
> > int i0[0], i1;
>
>   Huh?
>
> > void test1 (void)
> > {
> >   int a = (int) (long long) __builtin_return_address (0);
> >   vararg (0, a);
> > }
> > 
> > void test2 (void)
> > {
> >   i0[0] = (int) (long long) __builtin_return_address (0);
> > }
>
>   Nasal demons?
>
> > void test3 (void)
> > {
> >   i1 = (int) (long long) __builtin_return_address (0);
> > }
> > 
> > void test4 (void)
> > {
> >   volatile long long a = (long long) __builtin_return_address (0);
> >   i0[0] = (int) a;
> > }
>
>   
>   And testing it on Linux, I see that the linker goes ahead and assigns zero
> bytes of the common area to i0, then assigns four bytes of common space to i1
> at the same address, so writing to i0[0] aliases and alters i1.  That
> certainly sounds like undefined behaviour to me!
>   

It looks like a typo in the testcase to me, but let's add some CCs to be 
sure.

Uros.

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2009-05-29  9:04 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-24  9:07 What does zero-length array mean at file scope? Dave Korn
2009-05-24  9:21 ` Dave Korn
2009-05-24  9:26   ` Dave Korn
2009-05-24 10:34     ` Andrew Haley
2009-05-24 10:57       ` Dave Korn
2009-05-24 12:03         ` Andrew Haley
2009-05-24 12:29           ` Dave Korn
2009-05-24 12:41             ` Dave Korn
2009-05-24 20:57               ` Andrew Haley
2009-05-24 22:51                 ` Dave Korn
2009-05-28 22:48           ` Joseph S. Myers
2009-05-28 23:18             ` Dave Korn
2009-05-29 10:17             ` Andrew Haley
2009-05-28 22:45 ` Joseph S. Myers
2009-05-29  9:04   ` Dave Korn
2009-05-24  9:47 Uros Bizjak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).