swupdate_remove_directory() dynamically allocates dpath using asprintf().
However, if stat() fails, the target directory does not exist, or the path
is not a directory, the function returns early without executing free().
Fix this by routing all early exits through an `out` label to ensure
dpath is freed.
Reviewed-by: Dominique Martinet <
dominique...@atmark-techno.com>
Signed-off-by: Shota Shimoyama <
shota.s...@atmark-techno.com>
---
core/util.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/core/util.c b/core/util.c
index eb1fefbe..da4771c1 100644
--- a/core/util.c
+++ b/core/util.c
@@ -212,14 +212,18 @@ int swupdate_remove_directory(const char* path)
if (stat(dpath, &path_stat)) {
/* not exist, return ok */
- if (errno == ENOENT)
- return 0;
+ if (errno == ENOENT) {
+ ret = 0;
+ goto out;
+ }
ERROR("stat for path %s failed: %s", path, strerror(errno));
- return -errno;
+ ret = -errno;
+ goto out;
}
if (!S_ISDIR(path_stat.st_mode)) {
ERROR("Tried to remove %s dir, but it is not a dir", path);
- return -ENODEV;
+ ret = -ENODEV;
+ goto out;
}
ret = _is_mount_point(dpath, get_tmpdir());
if (ret < 0)
--
2.34.1