]> git.ozlabs.org Git - ccan/commitdiff
tal: make tal_len/tal_count(NULL) return 0.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 24 Jan 2017 06:22:00 +0000 (16:52 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 24 Jan 2017 06:22:00 +0000 (16:52 +1030)
Previously it crashed, but if you're always dealing with tal arrays,
this is painful.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/tal/tal.c
ccan/tal/tal.h
ccan/tal/test/run-count.c
tools/depends.c

index 2a2eca77c04f5bbd37cc35e4ee4c4507e17ec33a..555dd1333d8f7be28ae837b4a4f0d1cba5686d07 100644 (file)
@@ -654,6 +654,9 @@ size_t tal_len(const tal_t *ptr)
 {
        struct length *l;
 
+       if (!ptr)
+               return 0;
+
        l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
        if (!l)
                return 0;
index 200c2b161fc60916de8fb8954b27c293d2c0e84e..e525a01d1193144168d52c87ab901a1d9cbc655e 100644 (file)
@@ -304,18 +304,18 @@ const char *tal_name(const tal_t *ptr);
 
 /**
  * tal_count - get the count of objects in a tal_arr.
- * @ptr: The tal allocated object array.
+ * @ptr: The tal allocated object array (or NULL)
  *
- * Returns 0 if @ptr has no length property, but be aware that that is
- * also a valid size!
+ * Returns 0 if @ptr has no length property or is NULL, but be aware
+ * that that is also a valid size!
  */
 #define tal_count(p) (tal_len(p) / sizeof(*p))
 
 /**
  * tal_len - get the count of bytes in a tal_arr.
- * @ptr: The tal allocated object array.
+ * @ptr: The tal allocated object array (or NULL)
  *
- * Returns 0 if @ptr has no length property, but be aware that that is
+ * Returns 0 if @ptr has no length property or NULL, but be aware that that is
  * also a valid size!
  */
 size_t tal_len(const tal_t *ptr);
index 6a4eb4ab8ada57a9aef4d772fedf61160e6662a4..33049b9f78d3e2edc9e675ed2afc7450d9b9597d 100644 (file)
@@ -43,7 +43,11 @@ int main(void)
 
        tal_set_backend(my_alloc, my_realloc, my_free, NULL);
 
-       plan_tests(19 * 3);
+       plan_tests(2 + 19 * 3);
+
+       p1 = NULL;
+       ok1(tal_len(p1) == 0);
+       ok1(tal_count(p1) == 0);
 
        for (i = 0; i < 3; i++) {
                move = i;
index 95bda1b50a7b33088f41345a7369fa5e982ffa21..e6a79d71134404e5046e714367224c9e5ed543d1 100644 (file)
@@ -306,7 +306,7 @@ static char *get_one_ported(const void *ctx, const char *dir,
        char **ported = get_one_prop(ctx, dir, "ported", get_info);
 
        /* No news is good news. */
-       if (!ported || tal_count(ported) == 0)
+       if (tal_count(ported) == 0)
                return NULL;
 
        if (tal_count(ported) != 1)