cdaf655 sd-bus: fix missing initializer in SD_BUS_VTABLE_END (#20253)

1 file Authored by Matthijs van Duin 2 years ago, Committed by zbyszek 2 years ago,
    sd-bus: fix missing initializer in SD_BUS_VTABLE_END (#20253)
    
    When two fields were added to the vtable.x.start struct, no initializers
    for these were added to SD_BUS_VTABLE_END which also (ab)used that
    struct (albeit sneakily by using non-designated initialization).
    
    While C tolerates this, C++ prohibits these missing initializers, and
    both g++ and clang++ will complain when using -Wextra.
    
    This patch gives SD_BUS_VTABLE_END its own case in the union and
    clarifies its initialization.
    
    I tested the behaviour of g++ 10.2 and clang 11 in various cases. Both will warn
    (-Wmissing-field-initializers, implied by -Wextra) if you provide initializers for some
    but not all fields of a struct. Declaring x.end as empty struct or using an empty initializer
    {} to initialize the union or one of its members is valid C++ but not C, although both gcc
    and clang accept it without warning (even at -Wall -Wextra -std=c90/c++11) unless you
    use -pedantic (which requires -std=c99/c++2a to support designated initializers).
    
    Interestingly, .x = { .start = { 0, 0, NULL } } is the only initializer I found for the union
    (among candidates for SD_BUS_VTABLE_END) where gcc doesn't zero-fill it entirely
    when allocated on stack, it looked like it did in all other cases (I only examined this on
    32-bit arm). clang always seems to initialize all bytes of the union.
    
    [zjs: test case:
    $ cat vtable-test.cc
    #include "sd-bus.h"
    
    const sd_bus_vtable vtable[] = {
       SD_BUS_VTABLE_END
    };
    
    $ g++ -I src/systemd/ -Wall -Wmissing-field-initializers -c vtable-test.cc
    vtable-test.cc:5:1: warning: missing initializer for member ‘sd_bus_vtable::<unnamed union>::<unnamed struct>::features’ [-Wmissing-field-initializers]
        5 | };
          | ^
    vtable-test.cc:5:1: warning: missing initializer for member ‘sd_bus_vtable::<unnamed union>::<unnamed struct>::vtable_format_reference’ [-Wmissing-field-initializers]
    
    $ clang++ -I src/systemd/ -Wmissing-field-initializers -c vtable-test.cc
    vtable-test.cc:4:4: warning: missing field 'features' initializer [-Wmissing-field-initializers]
       SD_BUS_VTABLE_END
       ^
    src/systemd/sd-bus-vtable.h:188:28: note: expanded from macro 'SD_BUS_VTABLE_END'
                    .x = { { 0 } },                                         \
                               ^
    1 warning generated.
    
    Both warnings are gone with the patch.]
    
    (cherry picked from commit 654eaa403070d3c897454a5190603fda4071c3ff)
    
        
file modified
+8 -1