Always good form to have cleanup functions preserve errno.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
#include <stddef.h>
#include <string.h>
#include <limits.h>
+#include <errno.h>
//#define TAL_DEBUG 1
void tal_free(const tal_t *ctx)
{
struct tal_hdr *t;
+ int saved_errno = errno;
if (!ctx)
return;
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)
*
* 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);
--- /dev/null
+#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();
+}