]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: introduce TAKE_PTR() macro
authorLennart Poettering <lennart@poettering.net>
Thu, 22 Mar 2018 15:53:26 +0000 (16:53 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 22 Mar 2018 19:21:42 +0000 (20:21 +0100)
This macro will read a pointer of any type, return it, and set the
pointer to NULL. This is useful as an explicit concept of passing
ownership of a memory area between pointers.

This takes inspiration from Rust:

https://doc.rust-lang.org/std/option/enum.Option.html#method.take

and was suggested by Alan Jenkins (@sourcejedi).

It drops ~160 lines of code from our codebase, which makes me like it.
Also, I think it clarifies passing of ownership, and thus helps
readability a bit (at least for the initiated who know the new macro)

93 files changed:
coccinelle/take-ptr.cocci [new file with mode: 0644]
src/basic/alloc-util.h
src/basic/cap-list.c
src/basic/cgroup-util.c
src/basic/extract-word.c
src/basic/fileio.c
src/basic/fs-util.c
src/basic/locale-util.c
src/basic/mount-util.c
src/basic/parse-util.c
src/basic/path-util.c
src/basic/proc-cmdline.c
src/basic/process-util.c
src/basic/securebits-util.c
src/basic/socket-util.c
src/basic/strv.c
src/basic/terminal-util.c
src/basic/unit-name.c
src/busctl/busctl-introspect.c
src/core/cgroup.c
src/core/dbus-execute.c
src/core/dbus-job.c
src/core/dbus.c
src/core/dynamic-user.c
src/core/execute.c
src/core/load-fragment.c
src/core/mount-setup.c
src/core/service.c
src/core/unit.c
src/coredump/coredump.c
src/coredump/coredumpctl.c
src/coredump/stacktrace.c
src/cryptsetup/cryptsetup.c
src/environment-d-generator/environment-d-generator.c
src/firstboot/firstboot.c
src/import/importd.c
src/import/pull-common.c
src/journal/mmap-cache.c
src/libsystemd-network/dhcp-option.c
src/libsystemd-network/dhcp6-option.c
src/libsystemd-network/network-internal.c
src/libsystemd-network/sd-dhcp-lease.c
src/libsystemd/sd-bus/bus-objects.c
src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-daemon/sd-daemon.c
src/libsystemd/sd-device/sd-device.c
src/libsystemd/sd-login/sd-login.c
src/libsystemd/sd-netlink/netlink-message.c
src/libsystemd/sd-network/sd-network.c
src/libudev/libudev-enumerate.c
src/locale/keymap-util.c
src/login/logind-seat.c
src/login/logind-session.c
src/login/logind.c
src/machine/machine-dbus.c
src/mount/mount-tool.c
src/network/netdev/netdev.c
src/network/netdev/wireguard.c
src/network/networkd-link-bus.c
src/network/networkd-network-bus.c
src/network/networkd-network.c
src/network/networkd-radv.c
src/network/networkd-routing-policy-rule.c
src/nspawn/nspawn-mount.c
src/nspawn/nspawn.c
src/resolve/resolved-dns-search-domain.c
src/resolve/resolved-dns-zone.c
src/resolve/resolved-dnssd-bus.c
src/resolve/resolved-dnssd.c
src/resolve/resolved-link-bus.c
src/resolve/resolved-link.c
src/resolve/resolved-manager.c
src/shared/ask-password-api.c
src/shared/bus-util.c
src/shared/cgroup-show.c
src/shared/dns-domain.c
src/shared/efivars.c
src/shared/fdset.c
src/shared/fstab-util.c
src/shared/import-util.c
src/shared/install-printf.c
src/shared/install.c
src/shared/machine-image.c
src/shared/nsflags.c
src/shared/path-lookup.c
src/shared/specifier.c
src/systemctl/systemctl.c
src/sysv-generator/sysv-generator.c
src/tmpfiles/tmpfiles.c
src/udev/net/ethtool-util.c
src/udev/udev-builtin-path_id.c
src/udev/udevd.c
src/vconsole/vconsole-setup.c

diff --git a/coccinelle/take-ptr.cocci b/coccinelle/take-ptr.cocci
new file mode 100644 (file)
index 0000000..0cebe81
--- /dev/null
@@ -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);
index ec7808c1f7c2855b2cf84f416795434c0dae488d..b1e0edbb7ffef2f7190fbbea3f377096f085c1ec 100644 (file)
@@ -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_;                          \
+        })
index c4557666efda1d4c90f3b0d930357b5c5c5e5d24..9416391a53ad87b4eb510e038e700fcca5c947f2 100644 (file)
@@ -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;
 }
