]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-bus: fix missing initializer in SD_BUS_VTABLE_END (#20253)
authorMatthijs van Duin <matthijsvanduin@gmail.com>
Wed, 21 Jul 2021 09:10:36 +0000 (11:10 +0200)
committerGitHub <noreply@github.com>
Wed, 21 Jul 2021 09:10:36 +0000 (11:10 +0200)
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.]

src/systemd/sd-bus-vtable.h

index 75f884836036def30f09efe5d2a7d0ed0da9f444..35c942b16cc6c3c77f8f0f36f40ab9fef6d51720 100644 (file)
@@ -75,6 +75,9 @@ struct sd_bus_vtable {
                         uint64_t features;
                         const unsigned *vtable_format_reference;
                 } start;
+                struct {
+                        size_t reserved;
+                } end;
                 struct {
                         const char *member;
                         const char *signature;
@@ -185,7 +188,11 @@ struct sd_bus_vtable {
         {                                                               \
                 .type = _SD_BUS_VTABLE_END,                             \
                 .flags = 0,                                             \
-                .x = { { 0 } },                                         \
+                .x = {                                                  \
+                    .end = {                                            \
+                        .reserved = 0,                                  \
+                    },                                                  \
+                },                                                      \
         }
 
 #define _SD_ECHO(X) X