From c7e964c9444ecaaf52c5cd487e3331ba58a09f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 31 Mar 2021 11:27:15 +0200 Subject: [PATCH] sd-bus: add assert to tell the compiler that the error code is positive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/libsystemd/sd-bus/bus-error.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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++; }