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:
@@ -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) {
|
||||
|
||||
+2
-6
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user