]> git.ozlabs.org Git - ccan/commitdiff
tal: make sure tal_free() preserves errno.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 19 Nov 2012 02:22:41 +0000 (12:52 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 19 Nov 2012 02:22:41 +0000 (12:52 +1030)
Always good form to have cleanup functions preserve errno.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/tal/tal.c
ccan/tal/tal.h
ccan/tal/test/run-free.c [new file with mode: 0644]

index 474c0ff045c72f3f03c426851dc3e47ed365e4ba..f33a069a4bf49af63932350aa236b513cfc912e4 100644 (file)
@@ -9,6 +9,7 @@
 #include <stddef.h>
 #include <string.h>
 #include <limits.h>
+#include <errno.h>
 
 //#define TAL_DEBUG 1
 
@@ -482,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;
@@ -489,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)
index da448d8f22ad8813520299546a51ce09c0a00705..a12e0dc476ba6acb773e2e65d0b2de68a1c8f234 100644 (file)
@@ -58,6 +58,8 @@ typedef void tal_t;
  *
  * This calls the destructors for p (if any), then does the same for all its
  * children (recursively) before finally freeing the memory.
+ *
+ * Note: errno is preserved by this call.
  */
 void tal_free(const tal_t *p);
 
diff --git a/ccan/tal/test/run-free.c b/ccan/tal/test/run-free.c
new file mode 100644 (file)
index 0000000..7b9a086
--- /dev/null
@@ -0,0 +1,25 @@
+#include <ccan/tal/tal.h>
+#include <ccan/tal/tal.c>
+#include <ccan/tap/tap.h>
+
+static void destroy_errno(char *p)
+{
+       errno = ENOENT;
+}
+
+int main(void)
+{
+       char *p;
+
+       plan_tests(2);
+
+       p = tal(NULL, char);
+       ok1(tal_add_destructor(p, destroy_errno));
+
+       /* Errno save/restored across free. */
+       errno = EINVAL;
+       tal_free(p);
+       ok1(errno == EINVAL);
+
+       return exit_status();
+}