public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* GCC C bug: sizeof a union of structs returns zero value
@ 2004-12-17  2:54 Hugh Daniel
  2004-12-17  4:19 ` Daniel Berlin
  2004-12-17  9:04 ` Richard Henderson
  0 siblings, 2 replies; 4+ messages in thread
From: Hugh Daniel @ 2004-12-17  2:54 UTC (permalink / raw)
  To: gcc-bugs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

  Note, I gave up on GNATS after repeatedly getting this error message
no matter what I did to the text:

"""
You have not described how to repeat the bug
You have not defined a category for the bug
"""

  If there is a maintainer of the <gcc-gnats@gcc.gnu.org> bot I would
be happy to help debug the problem with your script.

		||ugh


Submitter-Id:	net
Originator:	Hugh Daniel
Organization:	Xelerance Corporation
Confidential:	no
Synopsis:	sizeof a union of structs returns 0, yeilding bogus behavior
Severity:	non-critical
Priority:	low
Category: c
Class:		sw-bug
Release:	3.4.3
Environment:	Linux, Solarus, MacOS X
System:		Linux *.toad.com 2.6.9 #1 Thu Nov 11 23:34:13 PST 2004 i686 i686 i386 BSD/GNU/Linux
Architecture:	i686
host:		i686-pc-linux-gnu
build:		i686-pc-linux-gnu
target:		i686-pc-linux-gnu
configured with: ../gcc-3.4.3/configure
Description:
	  Short version:  the sizeof a union of structs returns zero
	in most recent GCC's.  Older GCC's return valid sizes.
	 
	  In porting some network code to SPARC I noticed an odd
	warning message (warning: declaration does not declare
	anything) which in the original context suggested something
	was wrong.  After some investigation it turned out that an
	array was being created but with zero size due to a sizeof
	returning zero, though none of this was clear from the warning
	message.

	  The size of the array was set by a sizeof a union of two
	structures (in_addr4 and in_addr6...).  In some gcc's a
	reasonable value is returned and in others a very unreasonable
	value of zero is returned.  A static array of size zero is
	very likely to cause bad things to happen when assigned to...

	  There is a 4 line C code example of the failure below.

	  I have tested this on 5 hosts with 8 versions of GCC and
	find a mixure of working and non working systems.  It seems
	that around about 3.3 something changed in GCC such that
	taking the sizeof a union of structs returns zero.  I would
	have expected it to return the largest size of the unions
	members (including any padding the architecture might
	require).

	  Note that telling folks to use a max function based on the
	?: construct will start breaking down when there are three or
	more items in the union, so while that will sove MY problem
	today there still a bug in gcc.

	  I suspect this could be a common problem as taking the
	sizeof a union is not uncommon and resulting problems will be
	of the memory corruption form, often quite hard to find.

	  Please reference section 6.5.3.4, paragraph #3 of "August 3,
	1998" (N843) ISO draft C standard to see why I believe this
	is a bug.  I have found nothing in the NEWS, Changlogs
	etc. that clearly states that there is a reason for the
	current broken behavior.

	  Fails on these gcc versions (host arch):
        powerpc-apple-darwin8-gcc-4.0.0 (GCC) 4.0.0 20041026 (Apple Computer, Inc. build 4023)
	gcc (GCC) 3.4.3 (sun4u)
	gcc (GCC) 3.4.3 (i686)
	gcc-3.4 (GCC) 3.4.2 (Debian 3.4.2-2) (SPARC)
	gcc (GCC) 3.3.4 (Debian 1:3.3.4-13) (SPARC)
	gcc (GCC) 3.3 20030304 (Apple Computer, Inc. build 1666) (PPC)

	  Works:
	gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5) (i686)
	gcc (GCC) 3.2.1 20030202 (Red Hat Linux 8.0 3.2.1-7) (i686)
	gcc 2.96 (i686)

