1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-03-31 11:27:15 +02:00
parent 55e2cfc938
commit c7e964c944

View File

@ -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++;
}