macro: introduce TAKE_PTR() macro

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)
This commit is contained in:
Lennart Poettering 2018-03-22 16:53:26 +01:00
parent 1147eef0b6
commit ae2a15bc14
93 changed files with 215 additions and 379 deletions

14
coccinelle/take-ptr.cocci Normal file
View File

@ -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);

View File

@ -130,3 +130,12 @@ void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
_new_ = alloca_align(_size_, (align)); \ _new_ = alloca_align(_size_, (align)); \
(void*)memset(_new_, 0, _size_); \ (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_; \
})

View 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 */ str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
*s = str; *s = TAKE_PTR(str);
str = NULL;
return 0; return 0;
} }

View File

@ -1424,10 +1424,9 @@ int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
if (r < 0) if (r < 0)
return r; return r;
if (c == raw) { if (c == raw)
*cgroup = raw; *cgroup = TAKE_PTR(raw);
raw = NULL; else {
} else {
char *n; char *n;
n = strdup(c); n = strdup(c);
@ -2010,8 +2009,7 @@ int cg_slice_to_path(const char *unit, char **ret) {
if (!strextend(&s, e, NULL)) if (!strextend(&s, e, NULL))
return -ENOMEM; return -ENOMEM;
*ret = s; *ret = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }
@ -2301,8 +2299,7 @@ int cg_mask_to_string(CGroupMask mask, char **ret) {
assert(s); assert(s);
s[n] = 0; s[n] = 0;
*ret = s; *ret = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }

View File

@ -194,8 +194,7 @@ finish:
finish_force_next: finish_force_next:
s[sz] = 0; s[sz] = 0;
*ret = s; *ret = TAKE_PTR(s);
s = NULL;
return 1; return 1;
} }

View File

@ -330,8 +330,7 @@ int read_full_stream(FILE *f, char **contents, size_t *size) {
} }
buf[l] = 0; buf[l] = 0;
*contents = buf; *contents = TAKE_PTR(buf);
buf = NULL; /* do not free */
if (size) if (size)
*size = l; *size = l;
@ -1432,8 +1431,7 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
if (fd < 0) if (fd < 0)
return -errno; return -errno;
*ret_path = tmp; *ret_path = TAKE_PTR(tmp);
tmp = NULL;
return fd; return fd;
} }
@ -1519,8 +1517,7 @@ int read_nul_string(FILE *f, char **ret) {
return -ENOMEM; return -ENOMEM;
} }
*ret = x; *ret = TAKE_PTR(x);
x = NULL;
return 0; return 0;
} }

View File

@ -458,10 +458,8 @@ int get_files_in_directory(const char *path, char ***list) {
n++; n++;
} }
if (list) { if (list)
*list = l; *list = TAKE_PTR(l);
l = NULL; /* avoid freeing */
}
return n; 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 this is not a symlink, then let's just add the name we read to what we already verified. */
if (!done) { if (!done)
done = first; done = TAKE_PTR(first);
first = NULL; else {
} else {
/* If done is "/", as first also contains slash at the head, then remove this redundant slash. */ /* If done is "/", as first also contains slash at the head, then remove this redundant slash. */
if (streq(done, "/")) if (streq(done, "/"))
*done = '\0'; *done = '\0';
@ -863,10 +860,8 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
return -ENOMEM; return -ENOMEM;
} }
if (ret) { if (ret)
*ret = done; *ret = TAKE_PTR(done);
done = NULL;
}
if (flags & CHASE_OPEN) { if (flags & CHASE_OPEN) {
int q; int q;

View File

@ -341,8 +341,7 @@ int get_keymaps(char ***ret) {
strv_sort(l); strv_sort(l);
*ret = l; *ret = TAKE_PTR(l);
l = NULL;
return 0; return 0;
} }

View File

@ -81,10 +81,8 @@ int name_to_handle_at_loop(
if (name_to_handle_at(fd, path, h, &mnt_id, flags) >= 0) { if (name_to_handle_at(fd, path, h, &mnt_id, flags) >= 0) {
if (ret_handle) { if (ret_handle)
*ret_handle = h; *ret_handle = TAKE_PTR(h);
h = NULL;
}
if (ret_mnt_id) if (ret_mnt_id)
*ret_mnt_id = mnt_id; *ret_mnt_id = mnt_id;
@ -951,8 +949,7 @@ int mount_option_mangle(
} }
*ret_mount_flags = mount_flags; *ret_mount_flags = mount_flags;
*ret_remaining_options = ret; *ret_remaining_options = TAKE_PTR(ret);
ret = NULL;
return 0; return 0;
} }

View File

@ -324,8 +324,7 @@ int parse_syscall_and_errno(const char *in, char **name, int *error) {
return -EINVAL; return -EINVAL;
*error = e; *error = e;
*name = n; *name = TAKE_PTR(n);
n = NULL;
return 0; return 0;
} }

View File

@ -290,8 +290,7 @@ char **path_strv_resolve(char **l, const char *root) {
r = chase_symlinks(t, root, 0, &u); r = chase_symlinks(t, root, 0, &u);
if (r == -ENOENT) { if (r == -ENOENT) {
if (root) { if (root) {
u = orig; u = TAKE_PTR(orig);
orig = NULL;
free(t); free(t);
} else } else
u = t; u = t;

View File

@ -204,10 +204,8 @@ int proc_cmdline_get_key(const char *key, unsigned flags, char **value) {
} }
} }
if (value) { if (value)
*value = ret; *value = TAKE_PTR(ret);
ret = NULL;
}
return found; return found;
} }

View File

@ -623,8 +623,7 @@ int get_process_environ(pid_t pid, char **env) {
} else } else
outcome[sz] = '\0'; outcome[sz] = '\0';
*env = outcome; *env = TAKE_PTR(outcome);
outcome = NULL;
return 0; return 0;
} }

View File

@ -48,8 +48,7 @@ int secure_bits_to_string_alloc(int i, char **s) {
if (len != 0) if (len != 0)
str[len - 1] = '\0'; str[len - 1] = '\0';
*s = str; *s = TAKE_PTR(str);
str = NULL;
return 0; return 0;
} }

View File

@ -985,8 +985,7 @@ int getpeersec(int fd, char **ret) {
if (isempty(s)) if (isempty(s))
return -EOPNOTSUPP; return -EOPNOTSUPP;
*ret = s; *ret = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }

