]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
portable: rework set handling in portable_detach()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 5 Jun 2020 12:06:02 +0000 (14:06 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 24 Jun 2020 08:38:15 +0000 (10:38 +0200)
_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

index 57c224decd08d49ae64f3e68a068923d1bcd19e1..3dc6d9e4220b3844290c36019c9170e58acc7fd1 100644 (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;
                 }
         }