Created attachment 38782 [details] Reduced file that triggers the segfault
Reduced testcase: extern struct S v, s; struct S { int t; int p[]; } v = { 4, 0 };
Started with r229505.
Created attachment 38783 [details] gcc7-pr71685.patch Untested fix.
The problem here is that I assumed that c_build_qualified_type would only be called when we want to create a variant type. However, there are a number of places that call it without checking to see if we have any type qualifiers first. These places can call it with no qualifiers, in which case it returns the original type, which may be a main variant type. So we need to avoid clearing C_TYPE_INCOMPLETE_TYPE in that case. Perhaps with an early exit at the top if type_quals is 0. Or alternatively, at the bottom, we can clear C_TYPE_INCOMPLETE_TYPE only if var_type != type.
Author: jakub Date: Tue Jun 28 22:30:04 2016 New Revision: 237830 URL: https://gcc.gnu.org/viewcvs?rev=237830&root=gcc&view=rev Log: PR c/71685 * c-typeck.c (c_build_qualified_type): Don't clear C_TYPE_INCOMPLETE_VARS for the main variant. * gcc.dg/pr71685.c: New test. Added: trunk/gcc/testsuite/gcc.dg/pr71685.c Modified: trunk/gcc/c/ChangeLog trunk/gcc/c/c-typeck.c trunk/gcc/testsuite/ChangeLog
Fixed on the trunk so far.
An early exit if type_quals is 0 does not work, as there is at least one place that deliberately calls c_build_qualified_type with no type qualifiers to create an unqualified type from a qualified type. Likewise, my earlier suggestion of a check for var_type != type can't work as it fails in this case too. I can see another possible fix, checking for type_quals non-zero before clearing the C_TYPE_INCOMPLETE_VARS, but given how complicated this code is turning out to be, the check for a type main variant is the safest fix. And I see that Jakub has just checked in a patch for that solution, so I'm assuming I don't need to do anymore work on this.
Author: jakub Date: Sat Jul 2 10:22:11 2016 New Revision: 237940 URL: https://gcc.gnu.org/viewcvs?rev=237940&root=gcc&view=rev Log: Backported from mainline 2016-06-29 Jakub Jelinek <jakub@redhat.com> PR c/71685 * c-typeck.c (c_build_qualified_type): Don't clear C_TYPE_INCOMPLETE_VARS for the main variant. * gcc.dg/pr71685.c: New test. Added: branches/gcc-6-branch/gcc/testsuite/gcc.dg/pr71685.c Modified: branches/gcc-6-branch/gcc/c/ChangeLog branches/gcc-6-branch/gcc/c/c-typeck.c branches/gcc-6-branch/gcc/testsuite/ChangeLog
Fixed.