Commit 1fe2fa08 authored by Rusty Russell's avatar Rusty Russell

noerr: add free_noerr().

Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 09378088
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "noerr.h" #include "noerr.h"
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h>
int close_noerr(int fd) int close_noerr(int fd)
{ {
...@@ -41,3 +42,10 @@ int unlink_noerr(const char *pathname) ...@@ -41,3 +42,10 @@ int unlink_noerr(const char *pathname)
errno = saved_errno; errno = saved_errno;
return ret; return ret;
} }
void free_noerr(void *p)
{
int saved_errno = errno;
free(p);
errno = saved_errno;
}
...@@ -30,4 +30,12 @@ int fclose_noerr(FILE *fp); ...@@ -30,4 +30,12 @@ int fclose_noerr(FILE *fp);
*/ */
int unlink_noerr(const char *pathname); 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 */ #endif /* NOERR_H */
...@@ -13,7 +13,7 @@ int main(int argc, char *argv[]) ...@@ -13,7 +13,7 @@ int main(int argc, char *argv[])
int fd; int fd;
FILE *fp; FILE *fp;
plan_tests(15); plan_tests(16);
/* Should fail to unlink. */ /* Should fail to unlink. */
ok1(unlink(name) != 0); ok1(unlink(name) != 0);
ok1(errno == ENOENT); ok1(errno == ENOENT);
...@@ -59,5 +59,9 @@ int main(int argc, char *argv[]) ...@@ -59,5 +59,9 @@ int main(int argc, char *argv[])
ok1(errno == 100); ok1(errno == 100);
unlink(name); unlink(name);
errno = 101;
free_noerr(malloc(7));
ok1(errno == 101);
return exit_status(); return exit_status();
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment