[PATCH] util: Fix memory leak in swupdate_remove_directory

12 views
Skip to first unread message

Shota Shimoyama

unread,
Sep 23, 2026, 9:31:37 PMSep 23
to swup...@googlegroups.com
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

Stefano Babic

unread,
Sep 28, 2026, 6:33:24 AM (11 days ago) Sep 28
to Shota Shimoyama, swup...@googlegroups.com
Reviewed-by: Stefano Babic <stefan...@swupdate.org>


--
_______________________________________________________________________
Nabla Software Engineering GmbH
Hirschstr. 111A | 86156 Augsburg | Tel: +49 821 45592596
Geschäftsführer : Stefano Babic | HRB 40522 Augsburg
E-Mail: sba...@nabladev.com

Reply all
Reply to author
Forward
0 new messages