View 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)) if (!GREEDY_REALLOC(l, allocated, n + 2))
return -ENOMEM; return -ENOMEM;
l[n++] = word; l[n++] = TAKE_PTR(word);
word = NULL;
l[n] = NULL; l[n] = NULL;
} }
@ -353,8 +352,7 @@ int strv_split_extract(char ***t, const char *s, const char *separators, Extract
return -ENOMEM; return -ENOMEM;
} }
*t = l; *t = TAKE_PTR(l);
l = NULL;
return (int) n; return (int) n;
} }

View File

@ -707,10 +707,9 @@ int vtnr_from_tty(const char *tty) {
tty = active; tty = active;
} }
if (tty == active) { if (tty == active)
*ret = active; *ret = TAKE_PTR(active);
active = NULL; else {
} else {
char *tmp; char *tmp;
tmp = strdup(tty); tmp = strdup(tty);
@ -778,8 +777,7 @@ int get_kernel_consoles(char ***ret) {
goto fallback; goto fallback;
} }
*ret = l; *ret = TAKE_PTR(l);
l = NULL;
return 0; return 0;
@ -788,8 +786,7 @@ fallback:
if (r < 0) if (r < 0)
return r; return r;
*ret = l; *ret = TAKE_PTR(l);
l = NULL;
return 0; return 0;
} }

View File

@ -363,8 +363,7 @@ int unit_name_unescape(const char *f, char **ret) {
*t = 0; *t = 0;
*ret = r; *ret = TAKE_PTR(r);
r = NULL;
return 0; return 0;
} }

View File

@ -275,10 +275,9 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth
free(node_path); free(node_path);
if (name[0] == '/') { if (name[0] == '/')
node_path = name; node_path = TAKE_PTR(name);
name = NULL; else {
} else {
if (endswith(prefix, "/")) if (endswith(prefix, "/"))
node_path = strappend(prefix, name); node_path = strappend(prefix, name);

View File

@ -1381,8 +1381,7 @@ int unit_set_cgroup_path(Unit *u, const char *path) {
unit_release_cgroup(u); unit_release_cgroup(u);
u->cgroup_path = p; u->cgroup_path = TAKE_PTR(p);
p = NULL;
return 1; return 1;
} }

View File

@ -424,10 +424,8 @@ static int property_get_syscall_filter(
if (r < 0) if (r < 0)
return -ENOMEM; return -ENOMEM;
} }
} else { } else
s = name; s = TAKE_PTR(name);
name = NULL;
}
r = strv_consume(&l, s); r = strv_consume(&l, s);
if (r < 0) if (r < 0)
@ -1125,8 +1123,7 @@ int bus_set_transient_exec_command(
return -ENOMEM; return -ENOMEM;
} }
c->argv = argv; c->argv = TAKE_PTR(argv);
argv = NULL;
c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0; c->flags = b ? EXEC_COMMAND_IGNORE_FAILURE : 0;

View File

@ -274,8 +274,7 @@ int bus_job_coldplug_bus_track(Job *j) {
assert(j); assert(j);
deserialized_clients = j->deserialized_clients; deserialized_clients = TAKE_PTR(j->deserialized_clients);
j->deserialized_clients = NULL;
if (strv_isempty(deserialized_clients)) if (strv_isempty(deserialized_clients))
return 0; return 0;

View 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); assert(hashmap_size(m->jobs) == k);
*nodes = l; *nodes = TAKE_PTR(l);
l = NULL;
return k; return k;
} }
@ -526,8 +525,7 @@ static int bus_unit_enumerate(sd_bus *bus, const char *path, void *userdata, cha
k++; k++;
} }
*nodes = l; *nodes = TAKE_PTR(l);
l = NULL;
return k; return k;
} }

View 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 */ if (check_uid != uid) /* lock file doesn't match our own idea */
return -ESRCH; return -ESRCH;
*ret = user; *ret = TAKE_PTR(user);
user = NULL;
return 0; return 0;
} }

View File

@ -1733,8 +1733,7 @@ static int build_environment(
our_env[n_env++] = NULL; our_env[n_env++] = NULL;
assert(n_env <= 12); assert(n_env <= 12);
*ret = our_env; *ret = TAKE_PTR(our_env);
our_env = NULL;
return 0; return 0;
} }
@ -1763,8 +1762,7 @@ static int build_pass_environment(const ExecContext *c, char ***ret) {
x = NULL; x = NULL;
} }
*ret = pass_env; *ret = TAKE_PTR(pass_env);
pass_env = NULL;
return 0; return 0;
} }
@ -2290,9 +2288,7 @@ static int compile_bind_mounts(
*ret_bind_mounts = bind_mounts; *ret_bind_mounts = bind_mounts;
*ret_n_bind_mounts = n; *ret_n_bind_mounts = n;
*ret_empty_directories = empty_directories; *ret_empty_directories = TAKE_PTR(empty_directories);
empty_directories = NULL;
return (int) n; return (int) n;
@ -2696,8 +2692,7 @@ static int compile_suggested_paths(const ExecContext *c, const ExecParameters *p
} }
} }
*ret = list; *ret = TAKE_PTR(list);
list = NULL;
return 0; return 0;
} }

View File

