summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coccinelle/free_and_replace.cocci15
-rw-r--r--src/basic/alloc-util.h8
-rw-r--r--src/basic/fs-util.c8
-rw-r--r--src/basic/path-util.c4
-rw-r--r--src/core/load-fragment.c10
-rw-r--r--src/core/mount.c14
-rw-r--r--src/core/service.c4
-rw-r--r--src/journal-remote/journal-upload.c4
-rw-r--r--src/shared/install.c10
9 files changed, 35 insertions, 42 deletions
diff --git a/coccinelle/free_and_replace.cocci b/coccinelle/free_and_replace.cocci
new file mode 100644
index 0000000000..9dcdbf4d42
--- /dev/null
+++ b/coccinelle/free_and_replace.cocci
@@ -0,0 +1,15 @@
+@@
+expression p, q;
+@@
+- free(p);
+- p = q;
+- q = NULL;
+- return 0;
++ return free_and_replace(p, q);
+@@
+expression p, q;
+@@
+- free(p);
+- p = q;
+- q = NULL;
++ free_and_replace(p, q);
diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h
index ceeee519b7..a44dd473c1 100644
--- a/src/basic/alloc-util.h
+++ b/src/basic/alloc-util.h
@@ -43,6 +43,14 @@ static inline void *mfree(void *memory) {
return NULL;
}
+#define free_and_replace(a, b) \
+ ({ \
+ free(a); \
+ (a) = (b); \
+ (b) = NULL; \
+ 0; \
+ })
+
void* memdup(const void *p, size_t l) _alloc_(2);
static inline void freep(void *p) {
diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c
index 86d9ad7e36..48952a1c26 100644
--- a/src/basic/fs-util.c
+++ b/src/basic/fs-util.c
@@ -678,9 +678,7 @@ int chase_symlinks(const char *path, const char *_root, char **ret) {
!path_startswith(parent, root))
return -EINVAL;
- free(done);
- done = parent;
- parent = NULL;
+ free_and_replace(done, parent);
fd_parent = openat(fd, "..", O_CLOEXEC|O_NOFOLLOW|O_PATH);
if (fd_parent < 0)
@@ -724,9 +722,7 @@ int chase_symlinks(const char *path, const char *_root, char **ret) {
if (fd < 0)
return -errno;
- free(buffer);
- buffer = destination;
- destination = NULL;
+ free_and_replace(buffer, destination);
todo = buffer;
free(done);
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index a76963aa9f..0f5b20cf05 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -288,9 +288,7 @@ char **path_strv_resolve(char **l, const char *prefix) {
} else {
/* canonicalized path goes outside of
* prefix, keep the original path instead */
- free(u);
- u = orig;
- orig = NULL;
+ free_and_replace(u, orig);
}
} else
free(t);
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 06c156a623..8cc7a8e765 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -1591,11 +1591,7 @@ int config_parse_fdname(
return 0;
}
- free(s->fdname);
- s->fdname = p;
- p = NULL;
-
- return 0;
+ return free_and_replace(s->fdname, p);
}
int config_parse_service_sockets(
@@ -2052,9 +2048,7 @@ int config_parse_working_directory(
return 0;
}
- free(c->working_directory);
- c->working_directory = k;
- k = NULL;
+ free_and_replace(c->working_directory, k);
c->working_directory_home = false;
}
diff --git a/src/core/mount.c b/src/core/mount.c
index 15619dffe3..da480001e1 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -1484,17 +1484,9 @@ static int mount_setup_unit(
MOUNT(u)->from_proc_self_mountinfo = true;
- free(p->what);
- p->what = w;
- w = NULL;
-
- free(p->options);
- p->options = o;
- o = NULL;
-
- free(p->fstype);
- p->fstype = f;
- f = NULL;
+ free_and_replace(p->what, w);
+ free_and_replace(p->options, o);
+ free_and_replace(p->fstype, f);
if (load_extras) {
r = mount_add_extras(MOUNT(u));
diff --git a/src/core/service.c b/src/core/service.c
index 63045ede55..c949de9cbe 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -3088,9 +3088,7 @@ static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds)
if (!streq_ptr(s->status_text, t)) {
- free(s->status_text);
- s->status_text = t;
- t = NULL;
+ free_and_replace(s->status_text, t);
notify_dbus = true;
}
diff --git a/src/journal-remote/journal-upload.c b/src/journal-remote/journal-upload.c
index c0f967ab94..61190ff83c 100644
--- a/src/journal-remote/journal-upload.c
+++ b/src/journal-remote/journal-upload.c
@@ -527,9 +527,7 @@ static int perform_upload(Uploader *u) {
log_debug("Upload finished successfully with code %ld: %s",
status, strna(u->answer));
- free(u->last_cursor);
- u->last_cursor = u->current_cursor;
- u->current_cursor = NULL;
+ free_and_replace(u->last_cursor, u->current_cursor);
return update_cursor_state(u);
}
diff --git a/src/shared/install.c b/src/shared/install.c
index c1ef62b337..f70b3e3dd5 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -1107,11 +1107,7 @@ static int config_parse_default_instance(
if (!unit_instance_is_valid(printed))
return -EINVAL;
- free(i->default_instance);
- i->default_instance = printed;
- printed = NULL;
-
- return 0;
+ return free_and_replace(i->default_instance, printed);
}
static int unit_file_load(
@@ -1357,9 +1353,7 @@ static int install_info_follow(
if (!streq(basename(i->symlink_target), i->name))
return -EXDEV;
- free(i->path);
- i->path = i->symlink_target;
- i->symlink_target = NULL;
+ free_and_replace(i->path, i->symlink_target);
i->type = _UNIT_FILE_TYPE_INVALID;
return unit_file_load_or_readlink(c, i, i->path, root_dir, flags);