summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-04-22 23:12:15 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-04-24 00:25:04 -0400
commitef42202ac8ed27e7ff1fc90ef8bc2590046dff25 (patch)
treeb1646eeb8ef5070337ae701ffb4abf0e398640ad
parentc79bb9e4e2e5b96b2ae2c432bf8b0ff9674fce60 (diff)
Add set_consume which always takes ownership
Freeing in error path is the common pattern with set_put().
-rw-r--r--src/core/dbus-manager.c6
-rw-r--r--src/core/load-fragment.c6
-rw-r--r--src/core/manager.c6
-rw-r--r--src/core/mount-setup.c3
-rw-r--r--src/journal/coredumpctl.c12
-rw-r--r--src/libsystemd-bus/sd-bus.c10
-rw-r--r--src/locale/localectl.c22
-rw-r--r--src/readahead/readahead-collect.c5
-rw-r--r--src/shared/install.c6
-rw-r--r--src/shared/set.c7
-rw-r--r--src/shared/set.h1
-rw-r--r--src/systemctl/systemctl.c3
-rw-r--r--src/test/test-prioq.c2
-rw-r--r--src/tmpfiles/tmpfiles.c10
14 files changed, 40 insertions, 59 deletions
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
index d767dd5236..1f5a7d94fd 100644
--- a/src/core/dbus-manager.c
+++ b/src/core/dbus-manager.c
@@ -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)
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 0571d517b0..3d23372630 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -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;
- }
}
}
diff --git a/src/core/manager.c b/src/core/manager.c
index 208b240ba1..b1a22890fb 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -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);
diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c
index 89adb0b4a3..b841f478c3 100644
--- a/src/core/mount-setup.c
+++ b/src/core/mount-setup.c
@@ -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;
}
}
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 0bbfff2ecd..5652c2f91a 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -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;
}
diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c
index c7511c32d2..7d6d848ec5 100644
--- a/src/libsystemd-bus/sd-bus.c
+++ b/src/libsystemd-bus/sd-bus.c
@@ -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);
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
index ea9187ff84..50250c4b47 100644
--- a/src/locale/localectl.c
+++ b/src/locale/localectl.c
@@ -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;
}
diff --git a/src/readahead/readahead-collect.c b/src/readahead/readahead-collect.c
index 75ec5b70c7..ccd8408c1b 100644
--- a/src/readahead/readahead-collect.c
+++ b/src/readahead/readahead-collect.c
@@ -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);
}
}
diff --git a/src/shared/install.c b/src/shared/install.c
index 959de04810..b22019d7b5 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -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;
}
diff --git a/src/shared/set.c b/src/shared/set.c
index 5f83c50839..c338dc3a44 100644
--- a/src/shared/set.c
+++ b/src/shared/set.c
@@ -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);
}
diff --git a/src/shared/set.h b/src/shared/set.h
index 8864f7b34e..e5d46e9a8f 100644
--- a/src/shared/set.h
+++ b/src/shared/set.h
@@ -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);
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index c2b1749def..b94f7f7958 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -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;
}
diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c
index 73c640840a..aeac73973b 100644
--- a/src/test/test-prioq.c
+++ b/src/test/test-prioq.c
@@ -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);
}
}
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index 535381c470..f4885ec942 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -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;