]> git.ozlabs.org Git - ccan/blobdiff - ccan/tal/tal.c
tal: make sure tal_free() preserves errno.
[ccan] / ccan / tal / tal.c
index ff25db19afb8a3b16da50ef254687a85b50cc71e..f33a069a4bf49af63932350aa236b513cfc912e4 100644 (file)
@@ -9,6 +9,7 @@
 #include <stddef.h>
 #include <string.h>
 #include <limits.h>
+#include <errno.h>
 
 //#define TAL_DEBUG 1
 
@@ -455,6 +456,7 @@ static struct tal_hdr *remove_node(struct tal_hdr *t)
 
                /* Are we the only one? */
                if (prev == t) {
+                       struct prop_hdr *next = (*prop)->next;
                        struct children *c = group->parent_child;
                        /* Is this the group embedded in the child property? */
                        if (group == &c->group) {
@@ -462,9 +464,9 @@ static struct tal_hdr *remove_node(struct tal_hdr *t)
                        } else {
                                /* Empty group, so free it. */
                                list_del_from(&c->group.list, &group->list.n);
-                               *prop = group->hdr.next;
                                freefn(group);
                        }
+                       *prop = next;
                        return c->parent;
                } else {
                        /* Move property to next node. */
@@ -481,6 +483,7 @@ static struct tal_hdr *remove_node(struct tal_hdr *t)
 void tal_free(const tal_t *ctx)
 {
         struct tal_hdr *t;
+       int saved_errno = errno;
 
         if (!ctx)
                 return;
@@ -488,6 +491,7 @@ void tal_free(const tal_t *ctx)
         t = debug_tal(to_tal_hdr(ctx));
         remove_node(t);
         del_tree(t);
+       errno = saved_errno;
 }
 
 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)