chiark / gitweb /
Add set_consume which always takes ownership
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 23 Apr 2013 03:12:15 +0000 (23:12 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 24 Apr 2013 04:25:04 +0000 (00:25 -0400)
Freeing in error path is the common pattern with set_put().

14 files changed:
src/core/dbus-manager.c
src/core/load-fragment.c
src/core/manager.c
src/core/mount-setup.c
src/journal/coredumpctl.c
src/libsystemd-bus/sd-bus.c
src/locale/localectl.c
src/readahead/readahead-collect.c
src/shared/install.c
src/shared/set.c
src/shared/set.h
src/systemctl/systemctl.c
src/test/test-prioq.c
src/tmpfiles/tmpfiles.c

index d767dd5..1f5a7d9 100644 (file)
@@ -1186,11 +1186,9 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
                 if (!client)
                         goto oom;
 
-                r = set_put(s, client);
-                if (r < 0) {
-                        free(client);
+                r = set_consume(s, client);
+                if (r < 0)
                         return bus_send_error_reply(connection, message, NULL, r);
-                }
 
                 reply = dbus_message_new_method_return(message);
                 if (!reply)
index 0571d51..3d23372 100644 (file)
@@ -2095,11 +2095,9 @@ static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
                                 if (!id)
                                         return -ENOMEM;
 
-                                r = set_put(names, id);
-                                if (r < 0) {
-                                        free(id);
+                                r = set_consume(names, id);
+                                if (r < 0)
                                         return r;
-                                }
                         }
                 }
 
index 208b240..b1a2289 100644 (file)
@@ -806,11 +806,9 @@ static void manager_build_unit_path_cache(Manager *m) {
                                 goto fail;
                         }
 
-                        r = set_put(m->unit_path_cache, p);
-                        if (r < 0) {
-                                free(p);
+                        r = set_consume(m->unit_path_cache, p);
+                        if (r < 0)
                                 goto fail;
-                        }
                 }
 
                 closedir(d);
index 89adb0b..b841f47 100644 (file)
@@ -251,10 +251,9 @@ int mount_cgroup_controllers(char ***join_controllers) {
                         continue;
                 }
 
-                r = set_put(controllers, controller);
+                r = set_consume(controllers, controller);
                 if (r < 0) {
                         log_error("Failed to add controller to set.");
-                        free(controller);
                         goto finish;
                 }
         }
index 0bbfff2..5652c2f 100644 (file)
@@ -68,10 +68,9 @@ static Set *new_matches(void) {
                 return NULL;
         }
 
-        r = set_put(set, tmp);
+        r = set_consume(set, tmp);
         if (r < 0) {
                 log_error("failed to add to set: %s", strerror(-r));
-                free(tmp);
                 set_free(set);
                 return NULL;
         }
