Add set_consume which always takes ownership
Freeing in error path is the common pattern with set_put().
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user