summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-10-26 16:55:50 +1030
committerRusty Russell <rusty@rustcorp.com.au>2011-10-26 16:55:50 +1030
commit2578442d9f40350f51cca44eeef60768dbae2dc6 (patch)
treebbf929c596e543fd21e34b87469569d083893a07
parented1b25bbd01f79b5b333fd28b53455d2c91b831f (diff)
strset: set errno on strset_add failures.
-rw-r--r--ccan/strset/strset.c8
-rw-r--r--ccan/strset/strset.h4
-rw-r--r--ccan/strset/test/run.c6
3 files changed, 13 insertions, 5 deletions
diff --git a/ccan/strset/strset.c b/ccan/strset/strset.c
index 8088db47..7c64b0cd 100644
--- a/ccan/strset/strset.c
+++ b/ccan/strset/strset.c
@@ -19,6 +19,7 @@
#include <ccan/ilog/ilog.h>
#include <assert.h>
#include <stdlib.h>
+#include <errno.h>
struct node {
/* To differentiate us from strings. */
@@ -75,8 +76,10 @@ static bool set_string(struct strset *set,
/* Substitute magic empty node if this is the empty string */
if (unlikely(!member[0])) {
n->u.n = malloc(sizeof(*n->u.n));
- if (unlikely(!n->u.n))
+ if (unlikely(!n->u.n)) {
+ errno = ENOMEM;
return false;
+ }
n->u.n->nul_byte = '\0';
n->u.n->byte_num = (size_t)-1;
/* Attach the string to child[0] */
@@ -108,6 +111,7 @@ bool strset_set(struct strset *set, const char *member)
for (byte_num = 0; str[byte_num] == member[byte_num]; byte_num++) {
if (member[byte_num] == '\0') {
/* All identical! */
+ errno = EEXIST;
return false;
}
}
@@ -122,7 +126,7 @@ bool strset_set(struct strset *set, const char *member)
/* Allocate new node. */
newn = malloc(sizeof(*newn));
if (!newn) {
- /* FIXME */
+ errno = ENOMEM;
return false;
}
newn->nul_byte = '\0';
diff --git a/ccan/strset/strset.h b/ccan/strset/strset.h
index 8d0d307e..b8116c35 100644
--- a/ccan/strset/strset.h
+++ b/ccan/strset/strset.h
@@ -65,8 +65,8 @@ char *strset_test(const struct strset *set, const char *member);
* @set: the set.
* @member: the string to place in the set.
*
- * This returns false if we run out of memory, or (more normally) if that
- * string already appears in the set.
+ * This returns false if we run out of memory (errno = ENOMEM), or
+ * (more normally) if that string already appears in the set (EEXIST).
*
* Note that the pointer is placed in the set, the string is not copied. If
* you want a copy in the set, use strdup().
diff --git a/ccan/strset/test/run.c b/ccan/strset/test/run.c
index 557c7080..cfda0ee3 100644
--- a/ccan/strset/test/run.c
+++ b/ccan/strset/test/run.c
@@ -10,7 +10,7 @@ int main(void)
char *dup = strdup(str);
/* This is how many tests you plan to run */
- plan_tests(24);
+ plan_tests(26);
strset_init(&set);
@@ -25,6 +25,10 @@ int main(void)
ok1(strset_test(&set, dup));
ok1(!strset_test(&set, none));
+ /* Add of duplicate should fail. */
+ ok1(!strset_set(&set, dup));
+ ok1(errno == EEXIST);
+
/* Delete should return original string. */
ok1(strset_clear(&set, dup) == str);
ok1(!strset_test(&set, str));