basic/hashmap: allow NULL values in strdup hashmaps and add test

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2020-05-06 16:56:42 +02:00
parent c73bb51364
commit 25b3e2a835
2 changed files with 69 additions and 6 deletions
+14 -6
View File
@@ -1775,22 +1775,30 @@ int hashmap_put_strdup(Hashmap **h, const char *k, const char *v) {
return r;
_cleanup_free_ char *kdup = NULL, *vdup = NULL;
kdup = strdup(k);
vdup = strdup(v);
if (!kdup || !vdup)
if (!kdup)
return -ENOMEM;
if (v) {
vdup = strdup(v);
if (!vdup)
return -ENOMEM;
}
r = hashmap_put(*h, kdup, vdup);
if (r < 0) {
if (r == -EEXIST && streq(v, hashmap_get(*h, kdup)))
if (r == -EEXIST && streq_ptr(v, hashmap_get(*h, kdup)))
return 0;
return r;
}
assert(r > 0); /* 0 would mean vdup is already in the hashmap, which cannot be */
kdup = vdup = NULL;
/* 0 with non-null vdup would mean vdup is already in the hashmap, which cannot be */
assert(vdup == NULL || r > 0);
if (r > 0)
kdup = vdup = NULL;
return 0;
return r;
}
int set_put_strdup(Set **s, const char *p) {