From: Bruno Haible Date: Sun, 3 Jan 2021 10:19:22 +0000 (+0100) Subject: free-posix: Work around GCC mis-optimization bug. X-Git-Tag: v1.0~3227 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=5d1dafd8346fda4e268bcf1747dc97925d1ab576;p=gnulib.git free-posix: Work around GCC mis-optimization bug. Code by Bernhard Voelker . * lib/free.c (rpl_free): Add alternative complicated code for GCC. --- diff --git a/ChangeLog b/ChangeLog index 840208f5ff..d7b506cf53 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2021-01-03 Bruno Haible + + free-posix: Work around GCC mis-optimization bug. + Code by Bernhard Voelker . + * lib/free.c (rpl_free): Add alternative complicated code for GCC. + 2021-01-02 Bruno Haible valgrind-tests: Disable valgrind if it would cause all tests to fail. diff --git a/lib/free.c b/lib/free.c index 135c3eb16b..5c89787aba 100644 --- a/lib/free.c +++ b/lib/free.c @@ -27,7 +27,21 @@ void rpl_free (void *p) #undef free { +#if defined __GNUC__ && !defined __clang__ + /* An invalid GCC optimization + + would optimize away the assignments in the code below, when link-time + optimization (LTO) is enabled. Make the code more complicated, so that + GCC does not grok how to optimize it. */ + int err[2]; + err[0] = errno; + err[1] = errno; + errno = 0; + free (p); + errno = err[errno == 0]; +#else int err = errno; free (p); errno = err; +#endif }