]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: introduce free_and_replace helper 4391/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 16 Oct 2016 23:23:35 +0000 (19:23 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 17 Oct 2016 03:35:39 +0000 (23:35 -0400)
It's a common pattern, so add a helper for it. A macro is necessary
because a function that takes a pointer to a pointer would be type specific,
similarly to cleanup functions. Seems better to use a macro.

coccinelle/free_and_replace.cocci [new file with mode: 0644]
src/basic/alloc-util.h
src/basic/fs-util.c
src/basic/path-util.c
src/core/load-fragment.c
src/core/mount.c
src/core/service.c
src/journal-remote/journal-upload.c
src/shared/install.c

diff --git a/coccinelle/free_and_replace.cocci b/coccinelle/free_and_replace.cocci
new file mode 100644 (file)
index 0000000..9dcdbf4
--- /dev/null
@@ -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);
index ceeee519b79f864ccbf359fede0c91c3364c7e36..a44dd473c1106574472ae2cd84e7db7f2df935ae 100644 (file)
@@ -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) {
index 86d9ad7e368a3ae52a70e9c72be888b342bf4493..48952a1c2633373203f1f151080e79f664b8a54e 100644 (file)
@@ -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);
index a76963aa9fd0151656cd5f98ae3bb57761a3de24..0f5b20cf05d1c336a87ec4a8a8a94bb89f2a96ce 100644 (file)
@@ -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);
index 06c156a623fc9e129719f11be79e48a40f01293f..8cc7a8e765cda18a681c450b681f5b2899f54395 100644 (file)
@@ -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;
         }
index 15619dffe34435037437e794e4d4df38679bc9aa..da480001e11e772d9f8806261ee18f310913f967 100644 (file)
@@ -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));
index 63045ede55dd7bafcd40ea1d3303c2a4ab21634b..c949de9cbe7b02446ab5b5fb91abdae810a3c125 100644 (file)
@@ -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;
                 }
index c0f967ab94e1194c2fc6e4a61ae87bd68fde17f8..61190ff83cab6f165d5ec53f14c4c32580c4d9fc 100644 (file)
@@ -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);
 }
index c1ef62b3378ccde0a5b86cc29169282bc411f175..f70b3e3dd5694b43a8d2bd2f697ebf0d7ae6e38e 100644 (file)
@@ -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);