From ff998900b163e96c52a4fefc71736ed34626ed86 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Sat, 6 May 2017 05:28:13 +0200 Subject: struct-init: Fix zero initialization with multi-level designators See the added testcase. When one used designators like .a.x to initialize sub-members of members, and didn't then initialize all of them the required zero-initialization of the other sub-members wasn't done. The fix also enables tiny code cleanups. --- tests/tests2/90_struct-init.c | 34 ++++++++++++++++++++++++++++++++++ tests/tests2/90_struct-init.expect | 3 +++ 2 files changed, 37 insertions(+) (limited to 'tests/tests2') diff --git a/tests/tests2/90_struct-init.c b/tests/tests2/90_struct-init.c index fd212ba..ab33812 100644 --- a/tests/tests2/90_struct-init.c +++ b/tests/tests2/90_struct-init.c @@ -217,7 +217,40 @@ void test_multi_relocs(void) for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) table[i](); } + +/* Following is from GCC gcc.c-torture/execute/20050613-1.c. */ +struct SEA { int i; int j; int k; int l; }; +struct SEB { struct SEA a; int r[1]; }; +struct SEC { struct SEA a; int r[0]; }; +struct SED { struct SEA a; int r[]; }; + +static void +test_correct_filling (struct SEA *x) +{ + static int i; + if (x->i != 0 || x->j != 5 || x->k != 0 || x->l != 0) + printf("sea_fill%d: wrong\n", i); + else + printf("sea_fill%d: okay\n", i); + i++; +} + +int +test_zero_init (void) +{ + /* The peculiarity here is that only a.j is initialized. That + means that all other members must be zero initialized. TCC + once didn't do that for sub-level designators. */ + struct SEB b = { .a.j = 5 }; + struct SEC c = { .a.j = 5 }; + struct SED d = { .a.j = 5 }; + test_correct_filling (&b.a); + test_correct_filling (&c.a); + test_correct_filling (&d.a); + return 0; +} + int main() { print(ce); @@ -244,5 +277,6 @@ int main() //printf("q: %s\n", q); test_compound_with_relocs(); test_multi_relocs(); + test_zero_init(); return 0; } diff --git a/tests/tests2/90_struct-init.expect b/tests/tests2/90_struct-init.expect index adda76d..e366121 100644 --- a/tests/tests2/90_struct-init.expect +++ b/tests/tests2/90_struct-init.expect @@ -38,3 +38,6 @@ flow: 9 8 7 6 0 0 0 0 0 0 0 0 0 0 0 0 6 5 4 3 0 0 0 0 0 0 0 0 0 0 0 0 one two three +sea_fill0: okay +sea_fill1: okay +sea_fill2: okay -- cgit v1.3.1