mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-08 21:17:47 +03:00
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;
|
||||
}
|
||||
|
||||
#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) {
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user