Commit 8bb0697b authored by Rusty Russell's avatar Rusty Russell

strmap: set errno to ENOENT even if we return NULL.

It seems redundant: strmap_get() and strmap_del() can only return NULL
when the string is not a member.  However, it became clear in writing
ccan/tsort that it's much more convenient for callers if we set errno
in this case too, so they can pass it up.
parent 13911ef7
...@@ -39,12 +39,13 @@ void *strmap_get_(const struct strmap *map, const char *member) ...@@ -39,12 +39,13 @@ void *strmap_get_(const struct strmap *map, const char *member)
{ {
struct strmap *n; struct strmap *n;
/* Empty map? */ /* Not empty map? */
if (!map->u.n) if (map->u.n) {
return NULL;
n = closest((struct strmap *)map, member); n = closest((struct strmap *)map, member);
if (streq(member, n->u.s)) if (streq(member, n->u.s))
return n->v; return n->v;
}
errno = ENOENT;
return NULL; return NULL;
} }
...@@ -129,8 +130,10 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep) ...@@ -129,8 +130,10 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep)
u8 direction = 0; /* prevent bogus gcc warning. */ u8 direction = 0; /* prevent bogus gcc warning. */
/* Empty map? */ /* Empty map? */
if (!map->u.n) if (!map->u.n) {
errno = ENOENT;
return NULL; return NULL;
}
/* Find closest, but keep track of parent. */ /* Find closest, but keep track of parent. */
n = map; n = map;
...@@ -148,8 +151,10 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep) ...@@ -148,8 +151,10 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep)
} }
/* Did we find it? */ /* Did we find it? */
if (!streq(member, n->u.s)) if (!streq(member, n->u.s)) {
errno = ENOENT;
return NULL; return NULL;
}
ret = n->u.s; ret = n->u.s;
if (valuep) if (valuep)
......
...@@ -77,7 +77,7 @@ static inline bool strmap_empty_(const struct strmap *map) ...@@ -77,7 +77,7 @@ static inline bool strmap_empty_(const struct strmap *map)
* @map: the typed strmap to search. * @map: the typed strmap to search.
* @member: the string to search for. * @member: the string to search for.
* *
* Returns the value, or NULL if it isn't in the map. * Returns the value, or NULL if it isn't in the map (and sets errno = ENOENT).
* *
* Example: * Example:
* int *val = strmap_get(&map, "hello"); * int *val = strmap_get(&map, "hello");
...@@ -118,7 +118,9 @@ bool strmap_add_(struct strmap *map, const char *member, const void *value); ...@@ -118,7 +118,9 @@ bool strmap_add_(struct strmap *map, const char *member, const void *value);
* @member: the string to remove from the map. * @member: the string to remove from the map.
* @valuep: the value (if non-NULL) * @valuep: the value (if non-NULL)
* *
* This returns the string which was passed to strmap_map(), or NULL. * This returns the string which was passed to strmap_map(), or NULL if
* it was not in the map (and sets errno = ENOENT).
*
* This means that if you allocated a string (eg. using strdup()), you * This means that if you allocated a string (eg. using strdup()), you
* can free it here. Similarly, the value is returned in @valuep if * can free it here. Similarly, the value is returned in @valuep if
* @valuep is not NULL. * @valuep is not NULL.
......
...@@ -14,20 +14,25 @@ int main(void) ...@@ -14,20 +14,25 @@ int main(void)
char *v; char *v;
/* This is how many tests you plan to run */ /* This is how many tests you plan to run */
plan_tests(32); plan_tests(42);
strmap_init(&map); strmap_init(&map);
ok1(!strmap_get(&map, str)); ok1(!strmap_get(&map, str));
ok1(errno == ENOENT);
ok1(!strmap_get(&map, none)); ok1(!strmap_get(&map, none));
ok1(errno == ENOENT);
ok1(!strmap_del(&map, str, NULL)); ok1(!strmap_del(&map, str, NULL));
ok1(errno == ENOENT);
ok1(!strmap_del(&map, none, NULL)); ok1(!strmap_del(&map, none, NULL));
ok1(errno == ENOENT);
ok1(strmap_add(&map, str, val)); ok1(strmap_add(&map, str, val));
ok1(strmap_get(&map, str) == val); ok1(strmap_get(&map, str) == val);
/* We compare the string, not the pointer. */ /* We compare the string, not the pointer. */
ok1(strmap_get(&map, dup) == val); ok1(strmap_get(&map, dup) == val);
ok1(!strmap_get(&map, none)); ok1(!strmap_get(&map, none));
ok1(errno == ENOENT);
/* Add a duplicate should fail. */ /* Add a duplicate should fail. */
ok1(!strmap_add(&map, dup, val)); ok1(!strmap_add(&map, dup, val));
...@@ -38,18 +43,23 @@ int main(void) ...@@ -38,18 +43,23 @@ int main(void)
ok1(strmap_del(&map, dup, &v) == str); ok1(strmap_del(&map, dup, &v) == str);
ok1(v == val); ok1(v == val);
ok1(!strmap_get(&map, str)); ok1(!strmap_get(&map, str));
ok1(errno == ENOENT);
ok1(!strmap_get(&map, none)); ok1(!strmap_get(&map, none));
ok1(errno == ENOENT);
/* Try insert and delete of empty string. */ /* Try insert and delete of empty string. */
ok1(strmap_add(&map, none, none)); ok1(strmap_add(&map, none, none));
ok1(strmap_get(&map, none) == none); ok1(strmap_get(&map, none) == none);
ok1(!strmap_get(&map, str)); ok1(!strmap_get(&map, str));
ok1(errno == ENOENT);
/* Delete should return original string. */ /* Delete should return original string. */
ok1(strmap_del(&map, "", &v) == none); ok1(strmap_del(&map, "", &v) == none);
ok1(v == none); ok1(v == none);
ok1(!strmap_get(&map, str)); ok1(!strmap_get(&map, str));
ok1(errno == ENOENT);
ok1(!strmap_get(&map, none)); ok1(!strmap_get(&map, none));
ok1(errno == ENOENT);
/* Both at once... */ /* Both at once... */
ok1(strmap_add(&map, none, none)); ok1(strmap_add(&map, none, none));
......
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