@@ -125,18 +124,17 @@ static int add_match(Set *set, const char *match) {
         if (!pattern)
                 goto fail;
 
-        r = set_put(set, pattern);
+        log_debug("Adding pattern: %s", pattern);
+        r = set_consume(set, pattern);
         if (r < 0) {
-                log_error("failed to add pattern '%s': %s",
+                log_error("Failed to add pattern '%s': %s",
                           pattern, strerror(-r));
                 goto fail;
         }
-        log_debug("Added pattern: %s", pattern);
 
         return 0;
 fail:
-        free(pattern);
-        log_error("failed to add match: %s", strerror(-r));
+        log_error("Failed to add match: %s", strerror(-r));
         return r;
 }
 
index c7511c3..7d6d848 100644 (file)
@@ -1841,13 +1841,9 @@ static int process_introspect(sd_bus *bus, sd_bus_message *m) {
                 if (p)
                         *p = 0;
 
-                r = set_put(s, a);
-                if (r < 0) {
-                        free(a);
-
-                        if (r != -EEXIST)
-                                return r;
-                }
+                r = set_consume(s, a);
+                if (r < 0 && r != -EEXIST)
+                        return r;
         }
 
         f = open_memstream(&introspection, &size);
index ea9187f..50250c4 100644 (file)
@@ -369,9 +369,8 @@ static int add_locales_from_archive(Set *locales) {
                         goto finish;
                 }
 
-                r = set_put(locales, z);
+                r = set_consume(locales, z);
                 if (r < 0) {
-                        free(z);
                         log_error("Failed to add locale: %s", strerror(-r));
                         goto finish;
                 }
@@ -411,14 +410,10 @@ static int add_locales_from_libdir (Set *locales) {
                 if (!z)
                         return log_oom();
 
-                r = set_put(locales, z);
-                if (r < 0) {
-                        free(z);
-
-                        if (r != -EEXIST) {
-                                log_error("Failed to add locale: %s", strerror(-r));
-                                return r;
-                        }
+                r = set_consume(locales, z);
+                if (r < 0 && r != -EEXIST) {
+                        log_error("Failed to add locale: %s", strerror(-r));
+                        return r;
                 }
 
                 errno = 0;
@@ -526,12 +521,9 @@ static int nftw_cb(
         if (e)
                 *e = 0;
 
-        r = set_put(keymaps, p);
-        if (r == -EEXIST)
-                free(p);
-        else if (r < 0) {
+        r = set_consume(keymaps, p);
+        if (r < 0 && r != -EEXIST) {
                 log_error("Can't add keymap: %s", strerror(-r));
-                free(p);
                 return r;
         }
 
index 75ec5b7..ccd8408 100644 (file)
@@ -479,8 +479,9 @@ static int collect(const char *root) {
                                         }
                                         entry->bin = (entrytime - starttime) / 2000000;
 
-                                        if ((k = hashmap_put(files, p, entry)) < 0) {
-                                                log_warning("set_put() failed: %s", strerror(-k));
+                                        k = hashmap_put(files, p, entry);
+                                        if (k < 0) {
+                                                log_warning("hashmap_put() failed: %s", strerror(-k));
                                                 free(p);
                                         }
                                 }
index 959de04..b22019d 100644 (file)
@@ -178,11 +178,9 @@ static int mark_symlink_for_removal(
 
         path_kill_slashes(n);
 
-        r = set_put(*remove_symlinks_to, n);
-        if (r < 0) {
-                free(n);
+        r = set_consume(*remove_symlinks_to, n);
+        if (r < 0)
                 return r == -EEXIST ? 0 : r;
-        }
 
         return 0;
 }
index 5f83c50..c338dc3 100644 (file)
@@ -49,6 +49,13 @@ int set_put(Set *s, void *value) {
         return hashmap_put(MAKE_HASHMAP(s), value, value);
 }
 
+int set_consume(Set *s, void *value) {
+        int r = set_put(s, value);
+        if (r < 0)
+                free(value);
+        return r;
+}
+
 int set_replace(Set *s, void *value) {
         return hashmap_replace(MAKE_HASHMAP(s), value, value);
 }
index 8864f7b..e5d46e9 100644 (file)
@@ -46,6 +46,7 @@ Set* set_copy(Set *s);
 int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func);
 
 int set_put(Set *s, void *value);
+int set_consume(Set *s, void *value);
 int set_replace(Set *s, void *value);
 void *set_get(Set *s, void *value);
 bool set_contains(Set *s, void *value);
index c2b1749..b94f7f7 100644 (file)
@@ -1843,9 +1843,8 @@ static int start_unit_one(
                 if (!p)
                         return log_oom();
 
-                r = set_put(s, p);
+                r = set_consume(s, p);
                 if (r < 0) {
-                        free(p);
                         log_error("Failed to add path to set.");
                         return r;
                 }
index 73c6408..aeac739 100644 (file)
@@ -119,7 +119,7 @@ static void test_struct(void) {
                 assert_se(r >= 0);
 
                 if (i % 4 == 0) {
-                        r = set_put(s, t);
+                        r = set_consume(s, t);
                         assert_se(r >= 0);
                 }
         }
index 535381c..f4885ec 100644 (file)
@@ -186,13 +186,9 @@ static void load_unix_sockets(void) {
 
                 path_kill_slashes(s);
 
-                k = set_put(unix_sockets, s);
-                if (k < 0) {
-                        free(s);
-
-                        if (k != -EEXIST)
-                                goto fail;
-                }
+                k = set_consume(unix_sockets, s);
+                if (k < 0 && k != -EEXIST)
+                        goto fail;
         }
 
         return;