Bug 58451

Summary: gcc-2.96 misallocates autosized array of structs
Product: [Retired] Red Hat Linux Reporter: Mark Maimone <mwm>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED CURRENTRELEASE QA Contact: Brian Brock <bbrock>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.2   
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2002-12-15 19:08:45 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Mark Maimone 2002-01-16 23:19:11 UTC
From Bugzilla Helper:
User-Agent: Mozilla/4.72 [en] (X11; U; SunOS 5.7 sun4u)

Description of problem:
Vanilla Redhat-7.2 gcc-2.96 on a Dell Latitude C610 notebook
miscompiles an implicitly-sized array of structs when internal braces are
omitted in the initializer.  Instead it allocates too much space, zero pads it,
and causes sizeof() to return an improper response.


Version-Release number of selected component (if applicable):
2.96

How reproducible:
Always

Steps to Reproduce:
CREATE THE SOURCE FILE
> cat foo.c 
#include <stdio.h> 
 
typedef struct { 
  int i, char *s; 
} RecordT; 
 
RecordT foo[] = { 
  1, "1", 
  2, "2", 
  3, "3", 
}; 
 
main() { 
  printf ("Foo is %d bytes, i.e. %d entries each of size %d", 
           sizeof(foo), sizeof(foo) / sizeof(RecordT), sizeof(RecordT)); 
} 
BUILD IT ONCE AS C CODE, THEN AS C++ CODE
RUN BOTH VERSIONS TO SEE THE DIFFERENCES

Actual results:

> gcc foo.c 
> ./a.out 
Foo is 24 bytes, i.e. 3 entries each of size 8 
> cp foo.c foo.cc 
> gcc foo.cc 
> ./a.out 
Foo is 48 bytes, i.e. 6 entries each of size 8. 


Expected Results:  C++ output should be identical to C output.  Instead, the C++
array is sized based on the number of *raw initializers* instead of the number
of *structs* being initialized.  C compiler handles it fine, C++ does not.

Can be "fixed" by adding braces to the initializer, but much old code will break
in the meantime.

Additional info:

http://gcc.gnu.org/gcc-2.96.html makes me wonder why 2.96 is being
provided as the default compiler at all.  Surely 3.0 or even 2.95 would have
been better choices?

Comment 1 Jakub Jelinek 2002-01-17 16:56:08 UTC
This was fixed in gcc 3.1 by:
http://gcc.gnu.org/ml/gcc-patches/2001-11/msg00850.html
g++ 3.0.x does get it wrong the same way.

Comment 2 Alan Cox 2002-12-15 19:08:45 UTC
We now use g++ 3.2 so closing