index 1a2d7fc0870f380a1c1dda4e7259c1fcd6deed80..5934ee6e8a4f54421666af68e4c2212fd5300581 100644 (file)
@@ -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;
 }
index 5e425604872428af8e89683fc8f42d57fbb10cc7..7a6b56f07190cbe69c3d3c55a00bdc1a9d9e556d 100644 (file)
@@ -194,8 +194,7 @@ finish:
 
 finish_force_next:
         s[sz] = 0;
-        *ret = s;
-        s = NULL;
+        *ret = TAKE_PTR(s);
 
         return 1;
 }
index fb26274afaff028b765947aeb9e60878212f994f..f807842c311b48f72e3476f2527ecd3002845080 100644 (file)
@@ -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;
 }
index c65ba4bfe57f23118084d5f94e74d6fb28e70ea4..aec8b007445226d4475d56b4ce5fc101558b0819 100644 (file)
@@ -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;
 }
@@ -838,10 +836,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';
@@ -863,10 +860,8 @@ 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;
index 266cb29936c432e7e3b657d049fca50244c975e0..de3d7c8c89100ee1ab26f5c9e3186cb3101ed411 100644 (file)
@@ -341,8 +341,7 @@ int get_keymaps(char ***ret) {
 
         strv_sort(l);
 
-        *ret = l;
-        l = NULL;
+        *ret = TAKE_PTR(l);
 
         return 0;
 }
index 8151b3a4e4f7305babe535895d56d31590d4935c..b6e7c9401704eb3f4be94b620af14860b3aea367 100644 (file)
@@ -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;
 }
index fa5b4a353af85f5781992fd408ed323e276a9ed6..8d4912fdbf5c9c4a65ad5476812e49794ca30e53 100644 (file)
@@ -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;
 }
index df946293858ed871c16b8d6bed22860a2143bb78..d4c4a02cb3e1005f77d8ab474131f1f37239854f 100644 (file)
@@ -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;
index c5d1fb1d4162e46c0270753a2c9a04470e4bf4e0..c51e3c0a3b52b25ff0fe0ad8444fb73588240669 100644 (file)
@@ -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;
 }
index 853e0e3449cdb1c095732d3ad28475e1881d146b..2583b310a4b8f60da0d8119c09bff5ff9be9e6ed 100644 (file)
@@ -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;
 }
index 441d386f9e1f359c880d3f41e3510c4159208538..75675bf4c0bc11dae2c1ecbf307cfbee73ead51d 100644 (file)
@@ -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;
 }
index b91b093132e787d722e45f4fdb1cff05c85901e7..fd26ae713798a31965de37bb4c2dab97c971c4c3 100644 (file)
@@ -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;
 }
index e80ff4a62c400c5aa190fac2e0de30b81be99da2..80100164519f0cbe8792a778657b01a6671a4abe 100644 (file)
@@ -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;
 }
index eacfd14677a4e50cd47f245baebfd9236c4f26fb..87691d1d382e1bdbdef31856a502ab3f91e21745 100644 (file)
@@ -707,10 +707,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 +777,7 @@ int get_kernel_consoles(char ***ret) {
                 goto fallback;
         }
 
-        *ret = l;
-        l = NULL;
+        *ret = TAKE_PTR(l);
 
         return 0;
 
@@ -788,8 +786,7 @@ fallback:
         if (r < 0)
                 return r;
 
-        *ret = l;
-        l = NULL;
+        *ret = TAKE_PTR(l);
 
         return 0;
 }
index 3937dfc5eefde4e6443bfd13b8961fd72dbd3a9a..7d9367334ba20d3ff480d2e12915dddcfce886c8 100644 (file)
@@ -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;
 }
index 54d6e07d2c38f5319478031585595f3c14252b3b..cce68a480b757691246d51bca023bcb6a67ae575 100644 (file)
@@ -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);
index 65ed86580f6e08f8e7453ade61570e4211a50d6b..62a3d86ef16a975e9cef7445f103c999bc92f89b 100644 (file)
@@ -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;
 }
index 635213a866a84881d16998fa5e1cb10ba990157a..7344623ebf694cea59589559d57b42fe9e6127f1 100644 (file)
@@ -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;
 
index 0802fc977300d516214c614e2c21129def62382b..15d0e5dd8bc38488888cda344af985ccf208e0ce 100644 (file)
@@ -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;
index 56b43adcdaadda2f6fe17a78a402828fe7d305d8..eb9ec44f5b3711c74965db0d0852a585aa5c0d73 100644 (file)
@@ -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;
 }
