tree-wide: introduce free_and_replace helper
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.
This commit is contained in:
parent
6b430fdb7c
commit
3b319885c4
15
coccinelle/free_and_replace.cocci
Normal file
15
coccinelle/free_and_replace.cocci
Normal file
|
@ -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);
|
|
@ -43,6 +43,14 @@ static inline void *mfree(void *memory) {
|
||||||
return NULL;
|
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);
|
void* memdup(const void *p, size_t l) _alloc_(2);
|
||||||
|
|
||||||
static inline void freep(void *p) {
|
static inline void freep(void *p) {
|
||||||
|
|
|
@ -678,9 +678,7 @@ int chase_symlinks(const char *path, const char *_root, char **ret) {
|
||||||
!path_startswith(parent, root))
|
!path_startswith(parent, root))
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
free(done);
|
free_and_replace(done, parent);
|
||||||
done = parent;
|
|
||||||
parent = NULL;
|
|
||||||
|
|
||||||
fd_parent = openat(fd, "..", O_CLOEXEC|O_NOFOLLOW|O_PATH);
|
fd_parent = openat(fd, "..", O_CLOEXEC|O_NOFOLLOW|O_PATH);
|
||||||
if (fd_parent < 0)
|
if (fd_parent < 0)
|
||||||
|
@ -724,9 +722,7 @@ int chase_symlinks(const char *path, const char *_root, char **ret) {
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
free(buffer);
|
free_and_replace(buffer, destination);
|
||||||
buffer = destination;
|
|
||||||
destination = NULL;
|
|
||||||
|
|
||||||
todo = buffer;
|
todo = buffer;
|
||||||
free(done);
|
free(done);
|
||||||
|
|
|
@ -288,9 +288,7 @@ char **path_strv_resolve(char **l, const char *prefix) {
|
||||||
} else {
|
} else {
|
||||||
/* canonicalized path goes outside of
|
/* canonicalized path goes outside of
|
||||||
* prefix, keep the original path instead */
|
* prefix, keep the original path instead */
|
||||||
free(u);
|
free_and_replace(u, orig);
|
||||||
u = orig;
|
|
||||||
orig = NULL;
|
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
free(t);
|
free(t);
|
||||||
|
|
|
@ -1591,11 +1591,7 @@ int config_parse_fdname(
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(s->fdname);
|
return free_and_replace(s->fdname, p);
|
||||||
s->fdname = p;
|
|
||||||
p = NULL;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int config_parse_service_sockets(
|
int config_parse_service_sockets(
|
||||||
|
@ -2052,9 +2048,7 @@ int config_parse_working_directory(
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(c->working_directory);
|
free_and_replace(c->working_directory, k);
|
||||||
c->working_directory = k;
|
|
||||||
k = NULL;
|
|
||||||
|
|
||||||
c->working_directory_home = false;
|
c->working_directory_home = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1484,17 +1484,9 @@ static int mount_setup_unit(
|
||||||
|
|
||||||
MOUNT(u)->from_proc_self_mountinfo = true;
|
MOUNT(u)->from_proc_self_mountinfo = true;
|
||||||
|
|
||||||
free(p->what);
|
free_and_replace(p->what, w);
|
||||||
p->what = w;
|
free_and_replace(p->options, o);
|
||||||
w = NULL;
|
free_and_replace(p->fstype, f);
|
||||||
|
|
||||||
free(p->options);
|
|
||||||
p->options = o;
|
|
||||||
o = NULL;
|
|
||||||
|
|
||||||
free(p->fstype);
|
|
||||||
p->fstype = f;
|
|
||||||
f = NULL;
|
|
||||||
|
|
||||||
if (load_extras) {
|
if (load_extras) {
|
||||||
r = mount_add_extras(MOUNT(u));
|
r = mount_add_extras(MOUNT(u));
|
||||||
|
|
|
@ -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)) {
|
if (!streq_ptr(s->status_text, t)) {
|
||||||
|
|
||||||
free(s->status_text);
|
free_and_replace(s->status_text, t);
|
||||||
s->status_text = t;
|
|
||||||
t = NULL;
|
|
||||||
|
|
||||||
notify_dbus = true;
|
notify_dbus = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -527,9 +527,7 @@ static int perform_upload(Uploader *u) {
|
||||||
log_debug("Upload finished successfully with code %ld: %s",
|
log_debug("Upload finished successfully with code %ld: %s",
|
||||||
status, strna(u->answer));
|
status, strna(u->answer));
|
||||||
|
|
||||||
free(u->last_cursor);
|
free_and_replace(u->last_cursor, u->current_cursor);
|
||||||
u->last_cursor = u->current_cursor;
|
|
||||||
u->current_cursor = NULL;
|
|
||||||
|
|
||||||
return update_cursor_state(u);
|
return update_cursor_state(u);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1107,11 +1107,7 @@ static int config_parse_default_instance(
|
||||||
if (!unit_instance_is_valid(printed))
|
if (!unit_instance_is_valid(printed))
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
free(i->default_instance);
|
return free_and_replace(i->default_instance, printed);
|
||||||
i->default_instance = printed;
|
|
||||||
printed = NULL;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int unit_file_load(
|
static int unit_file_load(
|
||||||
|
@ -1357,9 +1353,7 @@ static int install_info_follow(
|
||||||
if (!streq(basename(i->symlink_target), i->name))
|
if (!streq(basename(i->symlink_target), i->name))
|
||||||
return -EXDEV;
|
return -EXDEV;
|
||||||
|
|
||||||
free(i->path);
|
free_and_replace(i->path, i->symlink_target);
|
||||||
i->path = i->symlink_target;
|
|
||||||
i->symlink_target = NULL;
|
|
||||||
i->type = _UNIT_FILE_TYPE_INVALID;
|
i->type = _UNIT_FILE_TYPE_INVALID;
|
||||||
|
|
||||||
return unit_file_load_or_readlink(c, i, i->path, root_dir, flags);
|
return unit_file_load_or_readlink(c, i, i->path, root_dir, flags);
|
||||||
|
|
Loading…
Reference in a new issue