https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61489

            Bug ID: 61489
           Summary: Wrong warning with -Wmissing-field-initializers.
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: D.Bahadir at GMX dot de

Created attachment 32928
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=32928&action=edit
test-case

This bug is related to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36750 but
addresses C++ instead of C.

When compiling the following code (in C++11 mode) with
-Wmissing-field-initializers then almost all attempts to initialize
"sockaddr_in" result in compiler-warnings about uninitialized field-members.
However, as far as I understand, almost all initializations should be fine and
zero-initialize (?) all struct-members and the compiler-warnings should
therefore not occur.


<code>
// Compile this code with: g++ -std=c++11 -Wmissing-field-initializers bug.cpp

#include <netinet/in.h>

int main()
{
    struct sockaddr_in addr0;      // No warning. But really zero-initialized?
    //struct sockaddr_in addr1();  // No warning, but a function-declaration.
    struct sockaddr_in addr2 = {};    // In C++: Warning about all members.
                                      // In C: Warning about first member.
    struct sockaddr_in addr3 = {0};   // In C++: Warning about all members
                                      //         except the first one.
    struct sockaddr_in addr4 = {0,};  // Same here.
#ifdef __cplusplus
    struct sockaddr_in addr5 = sockaddr_in();  // No warning and (probably)
                                               // zero-initialized.
#endif
#if __cplusplus >= 201103L
    struct sockaddr_in addr6{};    // Warning about all members.
    struct sockaddr_in addr7{0};   // Warning about all members except the
                                   // first one.
    struct sockaddr_in addr8{0,};  // Same here.
    struct sockaddr_in addr9  = sockaddr_in{};    // Warning about all members.
    struct sockaddr_in addr10 = sockaddr_in{0};   // Warning about all members
                                                  // except the first one.
    struct sockaddr_in addr11 = sockaddr_in{0,};  // Same here.
#endif

    return 0;
}
</code>


When compiling this code the following warnings will be issued:

bug.cpp: In function ‘int main()’:
bug.cpp:9:33: warning: missing initializer for member ‘sockaddr_in::sin_family’
[-Wmissing-field-initializers]
     struct sockaddr_in addr2 = {};    // In C++: Warning about all members.
                                 ^
bug.cpp:9:33: warning: missing initializer for member ‘sockaddr_in::sin_port’
[-Wmissing-field-initializers]
bug.cpp:9:33: warning: missing initializer for member ‘sockaddr_in::sin_addr’
[-Wmissing-field-initializers]
bug.cpp:9:33: warning: missing initializer for member ‘sockaddr_in::sin_zero’
[-Wmissing-field-initializers]
bug.cpp:11:34: warning: missing initializer for member ‘sockaddr_in::sin_port’
[-Wmissing-field-initializers]
     struct sockaddr_in addr3 = {0};   // In C++: Warning about all members
                                  ^
bug.cpp:11:34: warning: missing initializer for member ‘sockaddr_in::sin_addr’
[-Wmissing-field-initializers]
bug.cpp:11:34: warning: missing initializer for member ‘sockaddr_in::sin_zero’
[-Wmissing-field-initializers]
bug.cpp:13:35: warning: missing initializer for member ‘sockaddr_in::sin_port’
[-Wmissing-field-initializers]
     struct sockaddr_in addr4 = {0,};  // Same here.
                                   ^
bug.cpp:13:35: warning: missing initializer for member ‘sockaddr_in::sin_addr’
[-Wmissing-field-initializers]
bug.cpp:13:35: warning: missing initializer for member ‘sockaddr_in::sin_zero’
[-Wmissing-field-initializers]
bug.cpp:19:30: warning: missing initializer for member
‘sockaddr_in::sin_family’ [-Wmissing-field-initializers]
     struct sockaddr_in addr6{};    // Warning about all members.
                              ^
bug.cpp:19:30: warning: missing initializer for member ‘sockaddr_in::sin_port’
[-Wmissing-field-initializers]
bug.cpp:19:30: warning: missing initializer for member ‘sockaddr_in::sin_addr’
[-Wmissing-field-initializers]
bug.cpp:19:30: warning: missing initializer for member ‘sockaddr_in::sin_zero’
[-Wmissing-field-initializers]
bug.cpp:20:31: warning: missing initializer for member ‘sockaddr_in::sin_port’
[-Wmissing-field-initializers]
     struct sockaddr_in addr7{0};   // Warning about all members except the
                               ^
bug.cpp:20:31: warning: missing initializer for member ‘sockaddr_in::sin_addr’
[-Wmissing-field-initializers]
bug.cpp:20:31: warning: missing initializer for member ‘sockaddr_in::sin_zero’
[-Wmissing-field-initializers]
bug.cpp:22:32: warning: missing initializer for member ‘sockaddr_in::sin_port’
[-Wmissing-field-initializers]
     struct sockaddr_in addr8{0,};  // Same here.
                                ^
bug.cpp:22:32: warning: missing initializer for member ‘sockaddr_in::sin_addr’
[-Wmissing-field-initializers]
bug.cpp:22:32: warning: missing initializer for member ‘sockaddr_in::sin_zero’
[-Wmissing-field-initializers]
bug.cpp:23:45: warning: missing initializer for member
‘sockaddr_in::sin_family’ [-Wmissing-field-initializers]
     struct sockaddr_in addr9  = sockaddr_in{};    // Warning about all
members.
                                             ^
bug.cpp:23:45: warning: missing initializer for member ‘sockaddr_in::sin_port’
[-Wmissing-field-initializers]
bug.cpp:23:45: warning: missing initializer for member ‘sockaddr_in::sin_addr’
[-Wmissing-field-initializers]
bug.cpp:23:45: warning: missing initializer for member ‘sockaddr_in::sin_zero’
[-Wmissing-field-initializers]
bug.cpp:24:46: warning: missing initializer for member ‘sockaddr_in::sin_port’
[-Wmissing-field-initializers]
     struct sockaddr_in addr10 = sockaddr_in{0};   // Warning about all members
                                              ^
bug.cpp:24:46: warning: missing initializer for member ‘sockaddr_in::sin_addr’
[-Wmissing-field-initializers]
bug.cpp:24:46: warning: missing initializer for member ‘sockaddr_in::sin_zero’
[-Wmissing-field-initializers]
bug.cpp:26:47: warning: missing initializer for member ‘sockaddr_in::sin_port’
[-Wmissing-field-initializers]
     struct sockaddr_in addr11 = sockaddr_in{0,};  // Same here.
                                               ^
bug.cpp:26:47: warning: missing initializer for member ‘sockaddr_in::sin_addr’
[-Wmissing-field-initializers]
bug.cpp:26:47: warning: missing initializer for member ‘sockaddr_in::sin_zero’
[-Wmissing-field-initializers]

Reply via email to