From 78e334b50f12a3ef386fb5de03d0549fa853c692 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Fri, 8 Apr 2016 22:20:22 -0400 Subject: basic/util: silence stupid gcc warnings about unitialized variable --- src/basic/util.c | 7 ++++--- src/core/machine-id-setup.c | 9 +++++---- src/core/unit.c | 9 +++++---- 3 files changed, 14 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/basic/util.c b/src/basic/util.c index 957b0e1ff1..b70c50047f 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -792,10 +792,11 @@ int update_reboot_parameter_and_warn(const char *param) { return 0; } - RUN_WITH_UMASK(0022) + RUN_WITH_UMASK(0022) { r = write_string_file("/run/systemd/reboot-param", param, WRITE_STRING_FILE_CREATE); - if (r < 0) - return log_warning_errno(r, "Failed to write reboot parameter file: %m"); + if (r < 0) + return log_warning_errno(r, "Failed to write reboot parameter file: %m"); + } return 0; } diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c index 86da16c31e..9de528b6cf 100644 --- a/src/core/machine-id-setup.c +++ b/src/core/machine-id-setup.c @@ -259,11 +259,12 @@ int machine_id_setup(const char *root, sd_id128_t machine_id) { /* Hmm, we couldn't write it? So let's write it to * /run/machine-id as a replacement */ - RUN_WITH_UMASK(0022) + RUN_WITH_UMASK(0022) { r = write_string_file(run_machine_id, id, WRITE_STRING_FILE_CREATE); - if (r < 0) { - (void) unlink(run_machine_id); - return log_error_errno(r, "Cannot write %s: %m", run_machine_id); + if (r < 0) { + (void) unlink(run_machine_id); + return log_error_errno(r, "Cannot write %s: %m", run_machine_id); + } } /* And now, let's mount it over */ diff --git a/src/core/unit.c b/src/core/unit.c index fb88260d23..49c990c2d7 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -3480,11 +3480,12 @@ int unit_make_transient(Unit *u) { /* Let's open the file we'll write the transient settings into. This file is kept open as long as we are * creating the transient, and is closed in unit_load(), as soon as we start loading the file. */ - RUN_WITH_UMASK(0022) + RUN_WITH_UMASK(0022) { f = fopen(path, "we"); - if (!f) { - free(path); - return -errno; + if (!f) { + free(path); + return -errno; + } } if (u->transient_file) -- cgit v1.2.3-54-g00ecf From 81d621034badfd75edb5e1fc88334af9a87a18e3 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Sat, 9 Apr 2016 14:04:09 -0400 Subject: tree-wide: remove useless NULLs from strjoina The coccinelle patch didn't work in some places, I have no idea why. --- coccinelle/strjoina.cocci | 6 ++++++ src/core/unit.c | 13 ++++++------- src/coredump/coredump.c | 2 +- src/login/logind-session.c | 2 +- src/machine/machine-dbus.c | 2 +- src/resolve/resolved-dns-dnssec.c | 2 +- src/shared/bus-util.c | 4 ++-- src/shared/path-lookup.c | 2 +- src/systemctl/systemctl.c | 2 +- 9 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 coccinelle/strjoina.cocci (limited to 'src') diff --git a/coccinelle/strjoina.cocci b/coccinelle/strjoina.cocci new file mode 100644 index 0000000000..a6236eb0f9 --- /dev/null +++ b/coccinelle/strjoina.cocci @@ -0,0 +1,6 @@ +@@ +expression n, m; +expression list s; +@@ +- n = strjoina(m, s, NULL); ++ n = strjoina(m, s); diff --git a/src/core/unit.c b/src/core/unit.c index 49c990c2d7..c60ae2be9d 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -2518,12 +2518,11 @@ int unit_install_bus_match(Unit *u, sd_bus *bus, const char *name) { return -EBUSY; match = strjoina("type='signal'," - "sender='org.freedesktop.DBus'," - "path='/org/freedesktop/DBus'," - "interface='org.freedesktop.DBus'," - "member='NameOwnerChanged'," - "arg0='", name, "'", - NULL); + "sender='org.freedesktop.DBus'," + "path='/org/freedesktop/DBus'," + "interface='org.freedesktop.DBus'," + "member='NameOwnerChanged'," + "arg0='", name, "'"); return sd_bus_add_match(bus, &u->match_bus_slot, match, signal_name_owner_changed, u); } @@ -3437,7 +3436,7 @@ int unit_write_drop_in_private(Unit *u, UnitSetPropertiesMode mode, const char * if (!IN_SET(mode, UNIT_PERSISTENT, UNIT_RUNTIME)) return 0; - ndata = strjoina("[", UNIT_VTABLE(u)->private_section, "]\n", data, NULL); + ndata = strjoina("[", UNIT_VTABLE(u)->private_section, "]\n", data); return unit_write_drop_in(u, mode, name, ndata); } diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 085909c20c..f65cb6f9dd 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -1095,7 +1095,7 @@ static int process_kernel(int argc, char* argv[]) { IOVEC_SET_STRING(iovec[n_iovec++], core_environ); } - core_timestamp = strjoina("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000", NULL); + core_timestamp = strjoina("COREDUMP_TIMESTAMP=", context[CONTEXT_TIMESTAMP], "000000"); IOVEC_SET_STRING(iovec[n_iovec++], core_timestamp); IOVEC_SET_STRING(iovec[n_iovec++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1"); diff --git a/src/login/logind-session.c b/src/login/logind-session.c index e088225beb..676fbc15a3 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -513,7 +513,7 @@ static int session_start_scope(Session *s) { if (!scope) return log_oom(); - description = strjoina("Session ", s->id, " of user ", s->user->name, NULL); + description = strjoina("Session ", s->id, " of user ", s->user->name); r = manager_start_scope( s->manager, diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index c7ff0efac8..ab54d9e934 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -730,7 +730,7 @@ int bus_machine_method_open_shell(sd_bus_message *message, void *userdata, sd_bu return r; /* Name and mode */ - unit = strjoina("container-shell@", p, ".service", NULL); + unit = strjoina("container-shell@", p, ".service"); r = sd_bus_message_append(tm, "ss", unit, "fail"); if (r < 0) return r; diff --git a/src/resolve/resolved-dns-dnssec.c b/src/resolve/resolved-dns-dnssec.c index 0af7551425..a54aed3a63 100644 --- a/src/resolve/resolved-dns-dnssec.c +++ b/src/resolve/resolved-dns-dnssec.c @@ -1734,7 +1734,7 @@ static int dnssec_nsec_covers_wildcard(DnsResourceRecord *rr, const char *name) if (r <= 0) return r; - wc = strjoina("*.", common_suffix, NULL); + wc = strjoina("*.", common_suffix); return dns_name_between(dns_resource_key_name(rr->key), wc, rr->nsec.next_domain_name); } diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 0caaca03c7..2b86b1fcd6 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -2050,8 +2050,8 @@ static void log_job_error_with_service_result(const char* service, const char *r _cleanup_free_ char *t; t = strv_join((char**) extra_args, " "); - systemctl = strjoina("systemctl ", t ?: "", NULL); - journalctl = strjoina("journalctl ", t ?: "", NULL); + systemctl = strjoina("systemctl ", t ? : ""); + journalctl = strjoina("journalctl ", t ? : ""); } if (!isempty(result)) { diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c index ca69a0aef2..167108ee1b 100644 --- a/src/shared/path-lookup.c +++ b/src/shared/path-lookup.c @@ -249,7 +249,7 @@ static int acquire_generator_dirs( if (!e) return -ENXIO; - prefix = strjoina(e, "/systemd/", NULL); + prefix = strjoina(e, "/systemd/"); break; } diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index f0e788a508..65b52be04a 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -3254,7 +3254,7 @@ static int kill_unit(int argc, char *argv[], void *userdata) { /* --fail was specified */ if (streq(arg_job_mode, "fail")) - kill_who = strjoina(arg_kill_who, "-fail", NULL); + kill_who = strjoina(arg_kill_who, "-fail"); r = expand_names(bus, strv_skip(argv, 1), NULL, &names); if (r < 0) -- cgit v1.2.3-54-g00ecf From ea9b54f827feff046d7d6e5c485d01cf98214762 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Wed, 13 Apr 2016 21:09:32 -0400 Subject: test-strv: add a test that extending empty strv works as expected Just making sure :) --- src/test/test-strv.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/test/test-strv.c b/src/test/test-strv.c index fea1f848cd..fc01dcfaf1 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -358,7 +358,7 @@ static void test_strv_extend_strv_concat(void) { } static void test_strv_extend_strv(void) { - _cleanup_strv_free_ char **a = NULL, **b = NULL; + _cleanup_strv_free_ char **a = NULL, **b = NULL, **n = NULL; a = strv_new("abc", "def", "ghi", NULL); b = strv_new("jkl", "mno", "abc", "pqr", NULL); @@ -373,8 +373,14 @@ static void test_strv_extend_strv(void) { assert_se(streq(a[3], "jkl")); assert_se(streq(a[4], "mno")); assert_se(streq(a[5], "pqr")); - assert_se(strv_length(a) == 6); + + assert_se(strv_extend_strv(&n, b, false) >= 0); + assert_se(streq(n[0], "jkl")); + assert_se(streq(n[1], "mno")); + assert_se(streq(n[2], "abc")); + assert_se(streq(n[3], "pqr")); + assert_se(strv_length(n) == 4); } static void test_strv_extend(void) { -- cgit v1.2.3-54-g00ecf From e215d211d25139a46970101525ebdde874daf2f0 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Wed, 13 Apr 2016 21:10:33 -0400 Subject: shared/path-lookup: fix leak CID #1354671: char **l would be leaked. Also rename l to paths, to make the code easier to read, and do strv deduplication immediately when extending. No need to allocate strings to remove them a few lines down. --- src/shared/path-lookup.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c index 167108ee1b..80a2ea7940 100644 --- a/src/shared/path-lookup.c +++ b/src/shared/path-lookup.c @@ -451,7 +451,7 @@ int lookup_paths_init( *transient = NULL, *persistent_control = NULL, *runtime_control = NULL; bool append = false; /* Add items from SYSTEMD_UNIT_PATH before normal directories */ - char **l = NULL; + _cleanup_strv_free_ char **paths = NULL; const char *e; int r; @@ -506,13 +506,12 @@ int lookup_paths_init( /* FIXME: empty components in other places should be * rejected. */ - r = path_split_and_make_absolute(e, &l); + r = path_split_and_make_absolute(e, &paths); if (r < 0) return r; - } else - l = NULL; + } - if (!l || append) { + if (!paths || append) { /* Let's figure something out. */ _cleanup_strv_free_ char **add = NULL; @@ -587,14 +586,9 @@ int lookup_paths_init( if (!add) return -ENOMEM; - if (l) { - r = strv_extend_strv(&l, add, false); - if (r < 0) + r = strv_extend_strv(&paths, add, true); + if (r < 0) return r; - } else { - l = add; - add = NULL; - } } r = patch_root_prefix(&persistent_config, root); @@ -626,12 +620,12 @@ int lookup_paths_init( if (r < 0) return r; - r = patch_root_prefix_strv(l, root); + r = patch_root_prefix_strv(paths, root); if (r < 0) return -ENOMEM; - p->search_path = strv_uniq(l); - l = NULL; + p->search_path = strv_uniq(paths); + paths = NULL; p->persistent_config = persistent_config; p->runtime_config = runtime_config; -- cgit v1.2.3-54-g00ecf From 78df0edc1b594017b1d3493eb2033d8a92d9d43f Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Wed, 13 Apr 2016 21:11:28 -0400 Subject: systemctl: fix leak CID #1354670. --- src/systemctl/systemctl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 65b52be04a..91829fcbcf 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -6087,8 +6087,7 @@ static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { return r; STRV_FOREACH(name, names) { - _cleanup_free_ char *path = NULL; - char *new_path, *tmp_path; + _cleanup_free_ char *path = NULL, *new_path = NULL, *tmp_path = NULL; r = unit_find_paths(bus, *name, &lp, &path, NULL); if (r < 0) @@ -6111,6 +6110,7 @@ static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) { r = strv_push_pair(paths, new_path, tmp_path); if (r < 0) return log_oom(); + new_path = tmp_path = NULL; } return 0; -- cgit v1.2.3-54-g00ecf