@ -1248,8 +1248,7 @@ int config_parse_exec_cpu_affinity(const char *unit,
} }
if (!c->cpuset) { if (!c->cpuset) {
c->cpuset = cpuset; c->cpuset = TAKE_PTR(cpuset);
cpuset = NULL;
c->cpuset_ncpus = (unsigned) ncpus; c->cpuset_ncpus = (unsigned) ncpus;
return 0; return 0;
} }
@ -1257,8 +1256,7 @@ int config_parse_exec_cpu_affinity(const char *unit,
if (c->cpuset_ncpus < (unsigned) ncpus) { if (c->cpuset_ncpus < (unsigned) ncpus) {
CPU_OR_S(CPU_ALLOC_SIZE(c->cpuset_ncpus), cpuset, c->cpuset, cpuset); CPU_OR_S(CPU_ALLOC_SIZE(c->cpuset_ncpus), cpuset, c->cpuset, cpuset);
CPU_FREE(c->cpuset); CPU_FREE(c->cpuset);
c->cpuset = cpuset; c->cpuset = TAKE_PTR(cpuset);
cpuset = NULL;
c->cpuset_ncpus = (unsigned) ncpus; c->cpuset_ncpus = (unsigned) ncpus;
return 0; return 0;
} }
@ -2089,8 +2087,7 @@ int config_parse_user_group(
return -ENOEXEC; return -ENOEXEC;
} }
n = k; n = TAKE_PTR(k);
k = NULL;
} }
free(*user); free(*user);
@ -2320,10 +2317,8 @@ int config_parse_environ(
"Failed to resolve specifiers, ignoring: %s", word); "Failed to resolve specifiers, ignoring: %s", word);
continue; continue;
} }
} else { } else
k = word; k = TAKE_PTR(word);
word = NULL;
}
if (!env_assignment_is_valid(k)) { if (!env_assignment_is_valid(k)) {
log_syntax(unit, LOG_ERR, filename, line, 0, log_syntax(unit, LOG_ERR, filename, line, 0,
@ -2390,10 +2385,8 @@ int config_parse_pass_environ(
"Failed to resolve specifiers, ignoring: %s", word); "Failed to resolve specifiers, ignoring: %s", word);
continue; continue;
} }
} else { } else
k = word; k = TAKE_PTR(word);
word = NULL;
}
if (!env_name_is_valid(k)) { if (!env_name_is_valid(k)) {
log_syntax(unit, LOG_ERR, filename, line, 0, log_syntax(unit, LOG_ERR, filename, line, 0,
@ -2469,10 +2462,8 @@ int config_parse_unset_environ(
"Failed to resolve specifiers, ignoring: %s", word); "Failed to resolve specifiers, ignoring: %s", word);
continue; continue;
} }
} else { } else
k = word; k = TAKE_PTR(word);
word = NULL;
}
if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) { if (!env_assignment_is_valid(k) && !env_name_is_valid(k)) {
log_syntax(unit, LOG_ERR, filename, line, 0, log_syntax(unit, LOG_ERR, filename, line, 0,
@ -3515,8 +3506,7 @@ int config_parse_device_allow(
if (!a) if (!a)
return log_oom(); return log_oom();
a->path = path; a->path = TAKE_PTR(path);
path = NULL;
a->r = !!strchr(m, 'r'); a->r = !!strchr(m, 'r');
a->w = !!strchr(m, 'w'); a->w = !!strchr(m, 'w');
a->m = !!strchr(m, 'm'); a->m = !!strchr(m, 'm');
@ -3615,8 +3605,7 @@ int config_parse_io_device_weight(
if (!w) if (!w)
return log_oom(); return log_oom();
w->path = path; w->path = TAKE_PTR(path);
path = NULL;
w->weight = u; w->weight = u;
@ -3701,8 +3690,7 @@ int config_parse_io_limit(
if (!l) if (!l)
return log_oom(); return log_oom();
l->path = path; l->path = TAKE_PTR(path);
path = NULL;
for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++) for (ttype = 0; ttype < _CGROUP_IO_LIMIT_TYPE_MAX; ttype++)
l->limits[ttype] = cgroup_io_limit_defaults[ttype]; l->limits[ttype] = cgroup_io_limit_defaults[ttype];
@ -3804,8 +3792,7 @@ int config_parse_blockio_device_weight(
if (!w) if (!w)
return log_oom(); return log_oom();
w->path = path; w->path = TAKE_PTR(path);
path = NULL;
w->weight = u; w->weight = u;
@ -3885,8 +3872,7 @@ int config_parse_blockio_bandwidth(
if (!b) if (!b)
return log_oom(); return log_oom();
b->path = path; b->path = TAKE_PTR(path);
path = NULL;
b->rbps = CGROUP_LIMIT_MAX; b->rbps = CGROUP_LIMIT_MAX;
b->wbps = CGROUP_LIMIT_MAX; b->wbps = CGROUP_LIMIT_MAX;

View File

@ -315,10 +315,8 @@ int mount_cgroup_controllers(char ***join_controllers) {
options = strv_join(*k, ","); options = strv_join(*k, ",");
if (!options) if (!options)
return log_oom(); return log_oom();
} else { } else
options = controller; options = TAKE_PTR(controller);
controller = NULL;
}
where = strappend("/sys/fs/cgroup/", options); where = strappend("/sys/fs/cgroup/", options);
if (!where) if (!where)

View File

@ -2554,8 +2554,7 @@ static int service_deserialize_exec_command(Unit *u, const char *key, const char
state = STATE_EXEC_COMMAND_PATH; state = STATE_EXEC_COMMAND_PATH;
break; break;
case STATE_EXEC_COMMAND_PATH: case STATE_EXEC_COMMAND_PATH:
path = arg; path = TAKE_PTR(arg);
arg = NULL;
state = STATE_EXEC_COMMAND_ARGS; state = STATE_EXEC_COMMAND_ARGS;
if (!path_is_absolute(path)) if (!path_is_absolute(path))

View File

@ -709,10 +709,8 @@ static int set_complete_move(Set **s, Set **other) {
if (*s) if (*s)
return set_move(*s, *other); return set_move(*s, *other);
else { else
*s = *other; *s = TAKE_PTR(*other);
*other = NULL;
}
return 0; return 0;
} }
@ -726,10 +724,8 @@ static int hashmap_complete_move(Hashmap **s, Hashmap **other) {
if (*s) if (*s)
return hashmap_move(*s, *other); return hashmap_move(*s, *other);
else { else
*s = *other; *s = TAKE_PTR(*other);
*other = NULL;
}
return 0; return 0;
} }
@ -4017,8 +4013,7 @@ static int user_from_unit_name(Unit *u, char **ret) {
return r; return r;
if (valid_user_group_name(n)) { if (valid_user_group_name(n)) {
*ret = n; *ret = TAKE_PTR(n);
n = NULL;
return 0; 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) { char* unit_concat_strv(char **l, UnitWriteFlags flags) {
_cleanup_free_ char *result = NULL; _cleanup_free_ char *result = NULL;
size_t n = 0, allocated = 0; 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 /* 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 */ * way suitable for ExecStart= stanzas */
@ -4255,10 +4250,7 @@ char* unit_concat_strv(char **l, UnitWriteFlags flags) {
result[n] = 0; result[n] = 0;
ret = result; return TAKE_PTR(result);
result = NULL;
return ret;
} }
int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data) { int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data) {

View File

@ -584,8 +584,7 @@ static int compose_open_fds(pid_t pid, char **open_fds) {
if (errno > 0) if (errno > 0)
return -errno; return -errno;
*open_fds = buffer; *open_fds = TAKE_PTR(buffer);
buffer = NULL;
return 0; return 0;
} }

View 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); return log_error_errno(errno, "File \"%s\" is not readable: %m", filename);
if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) { if (path && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
*path = filename; *path = TAKE_PTR(filename);
filename = NULL;
return 0; return 0;
} }

View File

@ -184,8 +184,7 @@ int coredump_make_stack_trace(int fd, const char *executable, char **ret) {
c.f = safe_fclose(c.f); c.f = safe_fclose(c.f);
*ret = buf; *ret = TAKE_PTR(buf);
buf = NULL;
r = 0; r = 0;

View File

@ -402,8 +402,7 @@ static int get_password(const char *vol, const char *src, usec_t until, bool acc
*p = c; *p = c;
} }
*ret = passwords; *ret = TAKE_PTR(passwords);
passwords = NULL;
return 0; return 0;
} }

View File

@ -45,8 +45,7 @@ static int environment_dirs(char ***ret) {
if (r < 0) if (r < 0)
return r; return r;
*ret = dirs; *ret = TAKE_PTR(dirs);
dirs = NULL;
return 0; return 0;
} }

View File

@ -575,8 +575,7 @@ static int prompt_root_password(void) {
continue; continue;
} }
arg_root_password = a; arg_root_password = TAKE_PTR(a);
a = NULL;
break; break;
} }