index de6aadd597d028a3a7889e7242f29553a3faf85d..f87a5a20d47a2117e9f26234c5e5c990a5607143 100644 (file)
@@ -770,8 +770,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;
 }
index 7292b815db3bd28d3b5400f5f98f8cb0544014e0..bfd3dfdafc8343d216ddb84d78e854efff9b25df 100644 (file)
@@ -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;
 }
index 94605ab0d02c3877b844d330b2a84dfbfa3913f2..887eb1cf49beb0dda391b63861448c4b9ceab8dd 100644 (file)
@@ -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;
 
index 9c27972aff5c95215bc43f7e16f6ec05c829449a..b7d654619cc2d002719e657cfd31ff1984b39c23 100644 (file)
@@ -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)
index 23a5bcd1c4213f45226499c56d9f1b30bc8ee11e..588f08fef330b0b61d129307ad9fef74a1575418 100644 (file)
@@ -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))
index 52851b6ffc4adbdc5cf8583b6b1f9dc971c79757..90ec73231b92349bc92e9078dfb6cd05ac308da1 100644 (file)
@@ -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) {
index e924750d1b457498a94181bc6589dc433aa17486..6fbfe58bd64ed4b6102b4d552a9e72c15447b60e 100644 (file)
@@ -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;
 }
index 62bcbb6be3acc35366f858e6e8201c253ab06efd..a1b8b85e1050ba52518ba00ecbf345396030bb5b 100644 (file)
@@ -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;
                 }
index 95fd27b79a21ae04c0bb79f16eac0bbd315b4500..ec39fb8499d1686157ab31f8495146c682bcd6dc 100644 (file)
@@ -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;
 
index 7255ff418c8f002768913f4440be43ded7325a12..69bbf525fe06554286ec838bcfc21888ec52035a 100644 (file)
@@ -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;
 }
index bb4d76da900f0b5d401717500a5cd1bdad8981f1..9c16a60e2e064c08025d20a5e6d8d25a53713060 100644 (file)
@@ -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;
 }
 
index 308ef9d750759471d7e8392ab9d676fa3dfaacdd..a1f7e226f8e601e64a64b66ddc61fc60666f18a7 100644 (file)
@@ -575,8 +575,7 @@ static int prompt_root_password(void) {
                         continue;
                 }
 
-                arg_root_password = a;
-                a = NULL;
+                arg_root_password = TAKE_PTR(a);
                 break;
         }
 
index 10f52c7fc11e55ce9d830e43a2e38a85e3ee2b6e..a0c02b26abd2b4627f2513dd8926aef239ae4887 100644 (file)
@@ -1083,8 +1083,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;
 }
index 7651870bf09dea1b3a4aefb441fcc77404033cf6..92f2692c817e4868bc2ae0f61da4d6c00ada86c0 100644 (file)
@@ -124,8 +124,7 @@ int pull_find_old_etags(
                         return r;
         }
 
-        *etags = l;
-        l = NULL;
+        *etags = TAKE_PTR(l);
 
         return 0;
 }
index 630ae6fbac989a137e6ac01b9a0675ed2eae9bf2..91ed3cd519a6c773a65fe5e10a92cf6f414c96a4 100644 (file)
@@ -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) {
index 0489579e7f0cbf9e76a034172db655a923dfce6a..142e39bc665fd0504a0dffed3c30bfed57f3d3b8 100644 (file)
@@ -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;
 }
index df96ad739dfff85b647256a8c226823875d72e8e..2b3c8ceb59db2e339690ce16b9292d5661f52434 100644 (file)
@@ -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;
 
index 584a1f36ac7f2e92361efb44314c77b0fab340a5..00a56c820618f6ad5bfe391bfb9a383c7a467205 100644 (file)
@@ -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;
 }
index 2e88e39878b0ba303250af8b1866d53ad613a993..9db0a9389817d603897452d080d6568f2b8169f9 100644 (file)
@@ -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;
 }
index 6e00255b20ba169390f8687cb3f783e547eee26e..08015a99efc84be1fa3f485e9acf13e91dac73fe 100644 (file)
@@ -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) {
index 2f5e483ae2bcbb4c870987e9bc8cd0987497a0c1..8c022fce1bec2dfc9998611f9fe8c350cc50e1c7 100644 (file)
@@ -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;
 }
index 1334498ca41eede059513874375f9209214c62bd..0da0ca5890a249b858f3a752927bc1f9654b7bb4 100644 (file)
@@ -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;
 }
