diff --git a/coccinelle/take-fd.cocci b/coccinelle/take-fd.cocci new file mode 100644 index 0000000000..ba242483cd --- /dev/null +++ b/coccinelle/take-fd.cocci @@ -0,0 +1,14 @@ +@@ +local idexpression p; +expression q; +@@ +- p = q; +- q = -1; +- return p; ++ return TAKE_FD(q); +@@ +expression p, q; +@@ +- p = q; +- q = -1; ++ p = TAKE_FD(q); diff --git a/coccinelle/take-ptr.cocci b/coccinelle/take-ptr.cocci new file mode 100644 index 0000000000..0cebe81575 --- /dev/null +++ b/coccinelle/take-ptr.cocci @@ -0,0 +1,14 @@ +@@ +local idexpression p; +expression q; +@@ +- p = q; +- q = NULL; +- return p; ++ return TAKE_PTR(q); +@@ +expression p, q; +@@ +- p = q; +- q = NULL; ++ p = TAKE_PTR(q); diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index ec7808c1f7..b1e0edbb7f 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -130,3 +130,12 @@ void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size); _new_ = alloca_align(_size_, (align)); \ (void*)memset(_new_, 0, _size_); \ }) + +/* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to + * NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */ +#define TAKE_PTR(ptr) \ + ({ \ + typeof(ptr) _ptr_ = (ptr); \ + (ptr) = NULL; \ + _ptr_; \ + }) diff --git a/src/basic/cap-list.c b/src/basic/cap-list.c index c4557666ef..9416391a53 100644 --- a/src/basic/cap-list.c +++ b/src/basic/cap-list.c @@ -103,8 +103,7 @@ int capability_set_to_string_alloc(uint64_t set, char **s) { str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */ - *s = str; - str = NULL; + *s = TAKE_PTR(str); return 0; } diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 1a2d7fc087..5934ee6e8a 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -1424,10 +1424,9 @@ int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) { if (r < 0) return r; - if (c == raw) { - *cgroup = raw; - raw = NULL; - } else { + if (c == raw) + *cgroup = TAKE_PTR(raw); + else { char *n; n = strdup(c); @@ -2010,8 +2009,7 @@ int cg_slice_to_path(const char *unit, char **ret) { if (!strextend(&s, e, NULL)) return -ENOMEM; - *ret = s; - s = NULL; + *ret = TAKE_PTR(s); return 0; } @@ -2301,8 +2299,7 @@ int cg_mask_to_string(CGroupMask mask, char **ret) { assert(s); s[n] = 0; - *ret = s; - s = NULL; + *ret = TAKE_PTR(s); return 0; } diff --git a/src/basic/extract-word.c b/src/basic/extract-word.c index 5e42560487..7a6b56f071 100644 --- a/src/basic/extract-word.c +++ b/src/basic/extract-word.c @@ -194,8 +194,7 @@ finish: finish_force_next: s[sz] = 0; - *ret = s; - s = NULL; + *ret = TAKE_PTR(s); return 1; } diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 6d7875361c..0726aac38c 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -484,10 +484,7 @@ int acquire_data_fd(const void *data, size_t size, unsigned flags) { if (r < 0) return r; - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); } try_pipe: @@ -524,10 +521,7 @@ try_pipe: (void) fd_nonblock(pipefds[0], false); - r = pipefds[0]; - pipefds[0] = -1; - - return r; + return TAKE_FD(pipefds[0]); } try_dev_shm: diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 635a538b5a..007580b48f 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -105,3 +105,11 @@ int rearrange_stdio(int original_input_fd, int original_output_fd, int original_ static inline int make_null_stdio(void) { return rearrange_stdio(-1, -1, -1); } + +/* Like TAKE_PTR() but for file descriptors, resetting them to -1 */ +#define TAKE_FD(fd) \ + ({ \ + int _fd_ = (fd); \ + (fd) = -1; \ + _fd_; \ + }) diff --git a/src/basic/fileio.c b/src/basic/fileio.c index fb26274afa..f807842c31 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -330,8 +330,7 @@ int read_full_stream(FILE *f, char **contents, size_t *size) { } buf[l] = 0; - *contents = buf; - buf = NULL; /* do not free */ + *contents = TAKE_PTR(buf); if (size) *size = l; @@ -1432,8 +1431,7 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) { if (fd < 0) return -errno; - *ret_path = tmp; - tmp = NULL; + *ret_path = TAKE_PTR(tmp); return fd; } @@ -1519,8 +1517,7 @@ int read_nul_string(FILE *f, char **ret) { return -ENOMEM; } - *ret = x; - x = NULL; + *ret = TAKE_PTR(x); return 0; } diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index c65ba4bfe5..150c47a719 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -458,10 +458,8 @@ int get_files_in_directory(const char *path, char ***list) { n++; } - if (list) { - *list = l; - l = NULL; /* avoid freeing */ - } + if (list) + *list = TAKE_PTR(l); return n; } @@ -732,8 +730,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags, } safe_close(fd); - fd = fd_parent; - fd_parent = -1; + fd = TAKE_FD(fd_parent); continue; } @@ -838,10 +835,9 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags, } /* If this is not a symlink, then let's just add the name we read to what we already verified. */ - if (!done) { - done = first; - first = NULL; - } else { + if (!done) + done = TAKE_PTR(first); + else { /* If done is "/", as first also contains slash at the head, then remove this redundant slash. */ if (streq(done, "/")) *done = '\0'; @@ -852,8 +848,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags, /* And iterate again, but go one directory further down. */ safe_close(fd); - fd = child; - child = -1; + fd = TAKE_FD(child); } if (!done) { @@ -863,22 +858,15 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags, return -ENOMEM; } - if (ret) { - *ret = done; - done = NULL; - } + if (ret) + *ret = TAKE_PTR(done); if (flags & CHASE_OPEN) { - int q; - /* Return the O_PATH fd we currently are looking to the caller. It can translate it to a proper fd by * opening /proc/self/fd/xyz. */ assert(fd >= 0); - q = fd; - fd = -1; - - return q; + return TAKE_FD(fd); } return exists; diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c index 266cb29936..de3d7c8c89 100644 --- a/src/basic/locale-util.c +++ b/src/basic/locale-util.c @@ -341,8 +341,7 @@ int get_keymaps(char ***ret) { strv_sort(l); - *ret = l; - l = NULL; + *ret = TAKE_PTR(l); return 0; } diff --git a/src/basic/memfd-util.c b/src/basic/memfd-util.c index e7eb895462..758ba6458a 100644 --- a/src/basic/memfd-util.c +++ b/src/basic/memfd-util.c @@ -168,8 +168,5 @@ int memfd_new_and_map(const char *name, size_t sz, void **p) { if (r < 0) return r; - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); } diff --git a/src/basic/mount-util.c b/src/basic/mount-util.c index 8151b3a4e4..b6e7c94017 100644 --- a/src/basic/mount-util.c +++ b/src/basic/mount-util.c @@ -81,10 +81,8 @@ int name_to_handle_at_loop( if (name_to_handle_at(fd, path, h, &mnt_id, flags) >= 0) { - if (ret_handle) { - *ret_handle = h; - h = NULL; - } + if (ret_handle) + *ret_handle = TAKE_PTR(h); if (ret_mnt_id) *ret_mnt_id = mnt_id; @@ -951,8 +949,7 @@ int mount_option_mangle( } *ret_mount_flags = mount_flags; - *ret_remaining_options = ret; - ret = NULL; + *ret_remaining_options = TAKE_PTR(ret); return 0; } diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index fa5b4a353a..8d4912fdbf 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -324,8 +324,7 @@ int parse_syscall_and_errno(const char *in, char **name, int *error) { return -EINVAL; *error = e; - *name = n; - n = NULL; + *name = TAKE_PTR(n); return 0; } diff --git a/src/basic/path-util.c b/src/basic/path-util.c index df94629385..d4c4a02cb3 100644 --- a/src/basic/path-util.c +++ b/src/basic/path-util.c @@ -290,8 +290,7 @@ char **path_strv_resolve(char **l, const char *root) { r = chase_symlinks(t, root, 0, &u); if (r == -ENOENT) { if (root) { - u = orig; - orig = NULL; + u = TAKE_PTR(orig); free(t); } else u = t; diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c index c5d1fb1d41..c51e3c0a3b 100644 --- a/src/basic/proc-cmdline.c +++ b/src/basic/proc-cmdline.c @@ -204,10 +204,8 @@ int proc_cmdline_get_key(const char *key, unsigned flags, char **value) { } } - if (value) { - *value = ret; - ret = NULL; - } + if (value) + *value = TAKE_PTR(ret); return found; } diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 853e0e3449..2583b310a4 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -623,8 +623,7 @@ int get_process_environ(pid_t pid, char **env) { } else outcome[sz] = '\0'; - *env = outcome; - outcome = NULL; + *env = TAKE_PTR(outcome); return 0; } diff --git a/src/basic/securebits-util.c b/src/basic/securebits-util.c index 441d386f9e..75675bf4c0 100644 --- a/src/basic/securebits-util.c +++ b/src/basic/securebits-util.c @@ -48,8 +48,7 @@ int secure_bits_to_string_alloc(int i, char **s) { if (len != 0) str[len - 1] = '\0'; - *s = str; - str = NULL; + *s = TAKE_PTR(str); return 0; } diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index b91b093132..fd26ae7137 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -985,8 +985,7 @@ int getpeersec(int fd, char **ret) { if (isempty(s)) return -EOPNOTSUPP; - *ret = s; - s = NULL; + *ret = TAKE_PTR(s); return 0; } diff --git a/src/basic/strv.c b/src/basic/strv.c index e80ff4a62c..8010016451 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -341,8 +341,7 @@ int strv_split_extract(char ***t, const char *s, const char *separators, Extract if (!GREEDY_REALLOC(l, allocated, n + 2)) return -ENOMEM; - l[n++] = word; - word = NULL; + l[n++] = TAKE_PTR(word); l[n] = NULL; } @@ -353,8 +352,7 @@ int strv_split_extract(char ***t, const char *s, const char *separators, Extract return -ENOMEM; } - *t = l; - l = NULL; + *t = TAKE_PTR(l); return (int) n; } diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index eacfd14677..c7e2d2abb7 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -496,10 +496,7 @@ int acquire_terminal( fd = safe_close(fd); } - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); } int release_terminal(void) { @@ -707,10 +704,9 @@ int vtnr_from_tty(const char *tty) { tty = active; } - if (tty == active) { - *ret = active; - active = NULL; - } else { + if (tty == active) + *ret = TAKE_PTR(active); + else { char *tmp; tmp = strdup(tty); @@ -778,8 +774,7 @@ int get_kernel_consoles(char ***ret) { goto fallback; } - *ret = l; - l = NULL; + *ret = TAKE_PTR(l); return 0; @@ -788,8 +783,7 @@ fallback: if (r < 0) return r; - *ret = l; - l = NULL; + *ret = TAKE_PTR(l); return 0; } diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c index 3937dfc5ee..7d9367334b 100644 --- a/src/basic/unit-name.c +++ b/src/basic/unit-name.c @@ -363,8 +363,7 @@ int unit_name_unescape(const char *f, char **ret) { *t = 0; - *ret = r; - r = NULL; + *ret = TAKE_PTR(r); return 0; } diff --git a/src/busctl/busctl-introspect.c b/src/busctl/busctl-introspect.c index 54d6e07d2c..cce68a480b 100644 --- a/src/busctl/busctl-introspect.c +++ b/src/busctl/busctl-introspect.c @@ -275,10 +275,9 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth free(node_path); - if (name[0] == '/') { - node_path = name; - name = NULL; - } else { + if (name[0] == '/') + node_path = TAKE_PTR(name); + else { if (endswith(prefix, "/")) node_path = strappend(prefix, name); diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 65ed86580f..62a3d86ef1 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -1381,8 +1381,7 @@ int unit_set_cgroup_path(Unit *u, const char *path) { unit_release_cgroup(u); - u->cgroup_path = p; - p = NULL; + u->cgroup_path = TAKE_PTR(p); return 1; } diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 635213a866..7344623ebf 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -424,10 +424,8 @@ static int property_get_syscall_filter( if (r < 0) return -ENOMEM; } - } else { - s = name; - name = NULL; - } + } else + s = TAKE_PTR(name); r = strv_consume(&l, s); if (r < 0) @@ -1125,8 +1123,7 @@ int bus_set_transient_exec_command( return -ENOMEM; } - c->argv = argv; - argv = NULL; + c->argv = TAKE_PTR(argv); c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0; diff --git a/src/core/dbus-job.c b/src/core/dbus-job.c index 0802fc9773..15d0e5dd8b 100644 --- a/src/core/dbus-job.c +++ b/src/core/dbus-job.c @@ -274,8 +274,7 @@ int bus_job_coldplug_bus_track(Job *j) { assert(j); - deserialized_clients = j->deserialized_clients; - j->deserialized_clients = NULL; + deserialized_clients = TAKE_PTR(j->deserialized_clients); if (strv_isempty(deserialized_clients)) return 0; diff --git a/src/core/dbus.c b/src/core/dbus.c index 56b43adcda..eb9ec44f5b 100644 --- a/src/core/dbus.c +++ b/src/core/dbus.c @@ -501,8 +501,7 @@ static int bus_job_enumerate(sd_bus *bus, const char *path, void *userdata, char assert(hashmap_size(m->jobs) == k); - *nodes = l; - l = NULL; + *nodes = TAKE_PTR(l); return k; } @@ -526,8 +525,7 @@ static int bus_unit_enumerate(sd_bus *bus, const char *path, void *userdata, cha k++; } - *nodes = l; - l = NULL; + *nodes = TAKE_PTR(l); return k; } diff --git a/src/core/dynamic-user.c b/src/core/dynamic-user.c index de6aadd597..7236dcfabd 100644 --- a/src/core/dynamic-user.c +++ b/src/core/dynamic-user.c @@ -319,10 +319,7 @@ static int pick_uid(char **suggested_paths, const char *name, uid_t *ret_uid) { (void) make_uid_symlinks(candidate, name, true); /* also add direct lookup symlinks */ *ret_uid = candidate; - r = lock_fd; - lock_fd = -1; - - return r; + return TAKE_FD(lock_fd); next: ; @@ -770,8 +767,7 @@ int dynamic_user_lookup_uid(Manager *m, uid_t uid, char **ret) { if (check_uid != uid) /* lock file doesn't match our own idea */ return -ESRCH; - *ret = user; - user = NULL; + *ret = TAKE_PTR(user); return 0; } diff --git a/src/core/execute.c b/src/core/execute.c index 7292b815db..bfd3dfdafc 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -1733,8 +1733,7 @@ static int build_environment( our_env[n_env++] = NULL; assert(n_env <= 12); - *ret = our_env; - our_env = NULL; + *ret = TAKE_PTR(our_env); return 0; } @@ -1763,8 +1762,7 @@ static int build_pass_environment(const ExecContext *c, char ***ret) { x = NULL; } - *ret = pass_env; - pass_env = NULL; + *ret = TAKE_PTR(pass_env); return 0; } @@ -2290,9 +2288,7 @@ static int compile_bind_mounts( *ret_bind_mounts = bind_mounts; *ret_n_bind_mounts = n; - *ret_empty_directories = empty_directories; - - empty_directories = NULL; + *ret_empty_directories = TAKE_PTR(empty_directories); return (int) n; @@ -2696,8 +2692,7 @@ static int compile_suggested_paths(const ExecContext *c, const ExecParameters *p } } - *ret = list; - list = NULL; + *ret = TAKE_PTR(list); return 0; } diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index 94605ab0d0..887eb1cf49 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -1248,8 +1248,7 @@ int config_parse_exec_cpu_affinity(const char *unit, } if (!c->cpuset) { - c->cpuset = cpuset; - cpuset = NULL; + c->cpuset = TAKE_PTR(cpuset); c->cpuset_ncpus = (unsigned) ncpus; return 0; } @@ -1257,8 +1256,7 @@ int config_parse_exec_cpu_affinity(const char *unit, if (c->cpuset_ncpus < (unsigned) ncpus) { CPU_OR_S(CPU_ALLOC_SIZE(c->cpuset_ncpus), cpuset, c->cpuset, cpuset); CPU_FREE(c->cpuset); - c->cpuset = cpuset; - cpuset = NULL; + c->cpuset = TAKE_PTR(cpuset); c->cpuset_ncpus = (unsigned) ncpus; return 0; } @@ -2089,8 +2087,7 @@ int config_parse_user_group( return -ENOEXEC; } - n = k; - k = NULL; + n = TAKE_PTR(k); } free(*user); @@ -2320,10 +2317,8 @@ int config_parse_environ( "Failed to resolve specifiers, ignoring: %s", word); continue; } - } else { - k = word; - word = NULL; - } + } else + k = TAKE_PTR(word); if (!env_assignment_is_valid(k)) { log_syntax(unit, LOG_ERR, filename, line, 0, @@ -2390,10 +2385,8 @@ int config_parse_pass_environ( "Failed to resolve specifiers, ignoring: %s", word); continue; } - } else { - k = word; - word = NULL; - } + } else + k = TAKE_PTR(word); if (!env_name_is_valid(k)) { log_syntax(unit, LOG_ERR, filename, line, 0, @@ -2469,10 +2462,8 @@ int config_parse_unset_environ( "Failed to resolve specifiers, ignoring: %s", word); continue; } - } else { - k = word; - word = NULL; - } + } else + k = TAKE_PTR(word); if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) { log_syntax(unit, LOG_ERR, filename, line, 0, @@ -3515,8 +3506,7 @@ int config_parse_device_allow( if (!a) return log_oom(); - a->path = path; - path = NULL; + a->path = TAKE_PTR(path); a->r = !!strchr(m, 'r'); a->w = !!strchr(m, 'w'); a->m = !!strchr(m, 'm'); @@ -3615,8 +3605,7 @@ int config_parse_io_device_weight( if (!w) return log_oom(); - w->path = path; - path = NULL; + w->path = TAKE_PTR(path); w->weight = u; @@ -3701,8 +3690,7 @@ int config_parse_io_limit( if (!l) return log_oom(); - l->path = path; - path = NULL; + l->path = TAKE_PTR(path); for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++) l->limits[ttype] = cgroup_io_limit_defaults[ttype]; @@ -3804,8 +3792,7 @@ int config_parse_blockio_device_weight( if (!w) return log_oom(); - w->path = path; - path = NULL; + w->path = TAKE_PTR(path); w->weight = u; @@ -3885,8 +3872,7 @@ int config_parse_blockio_bandwidth( if (!b) return log_oom(); - b->path = path; - path = NULL; + b->path = TAKE_PTR(path); b->rbps = CGROUP_LIMIT_MAX; b->wbps = CGROUP_LIMIT_MAX; diff --git a/src/core/manager.c b/src/core/manager.c index 7026a58d73..9a71f5f2e7 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -844,8 +844,7 @@ static int manager_setup_notify(Manager *m) { if (r < 0) return log_error_errno(errno, "SO_PASSCRED failed: %m"); - m->notify_fd = fd; - fd = -1; + m->notify_fd = TAKE_FD(fd); log_debug("Using notification socket %s", m->notify_socket); } diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c index 9c27972aff..b7d654619c 100644 --- a/src/core/mount-setup.c +++ b/src/core/mount-setup.c @@ -315,10 +315,8 @@ int mount_cgroup_controllers(char ***join_controllers) { options = strv_join(*k, ","); if (!options) return log_oom(); - } else { - options = controller; - controller = NULL; - } + } else + options = TAKE_PTR(controller); where = strappend("/sys/fs/cgroup/", options); if (!where) diff --git a/src/core/service.c b/src/core/service.c index 23a5bcd1c4..588f08fef3 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -2554,8 +2554,7 @@ static int service_deserialize_exec_command(Unit *u, const char *key, const char state = STATE_EXEC_COMMAND_PATH; break; case STATE_EXEC_COMMAND_PATH: - path = arg; - arg = NULL; + path = TAKE_PTR(arg); state = STATE_EXEC_COMMAND_ARGS; if (!path_is_absolute(path)) diff --git a/src/core/socket.c b/src/core/socket.c index b3c09f67c0..eecf6fbe18 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -1238,10 +1238,7 @@ static int fifo_address_create( goto fail; } - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); fail: mac_selinux_create_file_clear(); @@ -1251,7 +1248,6 @@ fail: static int special_address_create(const char *path, bool writable) { _cleanup_close_ int fd = -1; struct stat st; - int r; assert(path); @@ -1266,16 +1262,12 @@ static int special_address_create(const char *path, bool writable) { if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) return -EEXIST; - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); } static int usbffs_address_create(const char *path) { _cleanup_close_ int fd = -1; struct stat st; - int r; assert(path); @@ -1290,10 +1282,7 @@ static int usbffs_address_create(const char *path) { if (!S_ISREG(st.st_mode)) return -EEXIST; - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); } static int mq_address_create( @@ -1306,7 +1295,6 @@ static int mq_address_create( struct stat st; mode_t old_mask; struct mq_attr _attr, *attr = NULL; - int r; assert(path); @@ -1338,10 +1326,7 @@ static int mq_address_create( st.st_gid != getgid()) return -EEXIST; - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); } static int socket_symlink(Socket *s) { diff --git a/src/core/unit.c b/src/core/unit.c index 52851b6ffc..90ec73231b 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -709,10 +709,8 @@ static int set_complete_move(Set **s, Set **other) { if (*s) return set_move(*s, *other); - else { - *s = *other; - *other = NULL; - } + else + *s = TAKE_PTR(*other); return 0; } @@ -726,10 +724,8 @@ static int hashmap_complete_move(Hashmap **s, Hashmap **other) { if (*s) return hashmap_move(*s, *other); - else { - *s = *other; - *other = NULL; - } + else + *s = TAKE_PTR(*other); return 0; } @@ -4017,8 +4013,7 @@ static int user_from_unit_name(Unit *u, char **ret) { return r; if (valid_user_group_name(n)) { - *ret = n; - n = NULL; + *ret = TAKE_PTR(n); return 0; } @@ -4220,7 +4215,7 @@ char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) { char* unit_concat_strv(char **l, UnitWriteFlags flags) { _cleanup_free_ char *result = NULL; size_t n = 0, allocated = 0; - char **i, *ret; + char **i; /* Takes a list of strings, escapes them, and concatenates them. This may be used to format command lines in a * way suitable for ExecStart= stanzas */ @@ -4255,10 +4250,7 @@ char* unit_concat_strv(char **l, UnitWriteFlags flags) { result[n] = 0; - ret = result; - result = NULL; - - return ret; + return TAKE_PTR(result); } int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data) { diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index e924750d1b..6fbfe58bd6 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -584,8 +584,7 @@ static int compose_open_fds(pid_t pid, char **open_fds) { if (errno > 0) return -errno; - *open_fds = buffer; - buffer = NULL; + *open_fds = TAKE_PTR(buffer); return 0; } diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 62bcbb6be3..a1b8b85e10 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -764,8 +764,7 @@ static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp) return log_error_errno(errno, "File \"%s\" is not readable: %m", filename); if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) { - *path = filename; - filename = NULL; + *path = TAKE_PTR(filename); return 0; } diff --git a/src/coredump/stacktrace.c b/src/coredump/stacktrace.c index 95fd27b79a..ec39fb8499 100644 --- a/src/coredump/stacktrace.c +++ b/src/coredump/stacktrace.c @@ -184,8 +184,7 @@ int coredump_make_stack_trace(int fd, const char *executable, char **ret) { c.f = safe_fclose(c.f); - *ret = buf; - buf = NULL; + *ret = TAKE_PTR(buf); r = 0; diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 7255ff418c..69bbf525fe 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -402,8 +402,7 @@ static int get_password(const char *vol, const char *src, usec_t until, bool acc *p = c; } - *ret = passwords; - passwords = NULL; + *ret = TAKE_PTR(passwords); return 0; } diff --git a/src/environment-d-generator/environment-d-generator.c b/src/environment-d-generator/environment-d-generator.c index bb4d76da90..9c16a60e2e 100644 --- a/src/environment-d-generator/environment-d-generator.c +++ b/src/environment-d-generator/environment-d-generator.c @@ -45,8 +45,7 @@ static int environment_dirs(char ***ret) { if (r < 0) return r; - *ret = dirs; - dirs = NULL; + *ret = TAKE_PTR(dirs); return 0; } diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 308ef9d750..a1f7e226f8 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -575,8 +575,7 @@ static int prompt_root_password(void) { continue; } - arg_root_password = a; - a = NULL; + arg_root_password = TAKE_PTR(a); break; } diff --git a/src/import/export-raw.c b/src/import/export-raw.c index eaa6d10915..9cab838b96 100644 --- a/src/import/export-raw.c +++ b/src/import/export-raw.c @@ -326,13 +326,10 @@ int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType /* Try to take a reflink snapshot of the file, if we can t make the export atomic */ tfd = reflink_snapshot(sfd, path); - if (tfd >= 0) { - e->input_fd = tfd; - tfd = -1; - } else { - e->input_fd = sfd; - sfd = -1; - } + if (tfd >= 0) + e->input_fd = TAKE_FD(tfd); + else + e->input_fd = TAKE_FD(sfd); r = import_compress_init(&e->compress, compress); if (r < 0) diff --git a/src/import/import-common.c b/src/import/import-common.c index a3dc1dde8c..b9d8c23905 100644 --- a/src/import/import-common.c +++ b/src/import/import-common.c @@ -117,13 +117,9 @@ int import_fork_tar_x(const char *path, pid_t *ret) { _exit(EXIT_FAILURE); } - pipefd[0] = safe_close(pipefd[0]); - r = pipefd[1]; - pipefd[1] = -1; - *ret = pid; - return r; + return TAKE_FD(pipefd[1]); } int import_fork_tar_c(const char *path, pid_t *ret) { @@ -165,11 +161,7 @@ int import_fork_tar_c(const char *path, pid_t *ret) { _exit(EXIT_FAILURE); } - pipefd[1] = safe_close(pipefd[1]); - r = pipefd[0]; - pipefd[0] = -1; - *ret = pid; - return r; + return TAKE_FD(pipefd[0]); } diff --git a/src/import/import-raw.c b/src/import/import-raw.c index f117c94da2..032b3dcda2 100644 --- a/src/import/import-raw.c +++ b/src/import/import-raw.c @@ -210,8 +210,7 @@ static int raw_import_maybe_convert_qcow2(RawImport *i) { free_and_replace(i->temp_path, t); safe_close(i->output_fd); - i->output_fd = converted_fd; - converted_fd = -1; + i->output_fd = TAKE_FD(converted_fd); return 1; } diff --git a/src/import/importd.c b/src/import/importd.c index 10f52c7fc1..55e61b485f 100644 --- a/src/import/importd.c +++ b/src/import/importd.c @@ -453,8 +453,7 @@ static int transfer_start(Transfer *t) { } pipefd[1] = safe_close(pipefd[1]); - t->log_fd = pipefd[0]; - pipefd[0] = -1; + t->log_fd = TAKE_FD(pipefd[0]); t->stdin_fd = safe_close(t->stdin_fd); @@ -1083,8 +1082,7 @@ static int transfer_node_enumerator(sd_bus *bus, const char *path, void *userdat k++; } - *nodes = l; - l = NULL; + *nodes = TAKE_PTR(l); return 1; } diff --git a/src/import/pull-common.c b/src/import/pull-common.c index 7651870bf0..92f2692c81 100644 --- a/src/import/pull-common.c +++ b/src/import/pull-common.c @@ -124,8 +124,7 @@ int pull_find_old_etags( return r; } - *etags = l; - l = NULL; + *etags = TAKE_PTR(l); return 0; } diff --git a/src/import/pull-raw.c b/src/import/pull-raw.c index 880cb84ba8..9784141871 100644 --- a/src/import/pull-raw.c +++ b/src/import/pull-raw.c @@ -272,8 +272,7 @@ static int raw_pull_maybe_convert_qcow2(RawPull *i) { free_and_replace(i->temp_path, t); safe_close(i->raw_job->disk_fd); - i->raw_job->disk_fd = converted_fd; - converted_fd = -1; + i->raw_job->disk_fd = TAKE_FD(converted_fd); return 1; } diff --git a/src/journal/journal-send.c b/src/journal/journal-send.c index 73329ba024..b6adf64c27 100644 --- a/src/journal/journal-send.c +++ b/src/journal/journal-send.c @@ -441,9 +441,7 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve if (r < 0) return r; - r = fd; - fd = -1; - return r; + return TAKE_FD(fd); } _public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) { diff --git a/src/journal/mmap-cache.c b/src/journal/mmap-cache.c index 630ae6fbac..91ed3cd519 100644 --- a/src/journal/mmap-cache.c +++ b/src/journal/mmap-cache.c @@ -220,8 +220,7 @@ static void context_detach_window(Context *c) { if (!c->window) return; - w = c->window; - c->window = NULL; + w = TAKE_PTR(c->window); LIST_REMOVE(by_window, w->contexts, c); if (!w->contexts && !w->keep_always) { diff --git a/src/libsystemd-network/arp-util.c b/src/libsystemd-network/arp-util.c index b8e9b2e496..3217ac0d1f 100644 --- a/src/libsystemd-network/arp-util.c +++ b/src/libsystemd-network/arp-util.c @@ -103,10 +103,7 @@ int arp_network_bind_raw_socket(int ifindex, be32_t address, const struct ether_ if (r < 0) return -errno; - r = s; - s = -1; - - return r; + return TAKE_FD(s); } static int arp_send_packet(int fd, int ifindex, diff --git a/src/libsystemd-network/dhcp-network.c b/src/libsystemd-network/dhcp-network.c index 602bf08a60..ac9a7a31b6 100644 --- a/src/libsystemd-network/dhcp-network.c +++ b/src/libsystemd-network/dhcp-network.c @@ -123,10 +123,7 @@ static int _bind_raw_socket(int ifindex, union sockaddr_union *link, if (r < 0) return -errno; - r = s; - s = -1; - - return r; + return TAKE_FD(s); } int dhcp_network_bind_raw_socket(int ifindex, union sockaddr_union *link, @@ -211,10 +208,7 @@ int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port) { if (r < 0) return -errno; - r = s; - s = -1; - - return r; + return TAKE_FD(s); } int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link, diff --git a/src/libsystemd-network/dhcp-option.c b/src/libsystemd-network/dhcp-option.c index 0489579e7f..142e39bc66 100644 --- a/src/libsystemd-network/dhcp-option.c +++ b/src/libsystemd-network/dhcp-option.c @@ -253,10 +253,8 @@ int dhcp_option_parse(DHCPMessage *message, size_t len, dhcp_option_callback_t c if (message_type == 0) return -ENOMSG; - if (_error_message && IN_SET(message_type, DHCP_NAK, DHCP_DECLINE)) { - *_error_message = error_message; - error_message = NULL; - } + if (_error_message && IN_SET(message_type, DHCP_NAK, DHCP_DECLINE)) + *_error_message = TAKE_PTR(error_message); return message_type; } diff --git a/src/libsystemd-network/dhcp6-network.c b/src/libsystemd-network/dhcp6-network.c index b3b8fddbcb..70f5909304 100644 --- a/src/libsystemd-network/dhcp6-network.c +++ b/src/libsystemd-network/dhcp6-network.c @@ -67,9 +67,7 @@ int dhcp6_network_bind_udp_socket(int index, struct in6_addr *local_address) { if (r < 0) return -errno; - r = s; - s = -1; - return r; + return TAKE_FD(s); } int dhcp6_network_send_udp_socket(int s, struct in6_addr *server_address, diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c index df96ad739d..2b3c8ceb59 100644 --- a/src/libsystemd-network/dhcp6-option.c +++ b/src/libsystemd-network/dhcp6-option.c @@ -617,8 +617,7 @@ int dhcp6_option_parse_domainname(const uint8_t *optval, uint16_t optlen, char * idx++; } - *str_arr = names; - names = NULL; + *str_arr = TAKE_PTR(names); return idx; diff --git a/src/libsystemd-network/icmp6-util.c b/src/libsystemd-network/icmp6-util.c index dd4e28bfd9..2347251572 100644 --- a/src/libsystemd-network/icmp6-util.c +++ b/src/libsystemd-network/icmp6-util.c @@ -98,9 +98,7 @@ static int icmp6_bind_router_message(const struct icmp6_filter *filter, if (r < 0) return -errno; - r = s; - s = -1; - return r; + return TAKE_FD(s); } int icmp6_bind_router_solicitation(int index) { diff --git a/src/libsystemd-network/lldp-network.c b/src/libsystemd-network/lldp-network.c index cb3841ef22..c653813cfb 100644 --- a/src/libsystemd-network/lldp-network.c +++ b/src/libsystemd-network/lldp-network.c @@ -92,8 +92,5 @@ int lldp_network_bind_raw_socket(int ifindex) { if (r < 0) return -errno; - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); } diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c index 584a1f36ac..00a56c8206 100644 --- a/src/libsystemd-network/network-internal.c +++ b/src/libsystemd-network/network-internal.c @@ -276,10 +276,9 @@ int config_parse_ifalias(const char *unit, } free(*s); - if (*n) { - *s = n; - n = NULL; - } else + if (*n) + *s = TAKE_PTR(n); + else *s = NULL; return 0; @@ -437,8 +436,7 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) { size++; } - *ret = addresses; - addresses = NULL; + *ret = TAKE_PTR(addresses); return size; } @@ -491,8 +489,7 @@ int deserialize_in6_addrs(struct in6_addr **ret, const char *string) { size++; } - *ret = addresses; - addresses = NULL; + *ret = TAKE_PTR(addresses); return size; } @@ -585,8 +582,7 @@ int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_size = size; *ret_allocated = allocated; - *ret = routes; - routes = NULL; + *ret = TAKE_PTR(routes); return 0; } diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index 2e88e39878..9db0a93898 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -807,8 +807,7 @@ int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***d pos = next_chunk; } - *domains = names; - names = NULL; + *domains = TAKE_PTR(names); return cnt; } diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c index 6e00255b20..08015a99ef 100644 --- a/src/libsystemd/sd-bus/bus-objects.c +++ b/src/libsystemd/sd-bus/bus-objects.c @@ -1470,8 +1470,7 @@ static struct node *bus_node_allocate(sd_bus *bus, const char *path) { return NULL; n->parent = parent; - n->path = s; - s = NULL; /* do not free */ + n->path = TAKE_PTR(s); r = hashmap_put(bus->nodes, n->path, n); if (r < 0) { diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 2f5e483ae2..8c022fce1b 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -1342,8 +1342,7 @@ int bus_set_address_user(sd_bus *b) { if (asprintf(&s, DEFAULT_USER_BUS_ADDRESS_FMT, ee) < 0) return -ENOMEM; - b->address = s; - s = NULL; + b->address = TAKE_PTR(s); return 0; } diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c index 1334498ca4..0da0ca5890 100644 --- a/src/libsystemd/sd-daemon/sd-daemon.c +++ b/src/libsystemd/sd-daemon/sd-daemon.c @@ -141,8 +141,7 @@ _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) { return r; } - *names = l; - l = NULL; + *names = TAKE_PTR(l); return n_fds; } diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 1297dfa911..fd2821b1a7 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -1297,8 +1297,7 @@ int device_get_id_filename(sd_device *device, const char **ret) { } } - device->id_filename = id; - id = NULL; + device->id_filename = TAKE_PTR(id); } *ret = device->id_filename; diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index 6601bcd6be..2a4eede9f8 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -325,8 +325,7 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) { if (isempty(s)) return -ENODATA; - *session = s; - s = NULL; + *session = TAKE_PTR(s); return 0; } @@ -355,8 +354,7 @@ static int file_of_seat(const char *seat, char **_p) { if (!p) return -ENOMEM; - *_p = p; - p = NULL; + *_p = TAKE_PTR(p); return 0; } @@ -529,8 +527,7 @@ _public_ int sd_session_get_state(const char *session, char **state) { if (isempty(s)) return -EIO; - *state = s; - s = NULL; + *state = TAKE_PTR(s); return 0; } @@ -575,8 +572,7 @@ static int session_get_string(const char *session, const char *field, char **val if (isempty(s)) return -ENODATA; - *value = s; - s = NULL; + *value = TAKE_PTR(s); return 0; } @@ -681,10 +677,8 @@ _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) { return r; } - if (session && s) { - *session = s; - s = NULL; - } + if (session && s) + *session = TAKE_PTR(s); return 0; } @@ -909,10 +903,9 @@ _public_ int sd_get_machine_names(char ***machines) { *b = NULL; } - if (machines) { - *machines = l; - l = NULL; - } + if (machines) + *machines = TAKE_PTR(l); + return r; } @@ -933,8 +926,7 @@ _public_ int sd_machine_get_class(const char *machine, char **class) { if (!c) return -EIO; - *class = c; - c = NULL; + *class = TAKE_PTR(c); return 0; } diff --git a/src/libsystemd/sd-netlink/netlink-message.c b/src/libsystemd/sd-netlink/netlink-message.c index af3d13edcd..3a5f9346aa 100644 --- a/src/libsystemd/sd-netlink/netlink-message.c +++ b/src/libsystemd/sd-netlink/netlink-message.c @@ -803,8 +803,7 @@ static int netlink_container_parse(sd_netlink_message *m, attributes[type].net_byteorder = RTA_FLAGS(rta) & NLA_F_NET_BYTEORDER; } - container->attributes = attributes; - attributes = NULL; + container->attributes = TAKE_PTR(attributes); container->n_attributes = count; return 0; diff --git a/src/libsystemd/sd-network/sd-network.c b/src/libsystemd/sd-network/sd-network.c index 8f4814019e..1d3e9b30f5 100644 --- a/src/libsystemd/sd-network/sd-network.c +++ b/src/libsystemd/sd-network/sd-network.c @@ -51,8 +51,7 @@ _public_ int sd_network_get_operational_state(char **state) { if (isempty(s)) return -ENODATA; - *state = s; - s = NULL; + *state = TAKE_PTR(s); return 0; } @@ -81,8 +80,7 @@ static int network_get_strv(const char *key, char ***ret) { strv_uniq(a); r = strv_length(a); - *ret = a; - a = NULL; + *ret = TAKE_PTR(a); return r; } @@ -121,8 +119,7 @@ static int network_link_get_string(int ifindex, const char *field, char **ret) { if (isempty(s)) return -ENODATA; - *ret = s; - s = NULL; + *ret = TAKE_PTR(s); return 0; } @@ -154,8 +151,7 @@ static int network_link_get_strv(int ifindex, const char *key, char ***ret) { strv_uniq(a); r = strv_length(a); - *ret = a; - a = NULL; + *ret = TAKE_PTR(a); return r; } @@ -263,8 +259,7 @@ static int network_link_get_ifindexes(int ifindex, const char *key, int **ret) { if (ifis) ifis[c] = 0; /* Let's add a 0 ifindex to the end, to be nice */ - *ret = ifis; - ifis = NULL; + *ret = TAKE_PTR(ifis); return c; } diff --git a/src/libudev/libudev-enumerate.c b/src/libudev/libudev-enumerate.c index 271880075e..a366ab16be 100644 --- a/src/libudev/libudev-enumerate.c +++ b/src/libudev/libudev-enumerate.c @@ -69,7 +69,6 @@ struct udev_enumerate { **/ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) { _cleanup_free_ struct udev_enumerate *udev_enumerate = NULL; - struct udev_enumerate *ret; int r; assert_return_errno(udev, NULL, EINVAL); @@ -97,10 +96,7 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) { udev_list_init(udev, &udev_enumerate->devices_list, false); - ret = udev_enumerate; - udev_enumerate = NULL; - - return ret; + return TAKE_PTR(udev_enumerate); } /** diff --git a/src/locale/keymap-util.c b/src/locale/keymap-util.c index 2d788106bb..393e9b5648 100644 --- a/src/locale/keymap-util.c +++ b/src/locale/keymap-util.c @@ -278,8 +278,7 @@ int locale_write_data(Context *c, char ***settings) { if (r < 0) return r; - *settings = l; - l = NULL; + *settings = TAKE_PTR(l); return 0; } @@ -539,8 +538,7 @@ int find_converted_keymap(const char *x11_layout, const char *x11_variant, char log_debug("Found converted keymap %s at %s", n, uncompressed ? p : pz); - *new_keymap = n; - n = NULL; + *new_keymap = TAKE_PTR(n); return 1; } } diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c index 46e7c06ddc..3e89b0f124 100644 --- a/src/login/logind-seat.c +++ b/src/login/logind-seat.c @@ -560,8 +560,7 @@ void seat_complete_switch(Seat *s) { if (!s->pending_switch) return; - session = s->pending_switch; - s->pending_switch = NULL; + session = TAKE_PTR(s->pending_switch); seat_set_active(s, session); } diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 1859150b5e..5673ea4f9e 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -1145,8 +1145,7 @@ void session_restore_vt(Session *s) { * little dance to avoid having the terminal be available * for reuse before we've cleaned it up. */ - old_fd = s->vtfd; - s->vtfd = -1; + old_fd = TAKE_FD(s->vtfd); vt = session_open_vt(s); safe_close(old_fd); @@ -1271,8 +1270,7 @@ int session_set_controller(Session *s, const char *sender, bool force, bool prep } session_release_controller(s, true); - s->controller = name; - name = NULL; + s->controller = TAKE_PTR(name); session_save(s); return 0; diff --git a/src/login/logind.c b/src/login/logind.c index 95b29ae85e..6d3b7cee23 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -438,8 +438,7 @@ static int parse_fdname(const char *fdname, char **session_id, dev_t *dev) { return r; *dev = makedev(major, minor); - *session_id = id; - id = NULL; + *session_id = TAKE_PTR(id); return 0; } diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index 9c435d6715..fb053343c1 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -1475,8 +1475,7 @@ int machine_node_enumerator(sd_bus *bus, const char *path, void *userdata, char return r; } - *nodes = l; - l = NULL; + *nodes = TAKE_PTR(l); return 1; } diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index dd139740c2..046def33ae 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -766,16 +766,14 @@ static int find_mount_points(const char *what, char ***list) { if (!GREEDY_REALLOC(l, bufsize, n + 2)) return log_oom(); - l[n++] = where; - where = NULL; + l[n++] = TAKE_PTR(where); } if (!GREEDY_REALLOC(l, bufsize, n + 1)) return log_oom(); l[n] = NULL; - *list = l; - l = NULL; /* avoid freeing */ + *list = TAKE_PTR(l); return n; } @@ -827,8 +825,7 @@ static int find_loop_device(const char *backing_file, char **loop_dev) { if (!l) return -ENXIO; - *loop_dev = l; - l = NULL; /* avoid freeing */ + *loop_dev = TAKE_PTR(l); return 0; } diff --git a/src/network/netdev/netdev.c b/src/network/netdev/netdev.c index 93648e1be0..421a32d567 100644 --- a/src/network/netdev/netdev.c +++ b/src/network/netdev/netdev.c @@ -488,8 +488,7 @@ int netdev_get_mac(const char *ifname, struct ether_addr **ret) { mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */ mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */ - *ret = mac; - mac = NULL; + *ret = TAKE_PTR(mac); return 0; } diff --git a/src/network/netdev/wireguard.c b/src/network/netdev/wireguard.c index f1f4bab475..9025693f70 100644 --- a/src/network/netdev/wireguard.c +++ b/src/network/netdev/wireguard.c @@ -228,8 +228,7 @@ static int on_resolve_retry(sd_event_source *s, usec_t usec, void *userdata) { w->resolve_retry_event_source = sd_event_source_unref(w->resolve_retry_event_source); - w->unresolved_endpoints = w->failed_endpoints; - w->failed_endpoints = NULL; + w->unresolved_endpoints = TAKE_PTR(w->failed_endpoints); resolve_endpoints(netdev); diff --git a/src/network/networkd-link-bus.c b/src/network/networkd-link-bus.c index 2f4850b1a1..cc966fa363 100644 --- a/src/network/networkd-link-bus.c +++ b/src/network/networkd-link-bus.c @@ -82,8 +82,7 @@ int link_node_enumerator(sd_bus *bus, const char *path, void *userdata, char *** } l[c] = NULL; - *nodes = l; - l = NULL; + *nodes = TAKE_PTR(l); return 1; } diff --git a/src/network/networkd-network-bus.c b/src/network/networkd-network-bus.c index 8d3d7236fc..459868a57f 100644 --- a/src/network/networkd-network-bus.c +++ b/src/network/networkd-network-bus.c @@ -122,8 +122,7 @@ int network_node_enumerator(sd_bus *bus, const char *path, void *userdata, char return r; } - *nodes = l; - l = NULL; + *nodes = TAKE_PTR(l); return 1; } diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index fc9fbed6f1..e3ed24e600 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -72,8 +72,7 @@ int network_config_section_new(const char *filename, unsigned line, NetworkConfi strcpy(cs->filename, filename); cs->line = line; - *s = cs; - cs = NULL; + *s = TAKE_PTR(cs); return 0; } diff --git a/src/network/networkd-radv.c b/src/network/networkd-radv.c index a921d120b3..1cc89f3b0a 100644 --- a/src/network/networkd-radv.c +++ b/src/network/networkd-radv.c @@ -126,8 +126,7 @@ int prefix_new(Prefix **ret) { if (sd_radv_prefix_new(&prefix->radv_prefix) < 0) return -ENOMEM; - *ret = prefix; - prefix = NULL; + *ret = TAKE_PTR(prefix); return 0; } @@ -344,8 +343,7 @@ static int radv_get_ip6dns(Network *network, struct in6_addr **dns, } if (addresses) { - *dns = addresses; - addresses = NULL; + *dns = TAKE_PTR(addresses); *n_dns = n_addresses; } diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index fdbaec58eb..d8f712c069 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -857,8 +857,7 @@ static int routing_policy_rule_read_full_file(const char *state_file, char **ret if (size <= 0) return -ENODATA; - *ret = s; - s = NULL; + *ret = TAKE_PTR(s); return size; } diff --git a/src/nspawn/nspawn-mount.c b/src/nspawn/nspawn-mount.c index 0ee69114b2..e32c6223d7 100644 --- a/src/nspawn/nspawn-mount.c +++ b/src/nspawn/nspawn-mount.c @@ -308,8 +308,7 @@ int overlay_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_o !source_path_is_valid(lower[1])) return -EINVAL; - upper = lower[1]; - lower[1] = NULL; + upper = TAKE_PTR(lower[1]); destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */ if (!destination) @@ -321,8 +320,7 @@ int overlay_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_o * the "upper", and all before that the "lower" directories. */ destination = lower[k - 1]; - upper = lower[k - 2]; - lower[k - 2] = NULL; + upper = TAKE_PTR(lower[k - 2]); STRV_FOREACH(i, lower) if (!source_path_is_valid(*i)) diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 49325f0d22..384b1ea5df 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -1175,8 +1175,7 @@ static int parse_argv(int argc, char *argv[]) { * accept this here, and silently make "--ephemeral --template=" equivalent to "--ephemeral * --directory=". */ - arg_directory = arg_template; - arg_template = NULL; + arg_directory = TAKE_PTR(arg_template); } if (arg_template && !(arg_directory || arg_machine)) { diff --git a/src/resolve/resolved-dns-scope.c b/src/resolve/resolved-dns-scope.c index bf6aac8300..305bb6afe8 100644 --- a/src/resolve/resolved-dns-scope.c +++ b/src/resolve/resolved-dns-scope.c @@ -325,7 +325,7 @@ static int dns_scope_socket( union sockaddr_union sa = {}; socklen_t salen; static const int one = 1; - int ret, r, ifindex; + int r, ifindex; assert(s); @@ -409,10 +409,7 @@ static int dns_scope_socket( if (r < 0 && errno != EINPROGRESS) return -errno; - ret = fd; - fd = -1; - - return ret; + return TAKE_FD(fd); } int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) { diff --git a/src/resolve/resolved-dns-search-domain.c b/src/resolve/resolved-dns-search-domain.c index 585c518e19..03f7536fd2 100644 --- a/src/resolve/resolved-dns-search-domain.c +++ b/src/resolve/resolved-dns-search-domain.c @@ -56,8 +56,7 @@ int dns_search_domain_new( d->n_ref = 1; d->manager = m; d->type = type; - d->name = normalized; - normalized = NULL; + d->name = TAKE_PTR(normalized); switch (type) { diff --git a/src/resolve/resolved-dns-stub.c b/src/resolve/resolved-dns-stub.c index 5ce8d240dd..20ff8a1d3b 100644 --- a/src/resolve/resolved-dns-stub.c +++ b/src/resolve/resolved-dns-stub.c @@ -447,10 +447,8 @@ static int manager_dns_stub_udp_fd(Manager *m) { return r; (void) sd_event_source_set_description(m->dns_stub_udp_event_source, "dns-stub-udp"); - m->dns_stub_udp_fd = fd; - fd = -1; - return m->dns_stub_udp_fd; + return m->dns_stub_udp_fd = TAKE_FD(fd); } static int on_dns_stub_stream_packet(DnsStream *s) { @@ -542,10 +540,8 @@ static int manager_dns_stub_tcp_fd(Manager *m) { return r; (void) sd_event_source_set_description(m->dns_stub_tcp_event_source, "dns-stub-tcp"); - m->dns_stub_tcp_fd = fd; - fd = -1; - return m->dns_stub_tcp_fd; + return m->dns_stub_tcp_fd = TAKE_FD(fd); } int manager_dns_stub_start(Manager *m) { diff --git a/src/resolve/resolved-dns-zone.c b/src/resolve/resolved-dns-zone.c index dcb9702e57..4888eccbd3 100644 --- a/src/resolve/resolved-dns-zone.c +++ b/src/resolve/resolved-dns-zone.c @@ -36,8 +36,7 @@ void dns_zone_item_probe_stop(DnsZoneItem *i) { if (!i->probe_transaction) return; - t = i->probe_transaction; - i->probe_transaction = NULL; + t = TAKE_PTR(i->probe_transaction); set_remove(t->notify_zone_items, i); set_remove(t->notify_zone_items_done, i); diff --git a/src/resolve/resolved-dnssd-bus.c b/src/resolve/resolved-dnssd-bus.c index c914e8f8d2..1a70968dd1 100644 --- a/src/resolve/resolved-dnssd-bus.c +++ b/src/resolve/resolved-dnssd-bus.c @@ -139,8 +139,7 @@ int dnssd_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ** } l[c] = NULL; - *nodes = l; - l = NULL; + *nodes = TAKE_PTR(l); return 1; } diff --git a/src/resolve/resolved-dnssd.c b/src/resolve/resolved-dnssd.c index db589f4436..18e61aaee3 100644 --- a/src/resolve/resolved-dnssd.c +++ b/src/resolve/resolved-dnssd.c @@ -198,8 +198,7 @@ int dnssd_render_instance_name(DnssdService *s, char **ret_name) { return -EINVAL; } - *ret_name = name; - name = NULL; + *ret_name = TAKE_PTR(name); return 0; } @@ -319,8 +318,7 @@ int dnssd_txt_item_new_from_string(const char *key, const char *value, DnsTxtIte } i->length = length; - *ret_item = i; - i = NULL; + *ret_item = TAKE_PTR(i); return 0; } @@ -345,8 +343,7 @@ int dnssd_txt_item_new_from_data(const char *key, const void *data, const size_t } i->length = length; - *ret_item = i; - i = NULL; + *ret_item = TAKE_PTR(i); return 0; } diff --git a/src/resolve/resolved-link-bus.c b/src/resolve/resolved-link-bus.c index 82a29289d2..7e7308a819 100644 --- a/src/resolve/resolved-link-bus.c +++ b/src/resolve/resolved-link-bus.c @@ -496,8 +496,7 @@ int bus_link_method_set_dnssec_negative_trust_anchors(sd_bus_message *message, v } set_free_free(l->dnssec_negative_trust_anchors); - l->dnssec_negative_trust_anchors = ns; - ns = NULL; + l->dnssec_negative_trust_anchors = TAKE_PTR(ns); (void) link_save_user(l); @@ -621,8 +620,7 @@ int link_node_enumerator(sd_bus *bus, const char *path, void *userdata, char *** } l[c] = NULL; - *nodes = l; - l = NULL; + *nodes = TAKE_PTR(l); return 1; } diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c index e3e50eca53..4d87e88cdf 100644 --- a/src/resolve/resolved-link.c +++ b/src/resolve/resolved-link.c @@ -440,8 +440,7 @@ static int link_update_dnssec_negative_trust_anchors(Link *l) { return r; set_free_free(l->dnssec_negative_trust_anchors); - l->dnssec_negative_trust_anchors = ns; - ns = NULL; + l->dnssec_negative_trust_anchors = TAKE_PTR(ns); return 0; @@ -1293,8 +1292,7 @@ int link_load_user(Link *l) { if (r < 0) goto fail; - l->dnssec_negative_trust_anchors = ns; - ns = NULL; + l->dnssec_negative_trust_anchors = TAKE_PTR(ns); } return 0; diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 2ee027791a..de1f0ce21d 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -396,11 +396,8 @@ static int determine_hostname(char **full_hostname, char **llmnr_hostname, char if (r < 0) return log_error_errno(r, "Failed to determine mDNS hostname: %m"); - *llmnr_hostname = n; - n = NULL; - - *full_hostname = h; - h = NULL; + *llmnr_hostname = TAKE_PTR(n); + *full_hostname = TAKE_PTR(h); return 0; } @@ -445,11 +442,8 @@ static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname, if (!h) return log_oom(); - *llmnr_hostname = n; - n = NULL; - - *mdns_hostname = m; - m = NULL; + *llmnr_hostname = TAKE_PTR(n); + *mdns_hostname = TAKE_PTR(m); *full_hostname = h; diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index 4fa9188957..88cafd4940 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -730,8 +730,7 @@ int ask_password_agent( if (keyname) (void) add_to_keyring_and_log(keyname, flags, l); - *ret = l; - l = NULL; + *ret = TAKE_PTR(l); r = 0; finish: diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 8061af1865..803f75335c 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -1094,8 +1094,7 @@ static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_ return r; strv_free(*p); - *p = l; - l = NULL; + *p = TAKE_PTR(l); return 0; } diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c index 0ddae95434..326a398075 100644 --- a/src/shared/cgroup-show.c +++ b/src/shared/cgroup-show.c @@ -189,8 +189,7 @@ int show_cgroup_by_path( free(last); } - last = k; - k = NULL; + last = TAKE_PTR(k); } if (r < 0) diff --git a/src/shared/dns-domain.c b/src/shared/dns-domain.c index 8c807e0e23..89d18431e4 100644 --- a/src/shared/dns-domain.c +++ b/src/shared/dns-domain.c @@ -295,8 +295,7 @@ int dns_label_escape_new(const char *p, size_t l, char **ret) { if (r < 0) return r; - *ret = s; - s = NULL; + *ret = TAKE_PTR(s); return r; } @@ -601,8 +600,7 @@ int dns_name_endswith(const char *name, const char *suffix) { /* Not the same, let's jump back, and try with the next label again */ s = suffix; - n = saved_n; - saved_n = NULL; + n = TAKE_PTR(saved_n); } } } diff --git a/src/shared/efivars.c b/src/shared/efivars.c index 9ca51cf750..d31cf2d860 100644 --- a/src/shared/efivars.c +++ b/src/shared/efivars.c @@ -249,8 +249,7 @@ int efi_get_variable( ((char*) buf)[st.st_size - 4] = 0; ((char*) buf)[st.st_size - 4 + 1] = 0; - *value = buf; - buf = NULL; + *value = TAKE_PTR(buf); *size = (size_t) st.st_size - 4; if (attribute) @@ -563,8 +562,7 @@ int efi_get_boot_order(uint16_t **order) { l / sizeof(uint16_t) > INT_MAX) return -EINVAL; - *order = buf; - buf = NULL; + *order = TAKE_PTR(buf); return (int) (l / sizeof(uint16_t)); } diff --git a/src/shared/fdset.c b/src/shared/fdset.c index 9ce1295223..845c19b1cf 100644 --- a/src/shared/fdset.c +++ b/src/shared/fdset.c @@ -25,6 +25,7 @@ #include "sd-daemon.h" +#include "alloc-util.h" #include "dirent-util.h" #include "fd-util.h" #include "fdset.h" @@ -168,8 +169,7 @@ int fdset_new_fill(FDSet **_s) { } r = 0; - *_s = s; - s = NULL; + *_s = TAKE_PTR(s); finish: /* We won't close the fds here! */ diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index bcd7b43084..1a3bc46d2a 100644 --- a/src/shared/fstab-util.c +++ b/src/shared/fstab-util.c @@ -169,10 +169,8 @@ answer: *filtered = f; } - if (value) { - *value = v; - v = NULL; - } + if (value) + *value = TAKE_PTR(v); return !!n; } @@ -201,8 +199,7 @@ int fstab_extract_values(const char *opts, const char *name, char ***values) { return r; } - *values = res; - res = NULL; + *values = TAKE_PTR(res); return !!*values; } diff --git a/src/shared/import-util.c b/src/shared/import-util.c index 07ba216e93..5f71add506 100644 --- a/src/shared/import-util.c +++ b/src/shared/import-util.c @@ -153,8 +153,7 @@ int raw_strip_suffixes(const char *p, char **ret) { break; } - *ret = q; - q = NULL; + *ret = TAKE_PTR(q); return 0; } diff --git a/src/shared/install-printf.c b/src/shared/install-printf.c index aaab2e6665..5135d0671e 100644 --- a/src/shared/install-printf.c +++ b/src/shared/install-printf.c @@ -50,10 +50,8 @@ static int specifier_prefix_and_instance(char specifier, void *data, void *userd if (!ans) return -ENOMEM; *ret = ans; - } else { - *ret = prefix; - prefix = NULL; - } + } else + *ret = TAKE_PTR(prefix); return 0; } diff --git a/src/shared/install.c b/src/shared/install.c index ed5f51cc46..7506018d41 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -1422,8 +1422,7 @@ static int unit_file_search( r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags); if (r >= 0) { - info->path = path; - path = NULL; + info->path = TAKE_PTR(path); result = r; found_unit = true; break; @@ -1446,8 +1445,7 @@ static int unit_file_search( r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags); if (r >= 0) { - info->path = path; - path = NULL; + info->path = TAKE_PTR(path); result = r; found_unit = true; break; @@ -1754,8 +1752,7 @@ static int install_info_symlink_wants( if (r < 0) return r; - path = instance.path; - instance.path = NULL; + path = TAKE_PTR(instance.path); if (instance.type == UNIT_FILE_TYPE_MASKED) { unit_file_changes_add(changes, n_changes, -ERFKILL, path, NULL); diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c index 66eefb3036..f48b6c693c 100644 --- a/src/shared/machine-image.c +++ b/src/shared/machine-image.c @@ -80,7 +80,6 @@ Image *image_unref(Image *i) { static char **image_settings_path(Image *image) { _cleanup_strv_free_ char **l = NULL; - char **ret; const char *fn, *s; unsigned i = 0; @@ -104,10 +103,7 @@ static char **image_settings_path(Image *image) { if (!l[i]) return NULL; - ret = l; - l = NULL; - - return ret; + return TAKE_PTR(l); } static char *image_roothash_path(Image *image) { diff --git a/src/shared/machine-pool.c b/src/shared/machine-pool.c index 031d443e9e..68e5bd19a0 100644 --- a/src/shared/machine-pool.c +++ b/src/shared/machine-pool.c @@ -89,11 +89,8 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) { * /var/lib/machines. */ fd = open("/var/lib/machines.raw", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); - if (fd >= 0) { - r = fd; - fd = -1; - return r; - } + if (fd >= 0) + return TAKE_FD(fd); if (errno != ENOENT) return sd_bus_error_set_errnof(error, errno, "Failed to open /var/lib/machines.raw: %m"); @@ -162,10 +159,7 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) { goto fail; } - r = fd; - fd = -1; - - return r; + return TAKE_FD(fd); fail: unlink_noerrno(tmp); diff --git a/src/shared/nsflags.c b/src/shared/nsflags.c index 05ec9feb8d..a8ddc9a57f 100644 --- a/src/shared/nsflags.c +++ b/src/shared/nsflags.c @@ -114,8 +114,7 @@ int namespace_flag_to_string_many(unsigned long flags, char **ret) { return -ENOMEM; } - *ret = s; - s = NULL; + *ret = TAKE_PTR(s); return 0; } diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c index 5f31831c99..a472e80aec 100644 --- a/src/shared/path-lookup.c +++ b/src/shared/path-lookup.c @@ -181,7 +181,6 @@ static char** user_dirs( _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; _cleanup_free_ char *data_home = NULL; _cleanup_strv_free_ char **res = NULL; - char **tmp; int r; r = xdg_user_dirs(&config_dirs, &data_dirs); @@ -242,10 +241,7 @@ static char** user_dirs( if (path_strv_make_absolute_cwd(res) < 0) return NULL; - tmp = res; - res = NULL; - - return tmp; + return TAKE_PTR(res); } bool path_is_user_data_dir(const char *path) { @@ -374,8 +370,7 @@ static int acquire_config_dirs(UnitFileScope scope, char **persistent, char **ru *runtime = NULL; } - *persistent = a; - a = NULL; + *persistent = TAKE_PTR(a); return 0; @@ -413,8 +408,7 @@ static int acquire_control_dirs(UnitFileScope scope, char **persistent, char **r if (!b) return -ENOMEM; - *runtime = b; - b = NULL; + *runtime = TAKE_PTR(b); break; } @@ -443,8 +437,7 @@ static int acquire_control_dirs(UnitFileScope scope, char **persistent, char **r assert_not_reached("Hmm, unexpected scope value."); } - *persistent = a; - a = NULL; + *persistent = TAKE_PTR(a); return 0; } diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c index 1e1a7e57ad..f65eed87f2 100644 --- a/src/shared/sleep-config.c +++ b/src/shared/sleep-config.c @@ -40,8 +40,6 @@ #include "string-util.h" #include "strv.h" -#define USE(x, y) do { (x) = (y); (y) = NULL; } while (0) - int parse_sleep_config(const char *verb, char ***_modes, char ***_states, usec_t *_delay) { _cleanup_strv_free_ char @@ -69,32 +67,32 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states, usec_t if (streq(verb, "suspend")) { /* empty by default */ - USE(modes, suspend_mode); + modes = TAKE_PTR(suspend_mode); if (suspend_state) - USE(states, suspend_state); + states = TAKE_PTR(suspend_state); else states = strv_new("mem", "standby", "freeze", NULL); } else if (streq(verb, "hibernate")) { if (hibernate_mode) - USE(modes, hibernate_mode); + modes = TAKE_PTR(hibernate_mode); else modes = strv_new("platform", "shutdown", NULL); if (hibernate_state) - USE(states, hibernate_state); + states = TAKE_PTR(hibernate_state); else states = strv_new("disk", NULL); } else if (streq(verb, "hybrid-sleep")) { if (hybrid_mode) - USE(modes, hybrid_mode); + modes = TAKE_PTR(hybrid_mode); else modes = strv_new("suspend", "platform", "shutdown", NULL); if (hybrid_state) - USE(states, hybrid_state); + states = TAKE_PTR(hybrid_state); else states = strv_new("disk", NULL); diff --git a/src/shared/specifier.c b/src/shared/specifier.c index 23aaa88c4b..98d95eef8e 100644 --- a/src/shared/specifier.c +++ b/src/shared/specifier.c @@ -117,8 +117,7 @@ int specifier_printf(const char *text, const Specifier table[], void *userdata, *(t++) = '%'; *t = 0; - *_ret = ret; - ret = NULL; + *_ret = TAKE_PTR(ret); return 0; } diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 064d593131..d23bddc99a 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -1924,8 +1924,7 @@ static int get_machine_list( return log_oom(); machine_infos[c].is_host = true; - machine_infos[c].name = hn; - hn = NULL; + machine_infos[c].name = TAKE_PTR(hn); (void) get_machine_properties(bus, &machine_infos[c]); c++; @@ -2462,10 +2461,9 @@ static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **un if (r < 0) return log_error_errno(r, "Failed to access path '%s': %m", path); - if (unit_path) { - *unit_path = lpath; - lpath = NULL; - } + if (unit_path) + *unit_path = TAKE_PTR(lpath); + return 1; } @@ -2497,10 +2495,9 @@ static int unit_find_template_path( if (r < 0) return r; - if (template) { - *template = _template; - _template = NULL; - } + if (template) + *template = TAKE_PTR(_template); + return r; } @@ -6671,8 +6668,7 @@ static int create_edit_temp_file(const char *new_path, const char *original_path } else if (r < 0) return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); - *ret_tmp_fn = t; - t = NULL; + *ret_tmp_fn = TAKE_PTR(t); return 0; } @@ -6703,12 +6699,9 @@ static int get_file_to_edit( return -EEXIST; } - *ret_path = run; - run = NULL; - } else { - *ret_path = path; - path = NULL; - } + *ret_path = TAKE_PTR(run); + } else + *ret_path = TAKE_PTR(path); return 0; } diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c index 92669c6758..394a04bb88 100644 --- a/src/sysv-generator/sysv-generator.c +++ b/src/sysv-generator/sysv-generator.c @@ -741,8 +741,7 @@ static int acquire_search_path(const char *def, const char *envvar, char ***ret) if (!path_strv_resolve_uniq(l, NULL)) return log_oom(); - *ret = l; - l = NULL; + *ret = TAKE_PTR(l); return 0; } diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index f1890f3261..1794290ebe 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -310,8 +310,7 @@ static int user_config_paths(char*** ret) { if (r < 0) return r; - *ret = res; - res = NULL; + *ret = TAKE_PTR(res); return 0; } diff --git a/src/udev/net/ethtool-util.c b/src/udev/net/ethtool-util.c index 1fdfc85408..69ac3e8c58 100644 --- a/src/udev/net/ethtool-util.c +++ b/src/udev/net/ethtool-util.c @@ -304,8 +304,7 @@ static int get_stringset(int fd, struct ifreq *ifr, int stringset_id, struct eth if (r < 0) return -errno; - *gstrings = strings; - strings = NULL; + *gstrings = TAKE_PTR(strings); return 0; } diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c index 0a32363deb..7d967e04de 100644 --- a/src/udev/udev-builtin-path_id.c +++ b/src/udev/udev-builtin-path_id.c @@ -62,10 +62,8 @@ static void path_prepend(char **path, const char *fmt, ...) { } free_and_replace(*path, new); - } else { - *path = pre; - pre = NULL; - } + } else + *path = TAKE_PTR(pre); } /* diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 615c4ed3e2..34976b30e0 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -233,8 +233,7 @@ static int worker_new(struct worker **ret, Manager *manager, struct udev_monitor if (r < 0) return r; - *ret = worker; - worker = NULL; + *ret = TAKE_PTR(worker); return 0; } diff --git a/src/vconsole/vconsole-setup.c b/src/vconsole/vconsole-setup.c index a9cc2bf63c..e7fced247d 100644 --- a/src/vconsole/vconsole-setup.c +++ b/src/vconsole/vconsole-setup.c @@ -49,35 +49,42 @@ #include "virt.h" static int verify_vc_device(int fd) { - unsigned char data[1]; + unsigned char data[] = { + TIOCL_GETFGCONSOLE, + }; + int r; - data[0] = TIOCL_GETFGCONSOLE; r = ioctl(fd, TIOCLINUX, data); - return r < 0 ? -errno : r; + if (r < 0) + return -errno; + + return r; } static int verify_vc_allocation(unsigned idx) { char vcname[sizeof("/dev/vcs") + DECIMAL_STR_MAX(unsigned) - 2]; - int r; xsprintf(vcname, "/dev/vcs%u", idx); - r = access(vcname, F_OK); - return r < 0 ? -errno : r; + + if (access(vcname, F_OK) < 0) + return -errno; + + return 0; } static int verify_vc_allocation_byfd(int fd) { struct vt_stat vcs = {}; - int r; - r = ioctl(fd, VT_GETSTATE, &vcs); - return r < 0 ? -errno : verify_vc_allocation(vcs.v_active); + if (ioctl(fd, VT_GETSTATE, &vcs) < 0) + return -errno; + + return verify_vc_allocation(vcs.v_active); } static int verify_vc_kbmode(int fd) { - int r, curr_mode; + int curr_mode; - r = ioctl(fd, KDGKBMODE, &curr_mode); /* * Make sure we only adjust consoles in K_XLATE or K_UNICODE mode. * Otherwise we would (likely) interfere with X11's processing of the @@ -85,7 +92,8 @@ static int verify_vc_kbmode(int fd) { * * http://lists.freedesktop.org/archives/systemd-devel/2013-February/008573.html */ - if (r < 0) + + if (ioctl(fd, KDGKBMODE, &curr_mode) < 0) return -errno; return IN_SET(curr_mode, K_XLATE, K_UNICODE) ? 0 : -EBUSY; @@ -129,7 +137,6 @@ static int toggle_utf8_sysfs(bool utf8) { } static int keyboard_load_and_wait(const char *vc, const char *map, const char *map_toggle, bool utf8) { - _cleanup_free_ char *cmd = NULL; const char *args[8]; unsigned i = 0; pid_t pid; @@ -150,8 +157,12 @@ static int keyboard_load_and_wait(const char *vc, const char *map, const char *m args[i++] = map_toggle; args[i++] = NULL; - log_debug("Executing \"%s\"...", - strnull((cmd = strv_join((char**) args, " ")))); + if (DEBUG_LOGGING) { + _cleanup_free_ char *cmd; + + cmd = strv_join((char**) args, " "); + log_debug("Executing \"%s\"...", strnull(cmd)); + } r = safe_fork("(loadkeys)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid); if (r < 0) @@ -165,7 +176,6 @@ static int keyboard_load_and_wait(const char *vc, const char *map, const char *m } static int font_load_and_wait(const char *vc, const char *font, const char *map, const char *unimap) { - _cleanup_free_ char *cmd = NULL; const char *args[9]; unsigned i = 0; pid_t pid; @@ -190,8 +200,12 @@ static int font_load_and_wait(const char *vc, const char *font, const char *map, args[i++] = font; args[i++] = NULL; - log_debug("Executing \"%s\"...", - strnull((cmd = strv_join((char**) args, " ")))); + if (DEBUG_LOGGING) { + _cleanup_free_ char *cmd; + + cmd = strv_join((char**) args, " "); + log_debug("Executing \"%s\"...", strnull(cmd)); + } r = safe_fork("(setfont)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid); if (r < 0) @@ -325,8 +339,8 @@ static void setup_remaining_vcs(int src_fd, unsigned src_idx, bool utf8) { static int find_source_vc(char **ret_path, unsigned *ret_idx) { _cleanup_free_ char *path = NULL; + int r, err = 0; unsigned i; - int ret_fd, r, err = 0; path = new(char, sizeof("/dev/tty63")); if (!path) @@ -358,20 +372,17 @@ static int find_source_vc(char **ret_path, unsigned *ret_idx) { /* all checks passed, return this one as a source console */ *ret_idx = i; - *ret_path = path; - path = NULL; - ret_fd = fd; - fd = -1; - return ret_fd; + *ret_path = TAKE_PTR(path); + return TAKE_FD(fd); } return log_error_errno(err, "No usable source console found: %m"); } static int verify_source_vc(char **ret_path, const char *src_vc) { - char *path; _cleanup_close_ int fd = -1; - int ret_fd, r; + char *path; + int r; fd = open_terminal(src_vc, O_RDWR|O_CLOEXEC|O_NOCTTY); if (fd < 0) @@ -394,9 +405,7 @@ static int verify_source_vc(char **ret_path, const char *src_vc) { return log_oom(); *ret_path = path; - ret_fd = fd; - fd = -1; - return ret_fd; + return TAKE_FD(fd); } int main(int argc, char **argv) { @@ -419,7 +428,6 @@ int main(int argc, char **argv) { fd = verify_source_vc(&vc, argv[1]); else fd = find_source_vc(&vc, &idx); - if (fd < 0) return EXIT_FAILURE;