From: Zbigniew Jędrzejewski-Szmek Date: Wed, 31 Mar 2021 09:27:15 +0000 (+0200) Subject: sd-bus: add assert to tell the compiler that the error code is positive X-Git-Tag: v249-rc1~490^2~13 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c7e964c9444ecaaf52c5cd487e3331ba58a09f78;p=thirdparty%2Fsystemd.git sd-bus: add assert to tell the compiler that the error code is positive I was hoping it would help with the following gcc warning: [35/657] Compiling C object src/shared/libsystemd-shared-248.a.p/bus-message-util.c.o ../src/shared/bus-message-util.c: In function ‘bus_message_read_dns_servers’: ../src/shared/bus-message-util.c:165:21: warning: ‘family’ may be used uninitialized in this function [-Wmaybe-uninitialized] 165 | r = in_addr_full_new(family, &a, port, 0, server_name, dns + n); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../src/shared/bus-message-util.c:165:21: warning: ‘port’ may be used uninitialized in this function [-Wmaybe-uninitialized] ../src/shared/bus-message-util.c:165:21: warning: ‘server_name’ may be used uninitialized in this function [-Wmaybe-uninitialized] It actually doesn't, but the compiler has a point here: the code is specified in sd_bus_error_map[], and it has no way of knowning that we want it to be a positive value. I think this should be an assert, because if this assumption fails, a programming error has occured, something that'd want to catch. --- diff --git a/src/libsystemd/sd-bus/bus-error.c b/src/libsystemd/sd-bus/bus-error.c index 8da2024a502..163cbb1a71d 100644 --- a/src/libsystemd/sd-bus/bus-error.c +++ b/src/libsystemd/sd-bus/bus-error.c @@ -86,8 +86,10 @@ static int bus_error_name_to_errno(const char *name) { if (m->code == BUS_ERROR_MAP_END_MARKER) break; - if (streq(m->name, name)) + if (streq(m->name, name)) { + assert(m->code > 0); return m->code; + } } m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*)); @@ -103,8 +105,10 @@ static int bus_error_name_to_errno(const char *name) { continue; } - if (streq(m->name, name)) + if (streq(m->name, name)) { + assert(m->code > 0); return m->code; + } m++; }