View File

@ -1083,8 +1083,7 @@ static int transfer_node_enumerator(sd_bus *bus, const char *path, void *userdat
k++; k++;
} }
*nodes = l; *nodes = TAKE_PTR(l);
l = NULL;
return 1; return 1;
} }

View File

@ -124,8 +124,7 @@ int pull_find_old_etags(
return r; return r;
} }
*etags = l; *etags = TAKE_PTR(l);
l = NULL;
return 0; return 0;
} }

View File

@ -220,8 +220,7 @@ static void context_detach_window(Context *c) {
if (!c->window) if (!c->window)
return; return;
w = c->window; w = TAKE_PTR(c->window);
c->window = NULL;
LIST_REMOVE(by_window, w->contexts, c); LIST_REMOVE(by_window, w->contexts, c);
if (!w->contexts && !w->keep_always) { if (!w->contexts && !w->keep_always) {

View File

@ -253,10 +253,8 @@ int dhcp_option_parse(DHCPMessage *message, size_t len, dhcp_option_callback_t c
if (message_type == 0) if (message_type == 0)
return -ENOMSG; return -ENOMSG;
if (_error_message && IN_SET(message_type, DHCP_NAK, DHCP_DECLINE)) { if (_error_message && IN_SET(message_type, DHCP_NAK, DHCP_DECLINE))
*_error_message = error_message; *_error_message = TAKE_PTR(error_message);
error_message = NULL;
}
return message_type; return message_type;
} }

View File

@ -617,8 +617,7 @@ int dhcp6_option_parse_domainname(const uint8_t *optval, uint16_t optlen, char *
idx++; idx++;
} }
*str_arr = names; *str_arr = TAKE_PTR(names);
names = NULL;
return idx; return idx;

View File

@ -276,10 +276,9 @@ int config_parse_ifalias(const char *unit,
} }
free(*s); free(*s);
if (*n) { if (*n)
*s = n; *s = TAKE_PTR(n);
n = NULL; else
} else
*s = NULL; *s = NULL;
return 0; return 0;
@ -437,8 +436,7 @@ int deserialize_in_addrs(struct in_addr **ret, const char *string) {
size++; size++;
} }
*ret = addresses; *ret = TAKE_PTR(addresses);
addresses = NULL;
return size; return size;
} }
@ -491,8 +489,7 @@ int deserialize_in6_addrs(struct in6_addr **ret, const char *string) {
size++; size++;
} }
*ret = addresses; *ret = TAKE_PTR(addresses);
addresses = NULL;
return size; 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_size = size;
*ret_allocated = allocated; *ret_allocated = allocated;
*ret = routes; *ret = TAKE_PTR(routes);
routes = NULL;
return 0; return 0;
} }

View File

@ -807,8 +807,7 @@ int dhcp_lease_parse_search_domains(const uint8_t *option, size_t len, char ***d
pos = next_chunk; pos = next_chunk;
} }
*domains = names; *domains = TAKE_PTR(names);
names = NULL;
return cnt; return cnt;
} }

View File

@ -1470,8 +1470,7 @@ static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
return NULL; return NULL;
n->parent = parent; n->parent = parent;
n->path = s; n->path = TAKE_PTR(s);
s = NULL; /* do not free */
r = hashmap_put(bus->nodes, n->path, n); r = hashmap_put(bus->nodes, n->path, n);
if (r < 0) { if (r < 0) {

View File

@ -1342,8 +1342,7 @@ int bus_set_address_user(sd_bus *b) {
if (asprintf(&s, DEFAULT_USER_BUS_ADDRESS_FMT, ee) < 0) if (asprintf(&s, DEFAULT_USER_BUS_ADDRESS_FMT, ee) < 0)
return -ENOMEM; return -ENOMEM;
b->address = s; b->address = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }

View File

@ -141,8 +141,7 @@ _public_ int sd_listen_fds_with_names(int unset_environment, char ***names) {
return r; return r;
} }
*names = l; *names = TAKE_PTR(l);
l = NULL;
return n_fds; return n_fds;
} }

View File

@ -1297,8 +1297,7 @@ int device_get_id_filename(sd_device *device, const char **ret) {
} }
} }
device->id_filename = id; device->id_filename = TAKE_PTR(id);
id = NULL;
} }
*ret = device->id_filename; *ret = device->id_filename;

View File

