]> git.ozlabs.org Git - ccan/commitdiff
strset: set errno on strset_add failures.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 26 Oct 2011 06:25:50 +0000 (16:55 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 26 Oct 2011 06:25:50 +0000 (16:55 +1030)
ccan/strset/strset.c
ccan/strset/strset.h
ccan/strset/test/run.c

index 8088db47e808e3adb40f5bf583a6a8e6babeec59..7c64b0cd016631017024da239cc29ad7e5470ec0 100644 (file)
@@ -19,6 +19,7 @@
 #include <ccan/ilog/ilog.h>
 #include <assert.h>
 #include <stdlib.h>
 #include <ccan/ilog/ilog.h>
 #include <assert.h>
 #include <stdlib.h>
+#include <errno.h>
 
 struct node {
        /* To differentiate us from strings. */
 
 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));
        /* 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;
                        return false;
+               }
                n->u.n->nul_byte = '\0';
                n->u.n->byte_num = (size_t)-1;
                /* Attach the string to child[0] */
                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! */
        for (byte_num = 0; str[byte_num] == member[byte_num]; byte_num++) {
                if (member[byte_num] == '\0') {
                        /* All identical! */
+                       errno = EEXIST;
                        return false;
                }
        }
                        return false;
                }
        }
@@ -122,7 +126,7 @@ bool strset_set(struct strset *set, const char *member)
        /* Allocate new node. */
        newn = malloc(sizeof(*newn));
        if (!newn) {
        /* Allocate new node. */
        newn = malloc(sizeof(*newn));
        if (!newn) {
-               /* FIXME */
+               errno = ENOMEM;
                return false;
        }
        newn->nul_byte = '\0';
                return false;
        }
        newn->nul_byte = '\0';
index 8d0d307e34c483d2b21351defe68d0ba0baab3c1..b8116c35c471813e5330d10b930aaaf82b26e69b 100644 (file)
@@ -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.
  *
  * @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().
  *
  * Note that the pointer is placed in the set, the string is not copied.  If
  * you want a copy in the set, use strdup().
index 557c7080aaba6af5939c97b53df4435db5df9e97..cfda0ee38f0ce998a16c5779fc793d8bf0722565 100644 (file)
@@ -10,7 +10,7 @@ int main(void)
        char *dup = strdup(str);
 
        /* This is how many tests you plan to run */
        char *dup = strdup(str);
 
        /* This is how many tests you plan to run */
-       plan_tests(24);
+       plan_tests(26);
 
        strset_init(&set);
 
 
        strset_init(&set);
 
@@ -25,6 +25,10 @@ int main(void)
        ok1(strset_test(&set, dup));
        ok1(!strset_test(&set, none));
 
        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));
        /* Delete should return original string. */
        ok1(strset_clear(&set, dup) == str);
        ok1(!strset_test(&set, str));