Bug 22043 - [4.1 Regression] Fields not initialized for automatic structs with flexible array members
Summary: [4.1 Regression] Fields not initialized for automatic structs with flexible a...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.0.1
: P2 critical
Target Milestone: 4.1.0
Assignee: Not yet assigned to anyone
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: patch, wrong-code
Depends on:
Blocks:
 
Reported: 2005-06-13 08:52 UTC by Jakub Jelinek
Modified: 2005-08-23 11:11 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-07-12 12:32:16


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Jelinek 2005-06-13 08:52:09 UTC
In:
extern void abort (void);

struct A { int i; int j; int k; int l; };
struct B { struct A a; int r[1]; };
struct C { struct A a; int r[0]; };
struct D { struct A a; int r[]; };

void
foo (struct A *x)
{
  if (x->i != 0 || x->j != 5 || x->k != 0 || x->l != 0)
    abort ();
}

int
main ()
{
  struct B b = { .a.j = 5 };
  struct C c = { .a.j = 5 };
  struct D d = { .a.j = 5 };
  foo (&b.a);
  foo (&c.a);
  foo (&d.a);
  return 0;
}

GCC 4.0/HEAD only zero initialize a.i, a.k and a.l for the b variable, not for
c nor d, while 3.4.x and older initialized them for all 3 variables.
This causes miscompilation of glibc by GCC 4.0+.
Comment 1 Andrew Pinski 2005-06-13 12:31:49 UTC
Confirmed, patch here: <http://gcc.gnu.org/ml/gcc-patches/2005-06/msg01103.html>.
Comment 2 GCC Commits 2005-06-14 17:08:08 UTC
Subject: Bug 22043

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	jakub@gcc.gnu.org	2005-06-14 17:08:00

Modified files:
	gcc            : ChangeLog gimplify.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.c-torture/execute: 20050613-1.c 

Log message:
	PR tree-optimization/22043
	* gimplify.c (gimplify_init_constructor): If num_type_elements < 0
	for a constant-sized object, set cleared as well.
	
	* gcc.c-torture/execute/20050613-1.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=2.7592.2.287&r2=2.7592.2.288
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/gimplify.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=2.113.2.5&r2=2.113.2.6
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.5084.2.238&r2=1.5084.2.239
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20050613-1.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1

Comment 3 Andrew Pinski 2005-06-23 14:32:13 UTC
Fixed on the 4.0 branch at least.
Comment 4 Steven Bosscher 2005-07-14 23:06:50 UTC
Jakub, do you plan to fix this bug on mainline as well?? 
Comment 5 Andrew Pinski 2005-08-22 02:20:10 UTC
http://gcc.gnu.org/ml/gcc-patches/2005-06/msg01203.html
contains the lastest patch.
Comment 6 GCC Commits 2005-08-23 07:28:51 UTC
Subject: Bug 22043

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jakub@gcc.gnu.org	2005-08-23 07:28:26

Modified files:
	gcc            : ChangeLog expr.c gimplify.c tree-sra.c tree.h 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.c-torture/execute: 20050613-1.c 

Log message:
	PR tree-optimization/22043
	* tree.h (count_type_elements): Add ALLOW_FLEXARR argument.
	* expr.c (count_type_elements): Add ALLOW_FLEXARR argument.
	If ALLOW_FLEXARR, handle types ending with flexible array member.
	Pass false as second argument to recursive count_type_elements calls.
	(categorize_ctor_elements_1, mostly_zeros_p): Pass false as second
	argument to count_type_elements call.
	* tree-sra.c (decide_block_copy): Likewise.
	* gimplify.c (gimplify_init_constructor): If num_type_elements < 0
	for a constant-sized object, set cleared as well.  Pass true as
	second argument to count_type_elements call.
	
	* gcc.c-torture/execute/20050613-1.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.9803&r2=2.9804
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/expr.c.diff?cvsroot=gcc&r1=1.808&r2=1.809
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/gimplify.c.diff?cvsroot=gcc&r1=2.147&r2=2.148
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-sra.c.diff?cvsroot=gcc&r1=2.70&r2=2.71
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree.h.diff?cvsroot=gcc&r1=1.753&r2=1.754
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5951&r2=1.5952
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20050613-1.c.diff?cvsroot=gcc&r1=1.1&r2=1.2

Comment 7 Andrew Pinski 2005-08-23 11:11:06 UTC
Fixed.