@ -325,8 +325,7 @@ _public_ int sd_uid_get_display(uid_t uid, char **session) {
if (isempty(s)) if (isempty(s))
return -ENODATA; return -ENODATA;
*session = s; *session = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }
@ -355,8 +354,7 @@ static int file_of_seat(const char *seat, char **_p) {
if (!p) if (!p)
return -ENOMEM; return -ENOMEM;
*_p = p; *_p = TAKE_PTR(p);
p = NULL;
return 0; return 0;
} }
@ -529,8 +527,7 @@ _public_ int sd_session_get_state(const char *session, char **state) {
if (isempty(s)) if (isempty(s))
return -EIO; return -EIO;
*state = s; *state = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }
@ -575,8 +572,7 @@ static int session_get_string(const char *session, const char *field, char **val
if (isempty(s)) if (isempty(s))
return -ENODATA; return -ENODATA;
*value = s; *value = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }
@ -681,10 +677,8 @@ _public_ int sd_seat_get_active(const char *seat, char **session, uid_t *uid) {
return r; return r;
} }
if (session && s) { if (session && s)
*session = s; *session = TAKE_PTR(s);
s = NULL;
}
return 0; return 0;
} }
@ -909,10 +903,9 @@ _public_ int sd_get_machine_names(char ***machines) {
*b = NULL; *b = NULL;
} }
if (machines) { if (machines)
*machines = l; *machines = TAKE_PTR(l);
l = NULL;
}
return r; return r;
} }
@ -933,8 +926,7 @@ _public_ int sd_machine_get_class(const char *machine, char **class) {
if (!c) if (!c)
return -EIO; return -EIO;
*class = c; *class = TAKE_PTR(c);
c = NULL;
return 0; return 0;
} }

View 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; attributes[type].net_byteorder = RTA_FLAGS(rta) & NLA_F_NET_BYTEORDER;
} }
container->attributes = attributes; container->attributes = TAKE_PTR(attributes);
attributes = NULL;
container->n_attributes = count; container->n_attributes = count;
return 0; return 0;

View File

@ -51,8 +51,7 @@ _public_ int sd_network_get_operational_state(char **state) {
if (isempty(s)) if (isempty(s))
return -ENODATA; return -ENODATA;
*state = s; *state = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }
@ -81,8 +80,7 @@ static int network_get_strv(const char *key, char ***ret) {
strv_uniq(a); strv_uniq(a);
r = strv_length(a); r = strv_length(a);
*ret = a; *ret = TAKE_PTR(a);
a = NULL;
return r; return r;
} }
@ -121,8 +119,7 @@ static int network_link_get_string(int ifindex, const char *field, char **ret) {
if (isempty(s)) if (isempty(s))
return -ENODATA; return -ENODATA;
*ret = s; *ret = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }
@ -154,8 +151,7 @@ static int network_link_get_strv(int ifindex, const char *key, char ***ret) {
strv_uniq(a); strv_uniq(a);
r = strv_length(a); r = strv_length(a);
*ret = a; *ret = TAKE_PTR(a);
a = NULL;
return r; return r;
} }
@ -263,8 +259,7 @@ static int network_link_get_ifindexes(int ifindex, const char *key, int **ret) {
if (ifis) if (ifis)
ifis[c] = 0; /* Let's add a 0 ifindex to the end, to be nice */ ifis[c] = 0; /* Let's add a 0 ifindex to the end, to be nice */
*ret = ifis; *ret = TAKE_PTR(ifis);
ifis = NULL;
return c; return c;
} }

View File

@ -69,7 +69,6 @@ struct udev_enumerate {
**/ **/
_public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) { _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
_cleanup_free_ struct udev_enumerate *udev_enumerate = NULL; _cleanup_free_ struct udev_enumerate *udev_enumerate = NULL;
struct udev_enumerate *ret;
int r; int r;
assert_return_errno(udev, NULL, EINVAL); 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); udev_list_init(udev, &udev_enumerate->devices_list, false);
ret = udev_enumerate; return TAKE_PTR(udev_enumerate);
udev_enumerate = NULL;
return ret;
} }
/** /**

View File

@ -278,8 +278,7 @@ int locale_write_data(Context *c, char ***settings) {
if (r < 0) if (r < 0)
return r; return r;
*settings = l; *settings = TAKE_PTR(l);
l = NULL;
return 0; 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", log_debug("Found converted keymap %s at %s",
n, uncompressed ? p : pz); n, uncompressed ? p : pz);
*new_keymap = n; *new_keymap = TAKE_PTR(n);
n = NULL;
return 1; return 1;
} }
} }

View File

@ -560,8 +560,7 @@ void seat_complete_switch(Seat *s) {
if (!s->pending_switch) if (!s->pending_switch)
return; return;
session = s->pending_switch; session = TAKE_PTR(s->pending_switch);
s->pending_switch = NULL;
seat_set_active(s, session); seat_set_active(s, session);
} }

View File

@ -1271,8 +1271,7 @@ int session_set_controller(Session *s, const char *sender, bool force, bool prep
} }
session_release_controller(s, true); session_release_controller(s, true);
s->controller = name; s->controller = TAKE_PTR(name);
name = NULL;
session_save(s); session_save(s);
return 0; return 0;

View File

@ -438,8 +438,7 @@ static int parse_fdname(const char *fdname, char **session_id, dev_t *dev) {
return r; return r;
*dev = makedev(major, minor); *dev = makedev(major, minor);
*session_id = id; *session_id = TAKE_PTR(id);
id = NULL;
return 0; return 0;
} }

View File

@ -1475,8 +1475,7 @@ int machine_node_enumerator(sd_bus *bus, const char *path, void *userdata, char
return r; return r;
} }
*nodes = l; *nodes = TAKE_PTR(l);
l = NULL;
return 1; return 1;
} }

View File

@ -766,16 +766,14 @@ static int find_mount_points(const char *what, char ***list) {
if (!GREEDY_REALLOC(l, bufsize, n + 2)) if (!GREEDY_REALLOC(l, bufsize, n + 2))
return log_oom(); return log_oom();
l[n++] = where; l[n++] = TAKE_PTR(where);
where = NULL;
} }
if (!GREEDY_REALLOC(l, bufsize, n + 1)) if (!GREEDY_REALLOC(l, bufsize, n + 1))
return log_oom(); return log_oom();
l[n] = NULL; l[n] = NULL;
*list = l; *list = TAKE_PTR(l);
l = NULL; /* avoid freeing */
return n; return n;
} }
@ -827,8 +825,7 @@ static int find_loop_device(const char *backing_file, char **loop_dev) {
if (!l) if (!l)
return -ENXIO; return -ENXIO;
*loop_dev = l; *loop_dev = TAKE_PTR(l);
l = NULL; /* avoid freeing */
return 0; return 0;
} }

View 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] &= 0xfe; /* clear multicast bit */
mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */ mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
*ret = mac; *ret = TAKE_PTR(mac);
mac = NULL;
return 0; return 0;
} }

View 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->resolve_retry_event_source = sd_event_source_unref(w->resolve_retry_event_source);
w->unresolved_endpoints = w->failed_endpoints; w->unresolved_endpoints = TAKE_PTR(w->failed_endpoints);
w->failed_endpoints = NULL;
resolve_endpoints(netdev); resolve_endpoints(netdev);

View File

@ -82,8 +82,7 @@ int link_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***
} }
l[c] = NULL; l[c] = NULL;
*nodes = l; *nodes = TAKE_PTR(l);
l = NULL;
return 1; return 1;
} }

View File

@ -122,8 +122,7 @@ int network_node_enumerator(sd_bus *bus, const char *path, void *userdata, char
return r; return r;
} }
*nodes = l; *nodes = TAKE_PTR(l);
l = NULL;
return 1; return 1;
} }

View File

@ -72,8 +72,7 @@ int network_config_section_new(const char *filename, unsigned line, NetworkConfi
strcpy(cs->filename, filename); strcpy(cs->filename, filename);
cs->line = line; cs->line = line;
*s = cs; *s = TAKE_PTR(cs);
cs = NULL;
return 0; return 0;
} }

View File

@ -126,8 +126,7 @@ int prefix_new(Prefix **ret) {
if (sd_radv_prefix_new(&prefix->radv_prefix) < 0) if (sd_radv_prefix_new(&prefix->radv_prefix) < 0)
return -ENOMEM; return -ENOMEM;
*ret = prefix; *ret = TAKE_PTR(prefix);
prefix = NULL;
return 0; return 0;
} }
@ -344,8 +343,7 @@ static int radv_get_ip6dns(Network *network, struct in6_addr **dns,
} }
if (addresses) { if (addresses) {
*dns = addresses; *dns = TAKE_PTR(addresses);
addresses = NULL;
*n_dns = n_addresses; *n_dns = n_addresses;
} }

View File

@ -857,8 +857,7 @@ static int routing_policy_rule_read_full_file(const char *state_file, char **ret
if (size <= 0) if (size <= 0)
return -ENODATA; return -ENODATA;
*ret = s; *ret = TAKE_PTR(s);
s = NULL;
return size; return size;
} }

View 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])) !source_path_is_valid(lower[1]))
return -EINVAL; return -EINVAL;
upper = lower[1]; upper = TAKE_PTR(lower[1]);
lower[1] = NULL;
destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */ destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */
if (!destination) 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. */ * the "upper", and all before that the "lower" directories. */
destination = lower[k - 1]; destination = lower[k - 1];
upper = lower[k - 2]; upper = TAKE_PTR(lower[k - 2]);
lower[k - 2] = NULL;
STRV_FOREACH(i, lower) STRV_FOREACH(i, lower)
if (!source_path_is_valid(*i)) if (!source_path_is_valid(*i))

View File

@ -1175,8 +1175,7 @@ static int parse_argv(int argc, char *argv[]) {
* accept this here, and silently make "--ephemeral --template=" equivalent to "--ephemeral * accept this here, and silently make "--ephemeral --template=" equivalent to "--ephemeral
* --directory=". */ * --directory=". */
arg_directory = arg_template; arg_directory = TAKE_PTR(arg_template);
arg_template = NULL;
} }
if (arg_template && !(arg_directory || arg_machine)) { if (arg_template && !(arg_directory || arg_machine)) {

View File

@ -56,8 +56,7 @@ int dns_search_domain_new(
d->n_ref = 1; d->n_ref = 1;
d->manager = m; d->manager = m;
d->type = type; d->type = type;
d->name = normalized; d->name = TAKE_PTR(normalized);
normalized = NULL;
switch (type) { switch (type) {

View File

@ -36,8 +36,7 @@ void dns_zone_item_probe_stop(DnsZoneItem *i) {
if (!i->probe_transaction) if (!i->probe_transaction)
return; return;
t = i->probe_transaction; t = TAKE_PTR(i->probe_transaction);
i->probe_transaction = NULL;
set_remove(t->notify_zone_items, i); set_remove(t->notify_zone_items, i);
set_remove(t->notify_zone_items_done, i); set_remove(t->notify_zone_items_done, i);

View File

@ -139,8 +139,7 @@ int dnssd_node_enumerator(sd_bus *bus, const char *path, void *userdata, char **
} }
l[c] = NULL; l[c] = NULL;
*nodes = l; *nodes = TAKE_PTR(l);
l = NULL;
return 1; return 1;
} }

View File

@ -198,8 +198,7 @@ int dnssd_render_instance_name(DnssdService *s, char **ret_name) {
return -EINVAL; return -EINVAL;
} }
*ret_name = name; *ret_name = TAKE_PTR(name);
name = NULL;
return 0; return 0;
} }
@ -319,8 +318,7 @@ int dnssd_txt_item_new_from_string(const char *key, const char *value, DnsTxtIte
} }
i->length = length; i->length = length;
*ret_item = i; *ret_item = TAKE_PTR(i);
i = NULL;
return 0; 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; i->length = length;
*ret_item = i; *ret_item = TAKE_PTR(i);
i = NULL;
return 0; return 0;
} }

View 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); set_free_free(l->dnssec_negative_trust_anchors);
l->dnssec_negative_trust_anchors = ns; l->dnssec_negative_trust_anchors = TAKE_PTR(ns);
ns = NULL;
(void) link_save_user(l); (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; l[c] = NULL;
*nodes = l; *nodes = TAKE_PTR(l);
l = NULL;
return 1; return 1;
} }

View File

