diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-04-22 23:12:15 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-04-24 00:25:04 -0400 |
commit | ef42202ac8ed27e7ff1fc90ef8bc2590046dff25 (patch) | |
tree | b1646eeb8ef5070337ae701ffb4abf0e398640ad /src/shared | |
parent | c79bb9e4e2e5b96b2ae2c432bf8b0ff9674fce60 (diff) |
Add set_consume which always takes ownership
Freeing in error path is the common pattern with set_put().
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/install.c | 6 | ||||
-rw-r--r-- | src/shared/set.c | 7 | ||||
-rw-r--r-- | src/shared/set.h | 1 |
3 files changed, 10 insertions, 4 deletions
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); |