index 1297dfa91116963b0c1ebbcf95b904a95d0c9460..fd2821b1a76a2d59a8250a471418a607d8e53714 100644 (file)
@@ -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;
index 6601bcd6be7057965a433aaaff46e5199802b836..2a4eede9f8ce13670343aa32bb6eb5ed21dc1a79 100644 (file)
@@ -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;
 }
index af3d13edcddd6465f5479f0c967de38653ac15eb..3a5f9346aa6e06f321326a6013f98e1eb5cedcce 100644 (file)
@@ -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;
index 8f4814019e208248c35b91b2d4eed24c4f4d9ae1..1d3e9b30f5e05660d92911216f9f8e2af1dbebbe 100644 (file)
@@ -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;
 }
index 271880075e8b3c82e8a5e7fbc6cd539518c863ba..a366ab16befcb84f0fd72952598c0e5ffae88d2e 100644 (file)
@@ -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);
 }
 
 /**
index 2d788106bb5c2c66a3073884802bd1beed55a7b9..393e9b564894e2a47fccc20e3afdc87b3cc55466 100644 (file)
@@ -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;
                 }
         }
index 46e7c06ddca65b0d711ea006dbe1e21dd2cc4391..3e89b0f124570e97d59804f75ac95238c4917e5a 100644 (file)
@@ -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);
 }
index 1859150b5e8dae7230236266f6e7d905e4b66826..7aeeafd8e149d2202ef61de07de8b7b2e510f6dd 100644 (file)
@@ -1271,8 +1271,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;
index 95b29ae85eb5a4157a5a124d963e78039b98c657..6d3b7cee2321df1338d747e288069ce09390ccb5 100644 (file)
@@ -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;
 }
index 9c435d6715a74c9fcb618417344a6a3710db6375..fb053343c173d5dbe307a231b6e31a2e97595283 100644 (file)
@@ -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;
 }
index dd139740c2d7e8fca36800a51efeb3cc2cdaf3ee..046def33ae8082ffed71014342e5ca7b10f04938 100644 (file)
@@ -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;
 }
index 93648e1be0bd3f49d4b41cbe094a4839d2527e21..421a32d567db3a03c35143abf30ae97b415e2135 100644 (file)
@@ -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;
 }
index f1f4bab47503f700702e01a3b131e6b357a9c753..9025693f7020e4bd38f4c27912542da29092068c 100644 (file)
@@ -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);
 
index 2f4850b1a19ee210b3141b7e8e89e1d8bcf7e37c..cc966fa36343a14bbf431385016c04cbe46766aa 100644 (file)
@@ -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;
 }
index 8d3d7236fc1eba4383528d46a7d6829fe79984f1..459868a57f4e5ff149426fe35e6b01eacf056393 100644 (file)
@@ -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;
 }
index fc9fbed6f18af21f1d0de688f06540ddda0a4f36..e3ed24e600b7063ecda42bab47e55dc0e9e6fb6e 100644 (file)
@@ -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;
 }
index a921d120b338e9c968162dedc3d142099115bb49..1cc89f3b0ad541a0a61acbec793d963c27635388 100644 (file)
@@ -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;
         }
index fdbaec58ebc7002dc0779a62cc5106a4740fe0e8..d8f712c0699c8ab3a0638e54e48d4c47dcbff0ce 100644 (file)
@@ -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;
 }
index 0ee69114b25518b764afb03d5d8de27dda092e06..e32c6223d7f36966c2a6245d5404fd26d58511d3 100644 (file)
@@ -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))
index 49325f0d22537df90b3f659880c1885b04aee1aa..384b1ea5df174144fb6522c2d8cc02bac9190970 100644 (file)
@@ -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)) {
index 585c518e19954b62785d0b67de4271d6a92c5030..03f7536fd2db58cc7fb121e154b437654ad0a6c0 100644 (file)
@@ -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) {
 
index dcb9702e5771e6cfc0bdc049f67258a900da6355..4888eccbd366404b05eb9ef07f9e032bb11eb777 100644 (file)
@@ -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);
index c914e8f8d2a528bf35fce02565f1e64649fa2769..1a70968dd1f9e41e6ca1d1e74438cc2c941a4b90 100644 (file)
@@ -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;
 }
index db589f4436040d5d40291e7006a662a184ecc802..18e61aaee32229e8f71aab648eac6967cfec436f 100644 (file)
@@ -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;
 }
index 82a29289d2cba37aa6985db1a251b90da63d348f..7e7308a8190da3218809d3d17924532be638b116 100644 (file)
@@ -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;
 }
index e3e50eca53f044d9da530df16d4a7fc13350fb31..4d87e88cdf937b0d01c5ca0f17832f749c7fd531 100644 (file)
@@ -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;
index 2ee027791ade02193509db8d547a7ee8645ba040..de1f0ce21d63e127daa9bc755e825af6cd80620e 100644 (file)
@@ -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;
 
index 4fa9188957c44be02d7a4e9718e5b4b82e703c7c..88cafd494059a88684cd7f3640393ec0fd0c28ba 100644 (file)
@@ -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:
index 8061af1865756b0a0d3c3a3e97d5b58c0986e136..803f75335c999d38f563da8e47a3fbc893f11062 100644 (file)
@@ -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;
         }
 
index 0ddae954343f7fc2895d1bc8c6abc54b26570551..326a3980752eba9c8a3f543bcf8e70201d3f14f7 100644 (file)
@@ -189,8 +189,7 @@ int show_cgroup_by_path(
                         free(last);
                 }
 
-                last = k;
-                k = NULL;
+                last = TAKE_PTR(k);
         }
 
         if (r < 0)
index 8c807e0e2381d26f5dd151e0c3da96c6bf775ab3..89d18431e42e28d0af1e80c9d4c5f9ba4e0892fe 100644 (file)
@@ -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);
                 }
         }
 }
index 9ca51cf7507092070ad6065562879eadfd7e8ff9..d31cf2d86016333deb58ce03ddf75488e3a337c5 100644 (file)
@@ -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));
 }
 
index 9ce1295223964894a82efb6cad36d2d816042955..845c19b1cf7826c44a0afa1fabf72d74f884b2c1 100644 (file)
@@ -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! */
index bcd7b43084140ee34cdccbc8cbbee1af14c0225c..1a3bc46d2a2d253a2f11205ec0d55b55e86a231a 100644 (file)
@@ -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;
 }
