diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2020-12-29 00:45:49 -0800 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2020-12-29 00:46:46 -0800 |
commit | 69fda43b8dd795c3658869633ca0708ed3134006 (patch) | |
tree | e86b216293d450bfbecaaf8230c3247444b668c4 /malloc/malloc.c | |
parent | 016c64236dee6e28f09c10ba38f274aad7205f95 (diff) | |
download | glibc-69fda43b8dd795c3658869633ca0708ed3134006.zip glibc-69fda43b8dd795c3658869633ca0708ed3134006.tar.gz glibc-69fda43b8dd795c3658869633ca0708ed3134006.tar.bz2 |
free: preserve errno [BZ#17924]
In the next release of POSIX, free must preserve errno
<https://www.austingroupbugs.net/view.php?id=385>.
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 <adhemerval.zanella@linaro.org>
Diffstat (limited to 'malloc/malloc.c')
-rw-r--r-- | malloc/malloc.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/malloc/malloc.c b/malloc/malloc.c index a3e914f..3b151f4 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) |