How-To-Repeat: gcc sizeof_fails.c
	  Here is the simplest test case to show and repeat the bug:
"""
cat > sizeof_fails.c <<EOF
struct aaa { int aaa_int; };
struct bbb { char bbb_char; };
char ccc[ sizeof( union{ struct aaa; struct bbb; })];
int main(){ return 0; }
EOF
gcc sizeof_fails.c
./a.out
"""

	  Here is a slightly more verbose test case:
"""
cat > sizeof_fails.c <<EOF
#include <stdio.h>
struct aaa { int aaa_int; };
struct bbb { char bbb_char; };
char ccc[ sizeof( union{ struct aaa; struct bbb; })];
int main(){
  printf("addr of ccc 0x%x\n", ccc);
  printf("size of ccc 0x%x\n", (unsigned int) sizeof(ccc));
  printf("unin of ccc 0x%x\n", (unsigned int) sizeof( union {struct aaa; struct bbb;}));
  printf("sily of ccc 0x%x\n", (unsigned int) sizeof( union silly {struct aaa; struct bbb;}));
}
EOF
gcc sizeof_fails.c
./a.out
"""
	  Here are examples of the aboves output:
	Works (i686):
"""
$ /usr/bin/gcc sizeof_fails.c
$ ./a.out
addr of ccc 0x8049584
size of ccc 0x4
unin of ccc 0x4
sily of ccc 0x4
$ /usr/bin/gcc --version
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
"""

	Fails (sun4u):
"""
$ gcc sizeof_fails.c
sizeof_fails.c:4: warning: declaration does not declare anything
sizeof_fails.c:4: warning: declaration does not declare anything
sizeof_fails.c: In function `main':
sizeof_fails.c:8: warning: declaration does not declare anything
sizeof_fails.c:8: warning: declaration does not declare anything
sizeof_fails.c:9: warning: declaration does not declare anything
sizeof_fails.c:9: warning: declaration does not declare anything
$ ./a.out
addr of ccc 0x209a8
size of ccc 0x0
unin of ccc 0x0
sily of ccc 0x0
$ gcc --version
gcc (GCC) 3.4.3
"""

Fix:
	Unknown to me.
	At least re-write the error message so that we know what
	construct on that line is not declaring 'anything'.  I had
	to guess and explore to figure out what was wrong.
	
-----BEGIN PGP SIGNATURE-----
Comment: For the matching public key, finger the Reply-To: address.

iQEVAwUBQcJJ6Qair3ZYNYS4AQJsyAgAyseDerUfxEeklqHG2SY+ncxNLOclS7Uz
knGeLlgTuMMe92ZQCl6zdsSKxmTqW+n4GeyWhVgMG1g//S+RONwTW0RWeUZXJBTv
LBc6DT9oWfi/rYj+KLP+1bYCDP1IMMHMjuYw82t/udFjpKJY7nfqUYq9nhjAjZ9g
eg+Uq/ZY2FbiPYMVBIN1ks0xlfoASIC0CUnPfgzsJ+A21qLWEX98jxVnEvjJKvVj
DKKsBo3nZWlQqpItJtK4SuJC3SDAqqvZV1L1BPUDhfi4nVtbnuJPC4F3rfbhTznU
WYQ3D58pKTyNcfoTJRjcA5PkQbkiDfeZGcNTzr79sglD5cKaEUzZDg==
=aovX
-----END PGP SIGNATURE-----


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

* Re: GCC C bug: sizeof a union of structs returns zero value
  2004-12-17  2:54 GCC C bug: sizeof a union of structs returns zero value Hugh Daniel
@ 2004-12-17  4:19 ` Daniel Berlin
  2004-12-17  9:04 ` Richard Henderson
  1 sibling, 0 replies; 4+ messages in thread
From: Daniel Berlin @ 2004-12-17  4:19 UTC (permalink / raw)
  To: hugh; +Cc: gcc-bugs, gerald



