From 868abdea9811937039a1df84b15ad17f9b9c7a27 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 1 Jan 2016 17:55:37 +0100 Subject: extension: free will perserve errno MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/malloc.c | 4 ++++ src/slibc-alloc.c | 8 ++++++++ 2 files changed, 12 insertions(+) (limited to 'src') diff --git a/src/malloc.c b/src/malloc.c index 6d1f220..9b45607 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -254,6 +254,8 @@ void* realloc(void* ptr, size_t size) /** * Free a memory allocation. + * + * As a slibc extension, `errno` is guaranteed not to be set. * * @etymology (Free) allocated memory. * @@ -277,6 +279,8 @@ void free(void* ptr) * This function uses variadic arguments because there * there are multiple conflicting specifications for `cfree`. * + * As a slibc extension, `errno` is guaranteed not to be set. + * * @param ptr Pointer to the beginning of the memory allocation. * The process may crash if it does not point to the * beginning of a memory allocation on the heap. diff --git a/src/slibc-alloc.c b/src/slibc-alloc.c index 2f51603..f93a4b5 100644 --- a/src/slibc-alloc.c +++ b/src/slibc-alloc.c @@ -58,15 +58,19 @@ * This function is identical to `free`, except it is guaranteed not to * override the memory segment with zeroes before freeing the allocation. * + * `errno` is guaranteed not to be set. + * * @param segment The memory segment to free. * * @since Always. */ void fast_free(void* segment) { + int saved_errno = errno; if (segment == NULL) return; munmap(PURE_ALLOC(segment), PURE_SIZE(segment)); + errno = saved_errno; } @@ -74,16 +78,20 @@ void fast_free(void* segment) * This function is identical to `free`, except it is guaranteed to * override the memory segment with zeroes before freeing the allocation. * + * `errno` is guaranteed not to be set. + * * @param segment The memory segment to free. * * @since Always. */ void secure_free(void* segment) { + int saved_errno = errno; if (segment == NULL) return; explicit_bzero(PURE_ALLOC(segment), PURE_SIZE(segment)); fast_free(segment); + errno = saved_errno; } -- cgit v1.2.3-70-g09d2