summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/alloc-util.h9
-rw-r--r--src/basic/cap-list.c3
-rw-r--r--src/basic/cgroup-util.c27
-rw-r--r--src/basic/extract-word.c3
-rw-r--r--src/basic/fileio.c9
-rw-r--r--src/basic/fs-util.c19
-rw-r--r--src/basic/locale-util.c3
-rw-r--r--src/basic/mount-util.c9
-rw-r--r--src/basic/parse-util.c3
-rw-r--r--src/basic/path-util.c3
-rw-r--r--src/basic/proc-cmdline.c6
-rw-r--r--src/basic/process-util.c5
-rw-r--r--src/basic/socket-util.c3
-rw-r--r--src/basic/strv.c6
-rw-r--r--src/basic/terminal-util.c13
-rw-r--r--src/basic/unit-name.c3
16 files changed, 55 insertions, 69 deletions
diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h
index ec7808c1f..b1e0edbb7 100644
--- a/src/basic/alloc-util.h
+++ b/src/basic/alloc-util.h
@@ -130,3 +130,12 @@ void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
_new_ = alloca_align(_size_, (align)); \
(void*)memset(_new_, 0, _size_); \
})
+
+/* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to
+ * NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
+#define TAKE_PTR(ptr) \
+ ({ \
+ typeof(ptr) _ptr_ = (ptr); \
+ (ptr) = NULL; \
+ _ptr_; \
+ })
diff --git a/src/basic/cap-list.c b/src/basic/cap-list.c
index c4557666e..9416391a5 100644
--- a/src/basic/cap-list.c
+++ b/src/basic/cap-list.c
@@ -103,8 +103,7 @@ int capability_set_to_string_alloc(uint64_t set, char **s) {
str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
- *s = str;
- str = NULL;
+ *s = TAKE_PTR(str);
return 0;
}
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c
index 4fc542cc8..dbc2938c0 100644
--- a/src/basic/cgroup-util.c
+++ b/src/basic/cgroup-util.c
@@ -1452,10 +1452,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);
@@ -2087,6 +2086,14 @@ int cg_slice_to_path(const char *unit, char **ret) {
_cleanup_free_ char *escaped = NULL;
char n[dash - p + sizeof(".slice")];
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+ /* msan doesn't instrument stpncpy, so it thinks
+ * n is later used unitialized:
+ * https://github.com/google/sanitizers/issues/926
+ */
+ zero(n);
+#endif
+
/* Don't allow trailing or double dashes */
if (IN_SET(dash[1], 0, '-'))
return -EINVAL;
@@ -2112,8 +2119,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;
}
@@ -2406,8 +2412,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;
}
@@ -2590,7 +2595,7 @@ static int cg_unified_update(void) {
return 0;
if (statfs("/sys/fs/cgroup/", &fs) < 0)
- return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/\" failed: %m");
+ return log_debug_errno(errno, "statfs(\"/sys/fs/cgroup/\") failed: %m");
if (F_TYPE_EQUAL(fs.f_type, CGROUP2_SUPER_MAGIC)) {
log_debug("Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy");
@@ -2727,10 +2732,8 @@ int cg_enable_everywhere(CGroupMask supported, CGroupMask mask, const char *p) {
}
r = write_string_stream(f, s, 0);
- if (r < 0) {
+ if (r < 0)
log_debug_errno(r, "Failed to enable controller %s for %s (%s): %m", n, p, fs);
- clearerr(f);
- }
}
}
diff --git a/src/basic/extract-word.c b/src/basic/extract-word.c
index b900d7e19..4bbd78ac5 100644
--- a/src/basic/extract-word.c
+++ b/src/basic/extract-word.c
@@ -194,8 +194,7 @@ finish:
finish_force_next:
s[sz] = 0;
- *ret = s;
- s = NULL;
+ *ret = TAKE_PTR(s);
return 1;
}
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 2e8991925..dd8fb1fcb 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -330,8 +330,7 @@ int read_full_stream(FILE *f, char **contents, size_t *size) {
}
buf[l] = 0;
- *contents = buf;
- buf = NULL; /* do not free */
+ *contents = TAKE_PTR(buf);
if (size)
*size = l;
@@ -1441,8 +1440,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;
}
@@ -1530,8 +1528,7 @@ int read_nul_string(FILE *f, char **ret) {
return -ENOMEM;
}
- *ret = x;
- x = NULL;
+ *ret = TAKE_PTR(x);
return 0;
}
diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c
index e556ff7e0..f5764454f 100644
--- a/src/basic/fs-util.c
+++ b/src/basic/fs-util.c
@@ -469,10 +469,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;
}
@@ -853,10 +851,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';
@@ -878,10 +875,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;
diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c
index 266cb2993..de3d7c8c8 100644
--- a/src/basic/locale-util.c
+++ b/src/basic/locale-util.c
@@ -341,8 +341,7 @@ int get_keymaps(char ***ret) {
strv_sort(l);
- *ret = l;
- l = NULL;
+ *ret = TAKE_PTR(l);
return 0;
}
diff --git a/src/basic/mount-util.c b/src/basic/mount-util.c
index db90194ff..b74d51a26 100644
--- a/src/basic/mount-util.c
+++ b/src/basic/mount-util.c
@@ -81,10 +81,8 @@ int name_to_handle_at_loop(
if (name_to_handle_at(fd, path, h, &mnt_id, flags) >= 0) {
- if (ret_handle) {
- *ret_handle = h;
- h = NULL;
- }
+ if (ret_handle)
+ *ret_handle = TAKE_PTR(h);
if (ret_mnt_id)
*ret_mnt_id = mnt_id;
@@ -956,8 +954,7 @@ int mount_option_mangle(
}
*ret_mount_flags = mount_flags;
- *ret_remaining_options = ret;
- ret = NULL;
+ *ret_remaining_options = TAKE_PTR(ret);
return 0;
}
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index 4c2625389..d5886b1f8 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -329,8 +329,7 @@ int parse_syscall_and_errno(const char *in, char **name, int *error) {
return -EINVAL;
*error = e;
- *name = n;
- n = NULL;
+ *name = TAKE_PTR(n);
return 0;
}
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index c350bc85e..aca5f486d 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -294,8 +294,7 @@ char **path_strv_resolve(char **l, const char *root) {
r = chase_symlinks(t, root, 0, &u);
if (r == -ENOENT) {
if (root) {
- u = orig;
- orig = NULL;
+ u = TAKE_PTR(orig);
free(t);
} else
u = t;
diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c
index 99d00ab0f..1d1226b28 100644
--- a/src/basic/proc-cmdline.c
+++ b/src/basic/proc-cmdline.c
@@ -207,10 +207,8 @@ int proc_cmdline_get_key(const char *key, unsigned flags, char **value) {
}
}
- if (value) {
- *value = ret;
- ret = NULL;
- }
+ if (value)
+ *value = TAKE_PTR(ret);
return found;
}
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 81c4077c8..b90272e97 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -626,8 +626,7 @@ int get_process_environ(pid_t pid, char **env) {
} else
outcome[sz] = '\0';
- *env = outcome;
- outcome = NULL;
+ *env = TAKE_PTR(outcome);
return 0;
}
@@ -1479,7 +1478,7 @@ static const char *const ioprio_class_table[] = {
[IOPRIO_CLASS_IDLE] = "idle"
};
-DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, INT_MAX);
+DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ioprio_class, int, IOPRIO_N_CLASSES);
static const char *const sigchld_code_table[] = {
[CLD_EXITED] = "exited",
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index c94e2899d..23a533700 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -1011,8 +1011,7 @@ int getpeersec(int fd, char **ret) {
if (isempty(s))
return -EOPNOTSUPP;
- *ret = s;
- s = NULL;
+ *ret = TAKE_PTR(s);
return 0;
}
diff --git a/src/basic/strv.c b/src/basic/strv.c
index b6f762702..2c635b951 100644
--- a/src/basic/strv.c
+++ b/src/basic/strv.c
@@ -345,8 +345,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;
}
@@ -357,8 +356,7 @@ int strv_split_extract(char ***t, const char *s, const char *separators, Extract
return -ENOMEM;
}
- *t = l;
- l = NULL;
+ *t = TAKE_PTR(l);
return (int) n;
}
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index d1d88ab93..3ae079a2b 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -717,10 +717,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);
@@ -788,8 +787,7 @@ int get_kernel_consoles(char ***ret) {
goto fallback;
}
- *ret = l;
- l = NULL;
+ *ret = TAKE_PTR(l);
return 0;
@@ -798,8 +796,7 @@ fallback:
if (r < 0)
return r;
- *ret = l;
- l = NULL;
+ *ret = TAKE_PTR(l);
return 0;
}
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
index 1d938f662..e94f1d6e9 100644
--- a/src/basic/unit-name.c
+++ b/src/basic/unit-name.c
@@ -368,8 +368,7 @@ int unit_name_unescape(const char *f, char **ret) {
*t = 0;
- *ret = r;
- r = NULL;
+ *ret = TAKE_PTR(r);
return 0;
}