From 623550aff7f290a180473cd1a713f1c5ba882d8a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 24 Jun 2019 14:57:50 +0900 Subject: [PATCH 1/2] sd-path: use _cleanup_strv_free_ attribute --- src/libsystemd/sd-path/sd-path.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/libsystemd/sd-path/sd-path.c b/src/libsystemd/sd-path/sd-path.c index 12ff30ef86..2dfde4ca71 100644 --- a/src/libsystemd/sd-path/sd-path.c +++ b/src/libsystemd/sd-path/sd-path.c @@ -388,9 +388,9 @@ static int search_from_environment( bool env_search_sufficient, const char *first, ...) { + _cleanup_strv_free_ char **l = NULL; const char *e; char *h = NULL; - char **l = NULL; int r; assert(list); @@ -403,7 +403,7 @@ static int search_from_environment( return -ENOMEM; if (env_search_sufficient) { - *list = l; + *list = TAKE_PTR(l); return 0; } } @@ -424,10 +424,8 @@ static int search_from_environment( e = secure_getenv(env_home); if (e && path_is_absolute(e)) { h = strdup(e); - if (!h) { - strv_free(l); + if (!h) return -ENOMEM; - } } } @@ -435,23 +433,18 @@ static int search_from_environment( e = secure_getenv("HOME"); if (e && path_is_absolute(e)) { h = path_join(e, home_suffix); - - if (!h) { - strv_free(l); + if (!h) return -ENOMEM; - } } } if (h) { r = strv_consume_prepend(&l, h); - if (r < 0) { - strv_free(l); + if (r < 0) return -ENOMEM; - } } - *list = l; + *list = TAKE_PTR(l); return 0; } From 6abdec98f38f457797d666586d117070ccf4e31f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 24 Jun 2019 14:57:58 +0900 Subject: [PATCH 2/2] tree-wide: use _cleanup_ attribute and strv_consume() + TAKE_PTR() --- src/basic/terminal-util.c | 8 +++----- src/core/dbus-execute.c | 9 +++------ src/import/pull-common.c | 8 +++----- src/resolve/resolvectl.c | 16 ++++++++-------- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index aa69bede6f..76d6d1a20c 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -720,8 +720,7 @@ int get_kernel_consoles(char ***ret) { p = line; for (;;) { - _cleanup_free_ char *tty = NULL; - char *path; + _cleanup_free_ char *tty = NULL, *path = NULL; r = extract_first_word(&p, &tty, NULL, 0); if (r < 0) @@ -736,17 +735,16 @@ int get_kernel_consoles(char ***ret) { return r; } - path = strappend("/dev/", tty); + path = path_join("/dev", tty); if (!path) return -ENOMEM; if (access(path, F_OK) < 0) { log_debug_errno(errno, "Console device %s is not accessible, skipping: %m", path); - free(path); continue; } - r = strv_consume(&l, path); + r = strv_consume(&l, TAKE_PTR(path)); if (r < 0) return r; } diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 9ca37fefa4..4d5fb2eb10 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -2108,22 +2108,19 @@ int bus_exec_context_set_transient_property( return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not absolute.", path); if (!UNIT_WRITE_FLAGS_NOOP(flags)) { - _cleanup_free_ char *q = NULL; - char *buf; + _cleanup_free_ char *q = NULL, *buf = NULL; buf = strjoin(b ? "-" : "", path); if (!buf) return -ENOMEM; q = specifier_escape(buf); - if (!q) { - free(buf); + if (!q) return -ENOMEM; - } fprintf(f, "EnvironmentFile=%s\n", q); - r = strv_consume(&l, buf); + r = strv_consume(&l, TAKE_PTR(buf)); if (r < 0) return r; } diff --git a/src/import/pull-common.c b/src/import/pull-common.c index 766b97cb0c..62b52309a4 100644 --- a/src/import/pull-common.c +++ b/src/import/pull-common.c @@ -61,8 +61,8 @@ int pull_find_old_etags( } FOREACH_DIRENT_ALL(de, d, return -errno) { + _cleanup_free_ char *u = NULL; const char *a, *b; - char *u; if (de->d_type != DT_UNKNOWN && de->d_type != dt) @@ -97,12 +97,10 @@ int pull_find_old_etags( if (r < 0) return r; - if (!http_etag_is_valid(u)) { - free(u); + if (!http_etag_is_valid(u)) continue; - } - r = strv_consume(&l, u); + r = strv_consume(&l, TAKE_PTR(u)); if (r < 0) return r; } diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index 6c5b589944..94ec8be896 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -1229,7 +1229,7 @@ static int map_link_dns_servers(sd_bus *bus, const char *member, sd_bus_message return r; for (;;) { - char *pretty = NULL; + _cleanup_free_ char *pretty = NULL; r = read_dns_server_one(m, false, &pretty); if (r < 0) @@ -1240,7 +1240,7 @@ static int map_link_dns_servers(sd_bus *bus, const char *member, sd_bus_message if (isempty(pretty)) continue; - r = strv_consume(l, pretty); + r = strv_consume(l, TAKE_PTR(pretty)); if (r < 0) return r; } @@ -1306,7 +1306,7 @@ static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, return r; for (;;) { - char *pretty = NULL; + _cleanup_free_ char *pretty = NULL; r = read_domain_one(m, false, &pretty); if (r < 0) @@ -1317,7 +1317,7 @@ static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, if (isempty(pretty)) continue; - r = strv_consume(l, pretty); + r = strv_consume(l, TAKE_PTR(pretty)); if (r < 0) return r; } @@ -1536,7 +1536,7 @@ static int map_global_dns_servers(sd_bus *bus, const char *member, sd_bus_messag return r; for (;;) { - char *pretty = NULL; + _cleanup_free_ char *pretty = NULL; r = read_dns_server_one(m, true, &pretty); if (r < 0) @@ -1547,7 +1547,7 @@ static int map_global_dns_servers(sd_bus *bus, const char *member, sd_bus_messag if (isempty(pretty)) continue; - r = strv_consume(l, pretty); + r = strv_consume(l, TAKE_PTR(pretty)); if (r < 0) return r; } @@ -1580,7 +1580,7 @@ static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m return r; for (;;) { - char *pretty = NULL; + _cleanup_free_ char *pretty = NULL; r = read_domain_one(m, true, &pretty); if (r < 0) @@ -1591,7 +1591,7 @@ static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m if (isempty(pretty)) continue; - r = strv_consume(l, pretty); + r = strv_consume(l, TAKE_PTR(pretty)); if (r < 0) return r; }