From 69fda43b8dd795c3658869633ca0708ed3134006 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 29 Dec 2020 00:45:49 -0800 Subject: free: preserve errno [BZ#17924] In the next release of POSIX, free must preserve errno . Modify __libc_free to save and restore errno, so that any internal munmap etc. syscalls do not disturb the caller's errno. Add a test malloc/tst-free-errno.c (almost all by Bruno Haible), and document that free preserves errno. Reviewed-by: Adhemerval Zanella --- malloc/malloc.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'malloc/malloc.c') diff --git a/malloc/malloc.c b/malloc/malloc.c index a3e914fa8a..3b151f44f7 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3278,6 +3278,8 @@ __libc_free (void *mem) *(volatile char *)mem; #endif + int err = errno; + p = mem2chunk (mem); /* Mark the chunk as belonging to the library again. */ @@ -3298,13 +3300,16 @@ __libc_free (void *mem) mp_.mmap_threshold, mp_.trim_threshold); } munmap_chunk (p); - return; } + else + { + MAYBE_INIT_TCACHE (); - MAYBE_INIT_TCACHE (); + ar_ptr = arena_for_chunk (p); + _int_free (ar_ptr, p, 0); + } - ar_ptr = arena_for_chunk (p); - _int_free (ar_ptr, p, 0); + __set_errno (err); } libc_hidden_def (__libc_free) -- cgit v1.2.3