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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-06-05 14:06:02 +02:00
parent ab9dd0b997
commit d378ef708b
1 changed files with 3 additions and 9 deletions

View File

@ -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;
}
}