As mentioned in <http://austingroupbugs.net/view.php?id=385>, the next major POSIX release will require 'free' to not set errno. Rich Felker points out some scenarios when glibc 'free' might set errno; see <https://sourceware.org/ml/libc-alpha/2015-02/msg00067.html>. These scenarios should be investigated and fixed. Also, the glibc documentation should be updated to make it clear to users that glibc 'free' does not set errno, and that any replacements for 'free' should do the same.
*** Bug 22157 has been marked as a duplicate of this bug. ***
Created attachment 13064 [details] Test case for one of the scenarios Find attached a test case for one of the scenarios, namely when 1. free() invokes munmap() and 2. munmap()'s attempt to split a VMA fails due to /proc/sys/vm/max_map_count. It fails on glibc 2.3.2, 2.23, 2.28, 2.32, as well as on musl libc 1.1.24.
Created attachment 13065 [details] patch 'free' to preserve errno Thanks for the test case, Bruno. Proposed glibc patch attached. It includes your test case.
I believe there is also another code path that can lead to free() setting errno. Namely, when glibc is not configured with --disable-experimental-malloc, USE_TCACHE gets defined and enables this code path: __libc_free -> malloc.c:3147 -> MAYBE_INIT_TCACHE -> malloc.c:3033 -> tcache_init -> malloc.c:3007 -> _int_malloc -> malloc.c:3580 -> sysmalloc
Created attachment 13067 [details] patch 'free' to preserve errno (take 2) > I believe there is also another code path that can lead to free() setting errno. Thanks, I'm attaching a superseding patch that should cover that code path as well.
Created attachment 13071 [details] patch 'free' to preserve errno (take 3) Here is v3 of the patch, reflecting comments from Siddhesh Poyarekar <https://sourceware.org/pipermail/libc-alpha/2020-December/120892.html> and from Carlos O'Donell <https://sourceware.org/pipermail/libc-alpha/2020-December/120901.html>. I'll also email this to libc-alpha.
Created attachment 13072 [details] patch 'free' to preserve errno (take 4) This reflects comments from Florian Weimer <https://sourceware.org/pipermail/libc-alpha/2020-December/120911.html> by improving the test case. Also, it fixes the bug number in the commit message.
Created attachment 13073 [details] patch 'free' to preserve errno (take 5) This reflects Adhemerval Zanella's comments <https://sourceware.org/pipermail/libc-alpha/2020-December/121063.html> by improving the test case.
I installed the patch after further review by Adhemerval Zanella, which resulted in a couple of minor changes to the test case.