On Thu, 16 Dec 2004, Hugh Daniel wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>  Note, I gave up on GNATS after repeatedly getting this error message
> no matter what I did to the text:
>
> """
> You have not described how to repeat the bug
> You have not defined a category for the bug
> """
>
>  If there is a maintainer of the <gcc-gnats@gcc.gnu.org> bot I would
> be happy to help debug the problem with your script.

If you can pass me the full raw email message you sent to the script 
(including headers, etc), i'm  happy to try to debug it.

Note that the gcc-gnats@gcc.gnu.org is (or should be) deprecated.
The bug reporting instructions will point you to report bugs using our 
bugzilla system now.

The gcc-gnats script is only really to handle the occasional gcc-gnats 
email that comes in.

--Dan


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

* Re: GCC C bug: sizeof a union of structs returns zero value
  2004-12-17  2:54 GCC C bug: sizeof a union of structs returns zero value Hugh Daniel
  2004-12-17  4:19 ` Daniel Berlin
@ 2004-12-17  9:04 ` Richard Henderson
  1 sibling, 0 replies; 4+ messages in thread
From: Richard Henderson @ 2004-12-17  9:04 UTC (permalink / raw)
  To: hugh; +Cc: gcc-bugs

On Thu, Dec 16, 2004 at 06:52:35PM -0800, Hugh Daniel wrote:
> char ccc[ sizeof( union{ struct aaa; struct bbb; })];

In ISO Standard C, this doesn't do what you think it does.

What you are attempting to use is an ill-conceived Microsoft
extension.  You can enable *some* amount of support for this
in gcc by using -fms-extensions.


r~


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

* Re: GCC C bug: sizeof a union of structs returns zero value
@ 2004-12-17 13:46 Hugh Daniel
  0 siblings, 0 replies; 4+ messages in thread
From: Hugh Daniel @ 2004-12-17 13:46 UTC (permalink / raw)
  To: gcc-bugs; +Cc: jcr, mcr, hugh, pleasant, dberlin

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

  My GCC sizeof a union of structs bug seems to be  pilot error after
all... 

  So Hugh Redelmeier found the actual bug when we were looking at the
various details of memory allocation: we were not declaring any struct
identifiers to take the size of.  This is the solution:

char ccc[ sizeof( union{ struct aaa; struct bbb; })];
char ccc[ sizeof( union{ struct aaa          ; struct bbb              ; })];
char ccc[ sizeof( union{ struct aaa Ignore_Me; struct bbb Ignore_Me_Too; })];
                                    ^^^^^^^^^             ^^^^^^^^^^^^^

  One might argue that Draft n2794.txt "6.7 Declarations" paragraphs
#2 and #6 are at odds in this case, one saying the lack of an
identifier is an error and the other saying it's optional.  I leave
this for you to decide.

  I will point out that the behavior of the C compiler has changed and
that this should be documented.

  Thanks for GCC folks.

		||ugh Daniel

-----BEGIN PGP SIGNATURE-----
Comment: For the matching public key, finger the Reply-To: address.

iQCVAwUBQcLiZ1ZpdJR7FBQRAQKcmQQA67Lx2VmQ3/A6iBhTBVHFxPWQtzcNntg1
R8pZNawG1TmwbklELSl9WGWJP6v9wrUeqvdMzLTDhxQWFJACg972ExSFdUGmCg6+
nq21wDDFiMikTimYflM/XVSa0WV6ZXZHEHwP7gKe9bqOBmTsEuFWGmbsrj7H76X3
jjJHkpL9qpM=
=4XbY
-----END PGP SIGNATURE-----


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

end of thread, other threads:[~2004-12-17 13:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-17  2:54 GCC C bug: sizeof a union of structs returns zero value Hugh Daniel
2004-12-17  4:19 ` Daniel Berlin
2004-12-17  9:04 ` Richard Henderson
2004-12-17 13:46 Hugh Daniel

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