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 --- manual/memory.texi | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'manual/memory.texi') diff --git a/manual/memory.texi b/manual/memory.texi index c132261..b2cc652 100644 --- a/manual/memory.texi +++ b/manual/memory.texi @@ -738,6 +738,12 @@ later call to @code{malloc} to reuse the space. In the meantime, the space remains in your program as part of a free-list used internally by @code{malloc}. +The @code{free} function preserves the value of @code{errno}, so that +cleanup code need not worry about saving and restoring @code{errno} +around a call to @code{free}. Although neither @w{ISO C} nor +POSIX.1-2017 requires @code{free} to preserve @code{errno}, a future +version of POSIX is planned to require it. + There is no point in freeing blocks at the end of a program, because all of the program's space is given back to the system when the process terminates. @@ -1935,6 +1941,9 @@ linking against @code{libc.a} (explicitly or implicitly). functions (that is, all the functions used by the application, @theglibc{}, and other linked-in libraries) can lead to static linking failures, and, at run time, to heap corruption and application crashes. +Replacement functions should implement the behavior documented for +their counterparts in @theglibc{}; for example, the replacement +@code{free} should also preserve @code{errno}. The minimum set of functions which has to be provided by a custom @code{malloc} is given in the table below. -- cgit v1.1