@ -440,8 +440,7 @@ static int link_update_dnssec_negative_trust_anchors(Link *l) {
return r; return r;
set_free_free(l->dnssec_negative_trust_anchors); set_free_free(l->dnssec_negative_trust_anchors);
l->dnssec_negative_trust_anchors = ns; l->dnssec_negative_trust_anchors = TAKE_PTR(ns);
ns = NULL;
return 0; return 0;
@ -1293,8 +1292,7 @@ int link_load_user(Link *l) {
if (r < 0) if (r < 0)
goto fail; goto fail;
l->dnssec_negative_trust_anchors = ns; l->dnssec_negative_trust_anchors = TAKE_PTR(ns);
ns = NULL;
} }
return 0; return 0;

View File

@ -396,11 +396,8 @@ static int determine_hostname(char **full_hostname, char **llmnr_hostname, char
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to determine mDNS hostname: %m"); return log_error_errno(r, "Failed to determine mDNS hostname: %m");
*llmnr_hostname = n; *llmnr_hostname = TAKE_PTR(n);
n = NULL; *full_hostname = TAKE_PTR(h);
*full_hostname = h;
h = NULL;
return 0; return 0;
} }
@ -445,11 +442,8 @@ static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname,
if (!h) if (!h)
return log_oom(); return log_oom();
*llmnr_hostname = n; *llmnr_hostname = TAKE_PTR(n);
n = NULL; *mdns_hostname = TAKE_PTR(m);
*mdns_hostname = m;
m = NULL;
*full_hostname = h; *full_hostname = h;

View File

@ -730,8 +730,7 @@ int ask_password_agent(
if (keyname) if (keyname)
(void) add_to_keyring_and_log(keyname, flags, l); (void) add_to_keyring_and_log(keyname, flags, l);
*ret = l; *ret = TAKE_PTR(l);
l = NULL;
r = 0; r = 0;
finish: finish:

View File

@ -1094,8 +1094,7 @@ static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_
return r; return r;
strv_free(*p); strv_free(*p);
*p = l; *p = TAKE_PTR(l);
l = NULL;
return 0; return 0;
} }

View File

@ -189,8 +189,7 @@ int show_cgroup_by_path(
free(last); free(last);
} }
last = k; last = TAKE_PTR(k);
k = NULL;
} }
if (r < 0) if (r < 0)

View File

@ -295,8 +295,7 @@ int dns_label_escape_new(const char *p, size_t l, char **ret) {
if (r < 0) if (r < 0)
return r; return r;
*ret = s; *ret = TAKE_PTR(s);
s = NULL;
return r; 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 */ /* Not the same, let's jump back, and try with the next label again */
s = suffix; s = suffix;
n = saved_n; n = TAKE_PTR(saved_n);
saved_n = NULL;
} }
} }
} }

View File

@ -249,8 +249,7 @@ int efi_get_variable(
((char*) buf)[st.st_size - 4] = 0; ((char*) buf)[st.st_size - 4] = 0;
((char*) buf)[st.st_size - 4 + 1] = 0; ((char*) buf)[st.st_size - 4 + 1] = 0;
*value = buf; *value = TAKE_PTR(buf);
buf = NULL;
*size = (size_t) st.st_size - 4; *size = (size_t) st.st_size - 4;
if (attribute) if (attribute)
@ -563,8 +562,7 @@ int efi_get_boot_order(uint16_t **order) {
l / sizeof(uint16_t) > INT_MAX) l / sizeof(uint16_t) > INT_MAX)
return -EINVAL; return -EINVAL;
*order = buf; *order = TAKE_PTR(buf);
buf = NULL;
return (int) (l / sizeof(uint16_t)); return (int) (l / sizeof(uint16_t));
} }

View File

@ -25,6 +25,7 @@
#include "sd-daemon.h" #include "sd-daemon.h"
#include "alloc-util.h"
#include "dirent-util.h" #include "dirent-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "fdset.h" #include "fdset.h"
@ -168,8 +169,7 @@ int fdset_new_fill(FDSet **_s) {
} }
r = 0; r = 0;
*_s = s; *_s = TAKE_PTR(s);
s = NULL;
finish: finish:
/* We won't close the fds here! */ /* We won't close the fds here! */

View File

@ -169,10 +169,8 @@ answer:
*filtered = f; *filtered = f;
} }
if (value) { if (value)
*value = v; *value = TAKE_PTR(v);
v = NULL;
}
return !!n; return !!n;
} }
@ -201,8 +199,7 @@ int fstab_extract_values(const char *opts, const char *name, char ***values) {
return r; return r;
} }
*values = res; *values = TAKE_PTR(res);
res = NULL;
return !!*values; return !!*values;
} }

View File

@ -153,8 +153,7 @@ int raw_strip_suffixes(const char *p, char **ret) {
break; break;
} }
*ret = q; *ret = TAKE_PTR(q);
q = NULL;
return 0; return 0;
} }

View File

@ -50,10 +50,8 @@ static int specifier_prefix_and_instance(char specifier, void *data, void *userd
if (!ans) if (!ans)
return -ENOMEM; return -ENOMEM;
*ret = ans; *ret = ans;
} else { } else
*ret = prefix; *ret = TAKE_PTR(prefix);
prefix = NULL;
}
return 0; return 0;
} }

View File

