From d378ef708b123b5da9ce2a52ea9e8a838f63bddb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 5 Jun 2020 14:06:02 +0200 Subject: [PATCH] portable: rework set handling in portable_detach() _cleanup_set_free_ is enough for unit_files, because unit_files is allocated in set_put_strdup(), which uses string_hash_ops_free. This fixes a leak if marker was already present in the table. --- src/portable/portable.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/portable/portable.c b/src/portable/portable.c index 57c224decd0..3dc6d9e4220 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -1125,7 +1125,7 @@ int portable_detach( sd_bus_error *error) { _cleanup_(lookup_paths_free) LookupPaths paths = {}; - _cleanup_set_free_free_ Set *unit_files = NULL, *markers = NULL; + _cleanup_set_free_ Set *unit_files = NULL, *markers = NULL; _cleanup_closedir_ DIR *d = NULL; const char *where, *item; Iterator iterator; @@ -1149,10 +1149,6 @@ int portable_detach( return log_debug_errno(errno, "Failed to open '%s' directory: %m", where); } - markers = set_new(&path_hash_ops); - if (!markers) - return -ENOMEM; - FOREACH_DIRENT(de, d, return log_debug_errno(errno, "Failed to enumerate '%s' directory: %m", where)) { _cleanup_free_ char *marker = NULL; UnitFileState state; @@ -1193,10 +1189,8 @@ int portable_detach( if (path_is_absolute(marker) && !image_in_search_path(IMAGE_PORTABLE, marker)) { - r = set_ensure_put(&markers, &path_hash_ops, marker); - if (r >= 0) - marker = NULL; - else if (r != -EEXIST) + r = set_ensure_consume(&markers, &path_hash_ops_free, TAKE_PTR(marker)); + if (r < 0) return r; } } -- 2.47.3