[systemd-devel] [PATCH] use more _cleanup_ macro
Tom Gundersen
teg at jklm.no
Tue Jun 24 10:08:29 PDT 2014
Applied both. Thanks!
Tom
On Tue, Jun 24, 2014 at 7:00 PM, Ronny Chevalier
<chevalier.ronny at gmail.com> wrote:
> ---
> src/core/automount.c | 3 +--
> src/core/execute.c | 20 ++++++--------------
> src/core/killall.c | 4 +---
> src/core/umount.c | 16 ++++------------
> src/shared/conf-files.c | 5 +----
> src/shared/fdset.c | 4 +---
> src/shared/path-util.c | 4 +---
> src/shared/util.c | 3 +--
> 8 files changed, 16 insertions(+), 43 deletions(-)
>
> diff --git a/src/core/automount.c b/src/core/automount.c
> index 65e6d6f..73a8ce1 100644
> --- a/src/core/automount.c
> +++ b/src/core/automount.c
> @@ -144,7 +144,7 @@ static int automount_add_default_dependencies(Automount *a) {
>
> static int automount_verify(Automount *a) {
> bool b;
> - char *e;
> + _cleanup_free_ char *e = NULL;
> assert(a);
>
> if (UNIT(a)->load_state != UNIT_LOADED)
> @@ -160,7 +160,6 @@ static int automount_verify(Automount *a) {
> return -ENOMEM;
>
> b = unit_has_name(UNIT(a), e);
> - free(e);
>
> if (!b) {
> log_error_unit(UNIT(a)->id, "%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->id);
> diff --git a/src/core/execute.c b/src/core/execute.c
> index 78fb81f..1ea6463 100644
> --- a/src/core/execute.c
> +++ b/src/core/execute.c
> @@ -561,7 +561,7 @@ static int restore_confirm_stdio(int *saved_stdin,
>
> static int ask_for_confirmation(char *response, char **argv) {
> int saved_stdout = -1, saved_stdin = -1, r;
> - char *line;
> + _cleanup_free_ char *line = NULL;
>
> r = setup_confirm_stdio(&saved_stdin, &saved_stdout);
> if (r < 0)
> @@ -572,7 +572,6 @@ static int ask_for_confirmation(char *response, char **argv) {
> return -ENOMEM;
>
> r = ask(response, "yns", "Execute %s? [Yes, No, Skip] ", line);
> - free(line);
>
> restore_confirm_stdio(&saved_stdin, &saved_stdout);
>
> @@ -2058,8 +2057,8 @@ int exec_context_load_environment(const ExecContext *c, char ***l) {
> }
>
> static bool tty_may_match_dev_console(const char *tty) {
> - char *active = NULL, *console;
> - bool b;
> + _cleanup_free_ char *active = NULL;
> + char *console;
>
> if (startswith(tty, "/dev/"))
> tty += 5;
> @@ -2074,10 +2073,7 @@ static bool tty_may_match_dev_console(const char *tty) {
> return true;
>
> /* "tty0" means the active VC, so it may be the same sometimes */
> - b = streq(console, tty) || (streq(console, "tty0") && tty_is_vc(tty));
> - free(active);
> -
> - return b;
> + return streq(console, tty) || (streq(console, "tty0") && tty_is_vc(tty));
> }
>
> bool exec_context_may_touch_console(ExecContext *ec) {
> @@ -2467,10 +2463,10 @@ char *exec_command_line(char **argv) {
> }
>
> void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
> - char *p2;
> + _cleanup_free_ char *p2 = NULL;
> const char *prefix2;
>
> - char *cmd;
> + _cleanup_free_ char *cmd = NULL;
>
> assert(c);
> assert(f);
> @@ -2486,11 +2482,7 @@ void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
> "%sCommand Line: %s\n",
> prefix, cmd ? cmd : strerror(ENOMEM));
>
> - free(cmd);
> -
> exec_status_dump(&c->exec_status, f, prefix2);
> -
> - free(p2);
> }
>
> void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
> diff --git a/src/core/killall.c b/src/core/killall.c
> index eab48f7..291e1f9 100644
> --- a/src/core/killall.c
> +++ b/src/core/killall.c
> @@ -202,7 +202,7 @@ static int killall(int sig, Set *pids, bool send_sighup) {
>
> void broadcast_signal(int sig, bool wait_for_exit, bool send_sighup) {
> sigset_t mask, oldmask;
> - Set *pids = NULL;
> + _cleanup_set_free_ Set *pids = NULL;
>
> if (wait_for_exit)
> pids = set_new(trivial_hash_func, trivial_compare_func);
> @@ -223,6 +223,4 @@ void broadcast_signal(int sig, bool wait_for_exit, bool send_sighup) {
> wait_for_children(pids, &mask);
>
> assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
> -
> - set_free(pids);
> }
> diff --git a/src/core/umount.c b/src/core/umount.c
> index a30f674..cffa453 100644
> --- a/src/core/umount.c
> +++ b/src/core/umount.c
> @@ -126,9 +126,8 @@ static int mount_points_list_get(MountPoint **head) {
> }
>
> static int swap_list_get(MountPoint **head) {
> - FILE *proc_swaps;
> + _cleanup_fclose_ FILE *proc_swaps = NULL;
> unsigned int i;
> - int r;
>
> assert(head);
>
> @@ -168,26 +167,19 @@ static int swap_list_get(MountPoint **head) {
> free(dev);
>
> if (!d) {
> - r = -ENOMEM;
> - goto finish;
> + return -ENOMEM;
> }
>
> if (!(swap = new0(MountPoint, 1))) {
> free(d);
> - r = -ENOMEM;
> - goto finish;
> + return -ENOMEM;
> }
>
> swap->path = d;
> LIST_PREPEND(mount_point, *head, swap);
> }
>
> - r = 0;
> -
> -finish:
> - fclose(proc_swaps);
> -
> - return r;
> + return 0;
> }
>
> static int loopback_list_get(MountPoint **head) {
> diff --git a/src/shared/conf-files.c b/src/shared/conf-files.c
> index 64ce8a0..c72a099 100644
> --- a/src/shared/conf-files.c
> +++ b/src/shared/conf-files.c
> @@ -98,7 +98,7 @@ static int base_cmp(const void *a, const void *b) {
> }
>
> static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
> - Hashmap *fh;
> + _cleanup_hashmap_free_ Hashmap *fh = NULL;
> char **files, **p;
> int r;
>
> @@ -116,7 +116,6 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
> STRV_FOREACH(p, dirs) {
> r = files_add(fh, root, *p, suffix);
> if (r == -ENOMEM) {
> - hashmap_free_free(fh);
> return r;
> } else if (r < 0)
> log_debug("Failed to search for files in %s: %s",
> @@ -125,14 +124,12 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
>
> files = hashmap_get_strv(fh);
> if (files == NULL) {
> - hashmap_free_free(fh);
> return -ENOMEM;
> }
>
> qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
> *strv = files;
>
> - hashmap_free(fh);
> return 0;
> }
>
> diff --git a/src/shared/fdset.c b/src/shared/fdset.c
> index a2c861d..d2ea665 100644
> --- a/src/shared/fdset.c
> +++ b/src/shared/fdset.c
> @@ -104,7 +104,7 @@ int fdset_remove(FDSet *s, int fd) {
> }
>
> int fdset_new_fill(FDSet **_s) {
> - DIR *d;
> + _cleanup_closedir_ DIR *d = NULL;
> struct dirent *de;
> int r = 0;
> FDSet *s;
> @@ -150,8 +150,6 @@ int fdset_new_fill(FDSet **_s) {
> s = NULL;
>
> finish:
> - closedir(d);
> -
> /* We won't close the fds here! */
> if (s)
> set_free(MAKE_SET(s));
> diff --git a/src/shared/path-util.c b/src/shared/path-util.c
> index d193494..fd35e0c 100644
> --- a/src/shared/path-util.c
> +++ b/src/shared/path-util.c
> @@ -442,7 +442,7 @@ int path_is_mount_point(const char *t, bool allow_symlink) {
> };
>
> int mount_id, mount_id_parent;
> - char *parent;
> + _cleanup_free_ char *parent = NULL;
> struct stat a, b;
> int r;
>
> @@ -473,7 +473,6 @@ int path_is_mount_point(const char *t, bool allow_symlink) {
>
> h.handle.handle_bytes = MAX_HANDLE_SZ;
> r = name_to_handle_at(AT_FDCWD, parent, &h.handle, &mount_id_parent, 0);
> - free(parent);
> if (r < 0) {
> /* The parent can't do name_to_handle_at() but the
> * directory we are interested in can? If so, it must
> @@ -504,7 +503,6 @@ fallback:
> return r;
>
> r = lstat(parent, &b);
> - free(parent);
> if (r < 0)
> return -errno;
>
> diff --git a/src/shared/util.c b/src/shared/util.c
> index dbdb692..e7ff0f8 100644
> --- a/src/shared/util.c
> +++ b/src/shared/util.c
> @@ -1440,7 +1440,7 @@ _pure_ static bool fd_in_set(int fd, const int fdset[], unsigned n_fdset) {
> }
>
> int close_all_fds(const int except[], unsigned n_except) {
> - DIR *d;
> + _cleanup_closedir_ DIR *d = NULL;
> struct dirent *de;
> int r = 0;
>
> @@ -1495,7 +1495,6 @@ int close_all_fds(const int except[], unsigned n_except) {
> }
> }
>
> - closedir(d);
> return r;
> }
>
> --
> 2.0.0
>
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
More information about the systemd-devel
mailing list