@ -1422,8 +1422,7 @@ static int unit_file_search(
r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags); r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags);
if (r >= 0) { if (r >= 0) {
info->path = path; info->path = TAKE_PTR(path);
path = NULL;
result = r; result = r;
found_unit = true; found_unit = true;
break; break;
@ -1446,8 +1445,7 @@ static int unit_file_search(
r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags); r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags);
if (r >= 0) { if (r >= 0) {
info->path = path; info->path = TAKE_PTR(path);
path = NULL;
result = r; result = r;
found_unit = true; found_unit = true;
break; break;
@ -1754,8 +1752,7 @@ static int install_info_symlink_wants(
if (r < 0) if (r < 0)
return r; return r;
path = instance.path; path = TAKE_PTR(instance.path);
instance.path = NULL;
if (instance.type == UNIT_FILE_TYPE_MASKED) { if (instance.type == UNIT_FILE_TYPE_MASKED) {
unit_file_changes_add(changes, n_changes, -ERFKILL, path, NULL); unit_file_changes_add(changes, n_changes, -ERFKILL, path, NULL);

View File

@ -80,7 +80,6 @@ Image *image_unref(Image *i) {
static char **image_settings_path(Image *image) { static char **image_settings_path(Image *image) {
_cleanup_strv_free_ char **l = NULL; _cleanup_strv_free_ char **l = NULL;
char **ret;
const char *fn, *s; const char *fn, *s;
unsigned i = 0; unsigned i = 0;
@ -104,10 +103,7 @@ static char **image_settings_path(Image *image) {
if (!l[i]) if (!l[i])
return NULL; return NULL;
ret = l; return TAKE_PTR(l);
l = NULL;
return ret;
} }
static char *image_roothash_path(Image *image) { static char *image_roothash_path(Image *image) {

View File

@ -114,8 +114,7 @@ int namespace_flag_to_string_many(unsigned long flags, char **ret) {
return -ENOMEM; return -ENOMEM;
} }
*ret = s; *ret = TAKE_PTR(s);
s = NULL;
return 0; return 0;
} }

View File

@ -181,7 +181,6 @@ static char** user_dirs(
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL; _cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
_cleanup_free_ char *data_home = NULL; _cleanup_free_ char *data_home = NULL;
_cleanup_strv_free_ char **res = NULL; _cleanup_strv_free_ char **res = NULL;
char **tmp;
int r; int r;
r = xdg_user_dirs(&config_dirs, &data_dirs); r = xdg_user_dirs(&config_dirs, &data_dirs);
@ -242,10 +241,7 @@ static char** user_dirs(
if (path_strv_make_absolute_cwd(res) < 0) if (path_strv_make_absolute_cwd(res) < 0)
return NULL; return NULL;
tmp = res; return TAKE_PTR(res);
res = NULL;
return tmp;
} }
bool path_is_user_data_dir(const char *path) { 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; *runtime = NULL;
} }
*persistent = a; *persistent = TAKE_PTR(a);
a = NULL;
return 0; return 0;
@ -413,8 +408,7 @@ static int acquire_control_dirs(UnitFileScope scope, char **persistent, char **r
if (!b) if (!b)
return -ENOMEM; return -ENOMEM;
*runtime = b; *runtime = TAKE_PTR(b);
b = NULL;
break; break;
} }
@ -443,8 +437,7 @@ static int acquire_control_dirs(UnitFileScope scope, char **persistent, char **r
assert_not_reached("Hmm, unexpected scope value."); assert_not_reached("Hmm, unexpected scope value.");
} }
*persistent = a; *persistent = TAKE_PTR(a);
a = NULL;
return 0; return 0;
} }

View File

@ -117,8 +117,7 @@ int specifier_printf(const char *text, const Specifier table[], void *userdata,
*(t++) = '%'; *(t++) = '%';
*t = 0; *t = 0;
*_ret = ret; *_ret = TAKE_PTR(ret);
ret = NULL;
return 0; return 0;
} }

View File

@ -1924,8 +1924,7 @@ static int get_machine_list(
return log_oom(); return log_oom();
machine_infos[c].is_host = true; machine_infos[c].is_host = true;
machine_infos[c].name = hn; machine_infos[c].name = TAKE_PTR(hn);
hn = NULL;
(void) get_machine_properties(bus, &machine_infos[c]); (void) get_machine_properties(bus, &machine_infos[c]);
c++; c++;
@ -2462,10 +2461,9 @@ static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **un
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to access path '%s': %m", path); return log_error_errno(r, "Failed to access path '%s': %m", path);
if (unit_path) { if (unit_path)
*unit_path = lpath; *unit_path = TAKE_PTR(lpath);
lpath = NULL;
}
return 1; return 1;
} }
@ -2497,10 +2495,9 @@ static int unit_find_template_path(
if (r < 0) if (r < 0)
return r; return r;
if (template) { if (template)
*template = _template; *template = TAKE_PTR(_template);
_template = NULL;
}
return r; return r;
} }
@ -6671,8 +6668,7 @@ static int create_edit_temp_file(const char *new_path, const char *original_path
} else if (r < 0) } else if (r < 0)
return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path); return log_error_errno(r, "Failed to create temporary file for \"%s\": %m", new_path);
*ret_tmp_fn = t; *ret_tmp_fn = TAKE_PTR(t);
t = NULL;
return 0; return 0;
} }
@ -6703,12 +6699,9 @@ static int get_file_to_edit(
return -EEXIST; return -EEXIST;
} }
*ret_path = run; *ret_path = TAKE_PTR(run);
run = NULL; } else
} else { *ret_path = TAKE_PTR(path);
*ret_path = path;
path = NULL;
}
return 0; return 0;
} }

View 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)) if (!path_strv_resolve_uniq(l, NULL))
return log_oom(); return log_oom();
*ret = l; *ret = TAKE_PTR(l);
l = NULL;
return 0; return 0;
} }

View File

@ -310,8 +310,7 @@ static int user_config_paths(char*** ret) {
if (r < 0) if (r < 0)
return r; return r;
*ret = res; *ret = TAKE_PTR(res);
res = NULL;
return 0; return 0;
} }

View File

@ -304,8 +304,7 @@ static int get_stringset(int fd, struct ifreq *ifr, int stringset_id, struct eth
if (r < 0) if (r < 0)
return -errno; return -errno;
*gstrings = strings; *gstrings = TAKE_PTR(strings);
strings = NULL;
return 0; return 0;
} }

View File

@ -62,10 +62,8 @@ static void path_prepend(char **path, const char *fmt, ...) {
} }
free_and_replace(*path, new); free_and_replace(*path, new);
} else { } else
*path = pre; *path = TAKE_PTR(pre);
pre = NULL;
}
} }
/* /*

View File

@ -233,8 +233,7 @@ static int worker_new(struct worker **ret, Manager *manager, struct udev_monitor
if (r < 0) if (r < 0)
return r; return r;
*ret = worker; *ret = TAKE_PTR(worker);
worker = NULL;
return 0; return 0;
} }

View 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 */ /* all checks passed, return this one as a source console */
*ret_idx = i; *ret_idx = i;
*ret_path = path; *ret_path = TAKE_PTR(path);
path = NULL;
ret_fd = fd; ret_fd = fd;
fd = -1; fd = -1;
return ret_fd; return ret_fd;