From e88ad64d46db476682df723834daac669c5f0f63 Mon Sep 17 00:00:00 2001 From: Duncan Overbruck Date: Sun, 7 Dec 2025 01:38:21 +0100 Subject: [PATCH 1/3] bin/xbps-remove: fix memory leak in --clean-cache --- bin/xbps-remove/clean-cache.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/xbps-remove/clean-cache.c b/bin/xbps-remove/clean-cache.c index 4027a91d2..83f9afdaa 100644 --- a/bin/xbps-remove/clean-cache.c +++ b/bin/xbps-remove/clean-cache.c @@ -179,7 +179,8 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun) .uninstalled = uninstalled, }; rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&data); - xbps_object_release(array); } + + xbps_object_release(array); return rv; } From 8cb883d591f5873d9f54a922eb223200eba36576 Mon Sep 17 00:00:00 2001 From: Duncan Overbruck Date: Sun, 7 Dec 2025 01:39:00 +0100 Subject: [PATCH 2/3] bin/xbps-remove: improve error handling and error messages --- bin/xbps-remove/clean-cache.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/bin/xbps-remove/clean-cache.c b/bin/xbps-remove/clean-cache.c index 83f9afdaa..b64182c35 100644 --- a/bin/xbps-remove/clean-cache.c +++ b/bin/xbps-remove/clean-cache.c @@ -143,20 +143,34 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun) DIR *dirp; struct dirent *dp; char *ext; - int rv = 0; + int r; // XXX: there is no public api to load the pkgdb so force it before // its done potentially concurrently by threads through the // xbps_array_foreach_cb_multi call later. (void)xbps_pkgdb_get_pkg(xhp, "foo"); - if (chdir(xhp->cachedir) == -1) - return -1; + if (chdir(xhp->cachedir) == -1) { + if (errno == ENOENT) + return 0; + r = -errno; + xbps_error_printf("failed to change to cache directory: %s: %s\n", + xhp->cachedir, strerror(-r)); + return r; + } - if ((dirp = opendir(xhp->cachedir)) == NULL) - return 0; + dirp = opendir("."); + if (!dirp) { + r = -errno; + xbps_error_printf("failed to open cache directory: %s: %s\n", + xhp->cachedir, strerror(-r)); + return r; + } array = xbps_array_create(); + if (!array) + return xbps_error_oom(); + while ((dp = readdir(dirp)) != NULL) { if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0)) @@ -169,7 +183,10 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun) xbps_dbg_printf("ignoring unknown file: %s\n", dp->d_name); continue; } - xbps_array_add_cstring(array, dp->d_name); + if (!xbps_array_add_cstring(array, dp->d_name)) { + xbps_object_release(array); + return xbps_error_oom(); + } } (void)closedir(dirp); @@ -178,9 +195,11 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun) .dry = drun, .uninstalled = uninstalled, }; - rv = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&data); + r = xbps_array_foreach_cb_multi(xhp, array, NULL, cleaner_cb, (void*)&data); + } else { + r = 0; } xbps_object_release(array); - return rv; + return r; } From f9cf68201c651135ee4ce5c51524a20b7cf3a86f Mon Sep 17 00:00:00 2001 From: Duncan Overbruck Date: Sun, 7 Dec 2025 01:42:36 +0100 Subject: [PATCH 3/3] bin/xbps-remove: work around multi threading issue in --clean-cache Fixes: #660 --- bin/xbps-remove/clean-cache.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bin/xbps-remove/clean-cache.c b/bin/xbps-remove/clean-cache.c index b64182c35..a768f7c28 100644 --- a/bin/xbps-remove/clean-cache.c +++ b/bin/xbps-remove/clean-cache.c @@ -148,7 +148,12 @@ clean_cachedir(struct xbps_handle *xhp, bool uninstalled, bool drun) // XXX: there is no public api to load the pkgdb so force it before // its done potentially concurrently by threads through the // xbps_array_foreach_cb_multi call later. - (void)xbps_pkgdb_get_pkg(xhp, "foo"); + // XXX: same for the repository pool... + if (uninstalled) { + (void)xbps_pkgdb_get_pkg(xhp, "foo"); + } else { + (void)xbps_rpool_get_pkg(xhp, "package-that-wont-exist-so-it-loads-all-repos"); + } if (chdir(xhp->cachedir) == -1) { if (errno == ENOENT)