Bug 1110870

Summary: -Wextra produces 'missing initializer' warnings when {0} struct initializer used for object containing aggregate members
Product: Red Hat Enterprise Linux 6 Reporter: Jason Vas Dias <jason.vas.dias>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: qe-baseos-tools-bugs
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 6.4CC: mpolacek
Target Milestone: rc   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2014-06-18 16:25:49 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Jason Vas Dias 2014-06-18 16:12:46 UTC
Description of problem:

Use of the '{0}' initializer on structure objects which have aggregate members
produces a warning when compiled with -Wextra.

Example:

file a.c :
<quote>
struct s { int a ; char *b; struct { float c; char d ; } e; char f ; };
typedef struct s t;
void f(void)
{
  t v={0};
}
</quote>

$ gcc -std=c99 -Wall -Wextra -c a.c
a.c: In function 'f':
a.c:5: warning: missing initializer
a.c:5: warning: (near initialization for 'v.b')
a.c:5: warning: unused variable 'v'

According to C99 (n1124.pdf), section 6.7.8 paragraph 21:
<quote>
If there are fewer initializers in a brace-enclosed list than there are 
elements or members of an aggregate, or fewer characters in a string literal
used to initialize an array of known size than there are elements in the array,
the remainder of the aggregate shall be initialized implicitly the same as
objects that have static storage duration.
</quote>

Indeed, gcc-4.8.x emits only the "unused variable 'v'" warning with the same
command line, so this appears to be fixed upstream.

Warning such as this get me worried - does the object truly contain an
uninitialized 'b' member, or is the warning bogus ?

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

4.4.7-3

$ gcc -v | tail -n 1:
gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)


How reproducible:

100% 

Steps to Reproduce:
try to compile code containing a partial structure initializer of an 
object containing aggregate members

Actual results:
gcc emits 'missing initializer' warning with -Wextra.

Expected results:
gcc should be fully initializing the structure object's memory to all 0's and
not emitting a warning in this case, even with -Wextra.


Additional info:

Comment 1 Jason Vas Dias 2014-06-18 16:22:34 UTC
oops, forget about 'aggregate members' in description above - it actually
occurs for any struct containing more than one member.

Comment 2 Jakub Jelinek 2014-06-18 16:25:49 UTC
This was changed with http://gcc.gnu.org/PR36750, but I'd say it is highly undesirable to change the warnings in an existing gcc release.  -Wextra certainly includes various warnings that go beyond what the standard mandates, the exception made for PR36750 was for C only and only for { 0 } initializers, e.g. if you write { 0, 0 }; even latest GCC will still complain about missing initializers.  It is true they are initialized with zero, on the other side not always is that the intended thing, often just some initializers were forgotten (say, you have a structure, write initializer for it when it has 2 fields, then add another field and forget to add initializer for that).
See -Wmissing-field-initializers documentation.