index 07ba216e938f0d43dc6fec13e95b466a39be2a66..5f71add50643ce846c97c9a988330b770b61878c 100644 (file)
@@ -153,8 +153,7 @@ int raw_strip_suffixes(const char *p, char **ret) {
                         break;
         }
 
-        *ret = q;
-        q = NULL;
+        *ret = TAKE_PTR(q);
 
         return 0;
 }
index aaab2e6665137a2eeb2179b892fdc8621bac2417..5135d0671e50ac5b7ee19cd4256270fd7e521afa 100644 (file)
@@ -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;
 }
index ed5f51cc463eaedcf695c4ed777684e7f54f455a..7506018d4169c333435ff45cd6f21b84d5a3e42e 100644 (file)
@@ -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);
index 66eefb3036628ecb02f7168f597e50ccfbe19e89..f48b6c693ca3b2051ab2741ff2e2c52cf6dfdd63 100644 (file)
@@ -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) {
index 05ec9feb8da6976df6e9bebd87266c487be23325..a8ddc9a57f4fe6db3c2b09c2067e35bc6fdc7b6d 100644 (file)
@@ -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;
 }
index 5f31831c99478218242b85a63fd4e5305cb8ac17..a472e80aec53189d8c22a7dcf30c90d04ace77ca 100644 (file)
@@ -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;
 }
index 23aaa88c4bdbfad960dd87d0a999d875e44619b7..98d95eef8efc875c92ee02f6fd03528561ddbf13 100644 (file)
@@ -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;
 }
 
index 064d59313195247094cf4af1dd28e1865daeb3a7..d23bddc99a460e7ca60c6185d89f08c3d889d67a 100644 (file)
@@ -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;
 }
index 92669c6758c8288f6d701f13231d15118c00f319..394a04bb885ff5103e3d86a587033f264db80d1c 100644 (file)
@@ -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;
 }
index f1890f326131da4fe3589d927b12f747a4a3c373..1794290ebe9e0497fec136d2f6c121b9326aca4d 100644 (file)
@@ -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;
 }
 
index 1fdfc8540848fabeeefeabf5833942b857b9e612..69ac3e8c58345dbaace928b66ffed771d1d2dbec 100644 (file)
@@ -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;
 }
index 0a32363debc8fbb3fdfbdd9123bb58f8de951277..7d967e04dee7ed9a4ff044b19c38e6b0aec8f313 100644 (file)
@@ -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);
 }
 
 /*
index 615c4ed3e2f33dfd020e53d0f3727541c93a09a2..34976b30e08d6f68696a04fdb7e8a7a23684fdaa 100644 (file)
@@ -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;
 }
index a9cc2bf63c1700ee309baceb21107694fcac6a5d..6bde3da1c8d9c00b6ef6aa46faeceb841e7902e3 100644 (file)
@@ -358,8 +358,7 @@ 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_path = TAKE_PTR(path);
                 ret_fd = fd;
                 fd = -1;
                 return ret_fd;