]> git.ozlabs.org Git - ccan/commitdiff
noerr: add free_noerr().
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 20 Nov 2015 06:22:34 +0000 (16:52 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 20 Nov 2015 06:35:47 +0000 (17:05 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/noerr/noerr.c
ccan/noerr/noerr.h
ccan/noerr/test/run.c

index 4368a5e7eb2349bb7268d1169a6cfb1b8cec6354..972f3074541b650c4cb471eccd362fa809262b3c 100644 (file)
@@ -2,6 +2,7 @@
 #include "noerr.h"
 #include <unistd.h>
 #include <errno.h>
+#include <stdlib.h>
 
 int close_noerr(int fd)
 {
@@ -41,3 +42,10 @@ int unlink_noerr(const char *pathname)
        errno = saved_errno;
        return ret;
 }
+
+void free_noerr(void *p)
+{
+       int saved_errno = errno;
+       free(p);
+       errno = saved_errno;
+}
index 99b0f69043ac458672597a7a3932a989528f491d..bafb59bd021d5ff76d3f6c215afb837d8a2783a5 100644 (file)
@@ -30,4 +30,12 @@ int fclose_noerr(FILE *fp);
  */
 int unlink_noerr(const char *pathname);
 
+/**
+ * free_noerr - free memory without stomping errno.
+ * @p: the pointer to free.
+ *
+ * errno is saved and restored across the call to free: the standard leaves
+ * that undefined.
+ */
+void free_noerr(void *p);
 #endif /* NOERR_H */
index d5d48584952abfcd82401a7bd45e16ca9fc0af22..48086a24d542e98dccaa33548748850f5d464b83 100644 (file)
@@ -13,7 +13,7 @@ int main(int argc, char *argv[])
        int fd;
        FILE *fp;
 
-       plan_tests(15);
+       plan_tests(16);
        /* Should fail to unlink. */
        ok1(unlink(name) != 0);
        ok1(errno == ENOENT);
@@ -59,5 +59,9 @@ int main(int argc, char *argv[])
        ok1(errno == 100);
        unlink(name);
 
+       errno = 101;
+       free_noerr(malloc(7));
+       ok1(errno == 101);
+
        return exit_status();
 }