summaryrefslogtreecommitdiff
path: root/src/basic/hashmap.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-04-29 09:55:28 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-05-06 16:56:42 +0200
commit25b3e2a8355e2f92767d042e85d7dbb1d54ad6d4 (patch)
tree3712c58a9f53870bb0b7aa18be31f43f7aae0ee4 /src/basic/hashmap.c
parentc73bb51364457f049082eb84447755e53892f542 (diff)
downloadsystemd-25b3e2a8355e2f92767d042e85d7dbb1d54ad6d4.tar.gz
basic/hashmap: allow NULL values in strdup hashmaps and add test
Diffstat (limited to 'src/basic/hashmap.c')
-rw-r--r--src/basic/hashmap.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c
index 7abe62fa93..efbe95bb9e 100644
--- a/src/basic/hashmap.c
+++ b/src/basic/hashmap.c
@@ -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) {