From 0a0e594a26efe69afc3b05529dc0145a8d674e12 Mon Sep 17 00:00:00 2001 From: Benjamin Robin Date: Wed, 6 May 2020 20:29:31 +0200 Subject: [PATCH 1/5] tree-wide: Mark as _unused_ variables that are only used in assert() Allow to build without any warning with NDEBUG defined --- src/core/load-fragment.c | 2 +- src/journal/fsprg.c | 2 +- src/login/logind-dbus.c | 8 ++++---- src/machine/machined-dbus.c | 4 ++-- src/network/networkd-manager.c | 4 ++-- src/shared/resize-fs.c | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index b16c62a5c7..9c24bb780a 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -2889,7 +2889,7 @@ int config_parse_syscall_filter( void *userdata) { ExecContext *c = data; - const Unit *u = userdata; + _unused_ const Unit *u = userdata; bool invert = false; const char *p; int r; diff --git a/src/journal/fsprg.c b/src/journal/fsprg.c index 1dda2149d4..0ef3df89df 100644 --- a/src/journal/fsprg.c +++ b/src/journal/fsprg.c @@ -60,7 +60,7 @@ static void mpi_export(void *buf, size_t buflen, const gcry_mpi_t x) { static gcry_mpi_t mpi_import(const void *buf, size_t buflen) { gcry_mpi_t h; - unsigned len; + _unused_ unsigned len; assert_se(gcry_mpi_scan(&h, GCRYMPI_FMT_USG, buf, buflen, NULL) == 0); len = (gcry_mpi_get_nbits(h) + 7) / 8; diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 47fc53167e..20794451f5 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -2567,7 +2567,7 @@ static int method_can_reboot_parameter( void *userdata, sd_bus_error *error) { - Manager *m = userdata; + _unused_ Manager *m = userdata; int r; assert(message); @@ -2699,7 +2699,7 @@ static int method_can_reboot_to_firmware_setup( void *userdata, sd_bus_error *error) { - Manager *m = userdata; + _unused_ Manager *m = userdata; int r; assert(message); @@ -2888,7 +2888,7 @@ static int method_can_reboot_to_boot_loader_menu( void *userdata, sd_bus_error *error) { - Manager *m = userdata; + _unused_ Manager *m = userdata; int r; assert(message); @@ -3079,7 +3079,7 @@ static int method_can_reboot_to_boot_loader_entry( void *userdata, sd_bus_error *error) { - Manager *m = userdata; + _unused_ Manager *m = userdata; int r; assert(message); diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index ceb3afadbd..865ce7baad 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -112,7 +112,7 @@ static int method_get_machine(sd_bus_message *message, void *userdata, sd_bus_er static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_free_ char *p = NULL; - Manager *m = userdata; + _unused_ Manager *m = userdata; const char *name; int r; @@ -471,7 +471,7 @@ static int method_get_machine_os_release(sd_bus_message *message, void *userdata static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_hashmap_free_ Hashmap *images = NULL; - Manager *m = userdata; + _unused_ Manager *m = userdata; Image *image; Iterator i; int r; diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 3e9fb8c217..f9b611e419 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -2233,7 +2233,7 @@ void manager_dirty(Manager *manager) { } static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { - Manager *manager = userdata; + _unused_ Manager *manager = userdata; const sd_bus_error *e; assert(m); @@ -2279,7 +2279,7 @@ int manager_set_hostname(Manager *m, const char *hostname) { } static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { - Manager *manager = userdata; + _unused_ Manager *manager = userdata; const sd_bus_error *e; assert(m); diff --git a/src/shared/resize-fs.c b/src/shared/resize-fs.c index 5ed26a6c92..0abd289fec 100644 --- a/src/shared/resize-fs.c +++ b/src/shared/resize-fs.c @@ -15,7 +15,6 @@ int resize_fs(int fd, uint64_t sz, uint64_t *ret_size) { struct statfs sfs; - int r; assert(fd >= 0); @@ -42,6 +41,7 @@ int resize_fs(int fd, uint64_t sz, uint64_t *ret_size) { *ret_size = u * sfs.f_bsize; } else if (is_fs_type(&sfs, BTRFS_SUPER_MAGIC)) { + _unused_ int r; struct btrfs_ioctl_vol_args args = {}; /* 256M is the minimize size enforced by the btrfs kernel code when resizing (which is From f391597c6712ad90db37fe4a79047a45f4aca86a Mon Sep 17 00:00:00 2001 From: Benjamin Robin Date: Wed, 6 May 2020 20:30:43 +0200 Subject: [PATCH 2/5] tree-wide: Fix, replace assert() by assert_se() when there is side effect --- src/import/import-fs.c | 4 ++-- src/partition/repart.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/import/import-fs.c b/src/import/import-fs.c index 468303a6dc..5e61049ced 100644 --- a/src/import/import-fs.c +++ b/src/import/import-fs.c @@ -172,8 +172,8 @@ static int import_fs(int argc, char *argv[], void *userdata) { progress.limit = (RateLimit) { 200*USEC_PER_MSEC, 1 }; /* Hook into SIGINT/SIGTERM, so that we can cancel things then */ - assert(sigaction(SIGINT, &sa, &old_sigint_sa) >= 0); - assert(sigaction(SIGTERM, &sa, &old_sigterm_sa) >= 0); + assert_se(sigaction(SIGINT, &sa, &old_sigint_sa) >= 0); + assert_se(sigaction(SIGTERM, &sa, &old_sigterm_sa) >= 0); r = btrfs_subvol_snapshot_fd_full( fd, diff --git a/src/partition/repart.c b/src/partition/repart.c index bc9b12f167..82d60cd42b 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -1722,7 +1722,7 @@ static int context_dump_partition_bar(Context *context, const char *node) { bool z = false; size_t c, j = 0; - assert((c = columns()) >= 2); + assert_se((c = columns()) >= 2); c -= 2; /* We do not use the leftmost and rightmost character cell */ bar = new0(Partition*, c); From 060d9c61b6ee80585082cf5a1e499195c4df8d42 Mon Sep 17 00:00:00 2001 From: Benjamin Robin Date: Wed, 6 May 2020 20:32:27 +0200 Subject: [PATCH 3/5] test: Use assert_se() where variables are only checked by assert Allow to build without any warning with NDEBUG defined --- src/test/test-alloc-util.c | 14 +++++++------- src/test/test-fd-util.c | 8 ++++---- src/test/test-time-util.c | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/test-alloc-util.c b/src/test/test-alloc-util.c index 758b753237..d85f705504 100644 --- a/src/test/test-alloc-util.c +++ b/src/test/test-alloc-util.c @@ -99,13 +99,13 @@ static void test_bool_assign(void) { g = cp; /* cast from pointer */ h = NULL; /* cast from pointer */ - assert(b); - assert(c); - assert(d); - assert(e); - assert(!f); - assert(g); - assert(!h); + assert_se(b); + assert_se(c); + assert_se(d); + assert_se(e); + assert_se(!f); + assert_se(g); + assert_se(!h); } static int cleanup_counter = 0; diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index 713795814d..44c74987c5 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -233,17 +233,17 @@ static void assert_equal_fd(int fd1, int fd2) { ssize_t x, y; x = read(fd1, a, sizeof(a)); - assert(x >= 0); + assert_se(x >= 0); y = read(fd2, b, sizeof(b)); - assert(y >= 0); + assert_se(y >= 0); - assert(x == y); + assert_se(x == y); if (x == 0) break; - assert(memcmp(a, b, x) == 0); + assert_se(memcmp(a, b, x) == 0); } } diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index a422cc8ddc..e3b1f6f8ca 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -433,7 +433,7 @@ static void assert_similar(usec_t a, usec_t b) { else d = b - a; - assert(d < 10*USEC_PER_SEC); + assert_se(d < 10*USEC_PER_SEC); } static void test_usec_shift_clock(void) { From 683d0bc041e766e31febc211bce289f9d36dbafe Mon Sep 17 00:00:00 2001 From: Benjamin Robin Date: Wed, 6 May 2020 22:58:24 +0200 Subject: [PATCH 4/5] resize-fs: Use xsprintf instead of snprintf --- src/shared/resize-fs.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/shared/resize-fs.c b/src/shared/resize-fs.c index 0abd289fec..24a17a2ffe 100644 --- a/src/shared/resize-fs.c +++ b/src/shared/resize-fs.c @@ -41,7 +41,6 @@ int resize_fs(int fd, uint64_t sz, uint64_t *ret_size) { *ret_size = u * sfs.f_bsize; } else if (is_fs_type(&sfs, BTRFS_SUPER_MAGIC)) { - _unused_ int r; struct btrfs_ioctl_vol_args args = {}; /* 256M is the minimize size enforced by the btrfs kernel code when resizing (which is @@ -54,8 +53,7 @@ int resize_fs(int fd, uint64_t sz, uint64_t *ret_size) { sz -= sz % sfs.f_bsize; - r = snprintf(args.name, sizeof(args.name), "%" PRIu64, sz); - assert((size_t) r < sizeof(args.name)); + xsprintf(args.name, "%" PRIu64, sz); if (ioctl(fd, BTRFS_IOC_RESIZE, &args) < 0) return -errno; From 0454681395fc2747f2c135596e66e102b2708824 Mon Sep 17 00:00:00 2001 From: Benjamin Robin Date: Wed, 6 May 2020 23:02:39 +0200 Subject: [PATCH 5/5] meson: Do not drop warnings (unused variable) when NDEBUG is defined This is no longer needed, since there is now no warning with NDEBUG defined --- meson.build | 9 --------- 1 file changed, 9 deletions(-) diff --git a/meson.build b/meson.build index bf353cd1f7..a922f9a2f1 100644 --- a/meson.build +++ b/meson.build @@ -335,15 +335,6 @@ basic_disabled_warnings = [ '-Wno-unused-result', '-Wno-format-signedness', ] -if get_option('b_ndebug') == 'true' - # With asserts disabled with get a bunch of warnings about variables which - # are used only in the asserts. This is not useful at all, so let's just silence - # those warnings. - basic_disabled_warnings += [ - '-Wno-unused-variable', - '-Wno-unused-but-set-variable', - ] -endif possible_cc_flags = [ '-Werror=undef',