Add set_consume which always takes ownership

Freeing in error path is the common pattern with set_put().
parent c79bb9e4
......@@ -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)
......
......@@ -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;
}
}
}
......
......@@ -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);
......
......@@ -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;
}
}
......
......@@ -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;
}
......
......@@ -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);
......
......@@ -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;
}
......
......@@ -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);
}
}
......
......@@ -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;
}
......
......@@ -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);
}
......
......@@ -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);
......
......@@ -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;
}
......
......@@ -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);
}
}
......
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment