Commit 9b3f4ef6 authored by Rusty Russell's avatar Rusty Russell

tal: make tal_len/tal_count(NULL) return 0.

Previously it crashed, but if you're always dealing with tal arrays,
this is painful.
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent b137aba8
...@@ -654,6 +654,9 @@ size_t tal_len(const tal_t *ptr) ...@@ -654,6 +654,9 @@ size_t tal_len(const tal_t *ptr)
{ {
struct length *l; struct length *l;
if (!ptr)
return 0;
l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH); l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
if (!l) if (!l)
return 0; return 0;
......
...@@ -304,18 +304,18 @@ const char *tal_name(const tal_t *ptr); ...@@ -304,18 +304,18 @@ const char *tal_name(const tal_t *ptr);
/** /**
* tal_count - get the count of objects in a tal_arr. * 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 * Returns 0 if @ptr has no length property or is NULL, but be aware
* also a valid size! * that that is also a valid size!
*/ */
#define tal_count(p) (tal_len(p) / sizeof(*p)) #define tal_count(p) (tal_len(p) / sizeof(*p))
/** /**
* tal_len - get the count of bytes in a tal_arr. * 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! * also a valid size!
*/ */
size_t tal_len(const tal_t *ptr); size_t tal_len(const tal_t *ptr);
......
...@@ -43,7 +43,11 @@ int main(void) ...@@ -43,7 +43,11 @@ int main(void)
tal_set_backend(my_alloc, my_realloc, my_free, NULL); 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++) { for (i = 0; i < 3; i++) {
move = i; move = i;
......
...@@ -306,7 +306,7 @@ static char *get_one_ported(const void *ctx, const char *dir, ...@@ -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); char **ported = get_one_prop(ctx, dir, "ported", get_info);
/* No news is good news. */ /* No news is good news. */
if (!ported || tal_count(ported) == 0) if (tal_count(ported) == 0)
return NULL; return NULL;
if (tal_count(ported) != 1) if (tal_count(ported) != 1)
......
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