]> git.ozlabs.org Git - ccan/blobdiff - ccan/tal/tal.c
tal: bounds checking fixes.
[ccan] / ccan / tal / tal.c
index e575130568ca26913ad97ed00fa2feb6f4cf7740..32cfd0433a395982e4158851bd10c7d73b5c8682 100644 (file)
@@ -294,6 +294,7 @@ static struct children *add_child_property(struct tal_hdr *parent,
 
                init_group_property(&prop->group, prop, child);
                list_head_init(&prop->group.list);
+               update_bounds(&prop->group);
        }
        return prop;
 }
@@ -417,7 +418,7 @@ static void del_tree(struct tal_hdr *t)
         freefn(t);
 }
 
-void *tal_alloc_(const tal_t *ctx, size_t size, bool clear)
+void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
 {
         struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
 
@@ -426,7 +427,7 @@ void *tal_alloc_(const tal_t *ctx, size_t size, bool clear)
                return NULL;
        if (clear)
                memset(from_tal_hdr(child), 0, size);
-        child->prop = NULL;
+        child->prop = (void *)label;
         if (!add_child(parent, child)) {
                freefn(child);
                return NULL;
@@ -706,7 +707,12 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
 
 void *tal_memdup(const tal_t *ctx, const void *p, size_t n)
 {
-       void *ret = tal_arr(ctx, char, n);
+       void *ret;
+
+       if (ctx == TAL_TAKE)
+               return (void *)p;
+
+       ret = tal_arr(ctx, char, n);
        if (ret)
                memcpy(ret, p, n);
        return ret;
@@ -727,9 +733,14 @@ char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
 {
        size_t max = strlen(fmt) * 2;
-       char *buf = tal_arr(ctx, char, max);
+       char *buf;
        int ret;
 
+       if (ctx == TAL_TAKE)
+               buf = tal_arr(tal_parent(fmt), char, max);
+       else
+               buf = tal_arr(ctx, char, max);
+
        while (buf) {
                va_list ap2;
 
@@ -741,6 +752,8 @@ char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
                        break;
                buf = tal_resize(buf, max *= 2);
        }
+       if (ctx == TAL_TAKE)
+               tal_free(fmt);
        return buf;
 }
 
@@ -875,7 +888,8 @@ static bool check_node(struct group *group,
                        name = (struct name *)p;
                        break;
                }
-               if (p != &null_parent.c.hdr && !in_bounds(p))
+               if (p != &null_parent.c.hdr && p != &null_parent.c.group.hdr
+                   && !in_bounds(p))
                        return check_err(t, errorstr,
                                         "has bad property pointer");