From: Eli Zaretskii Date: Thu, 20 May 2021 08:44:54 +0000 (+0300) Subject: Make sure gmalloc's hybrid_free preserves errno X-Git-Tag: emacs-28.0.90~2387 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=328efb47d04e3aa996bb8cd387d01c1a66ec29f5;p=emacs.git Make sure gmalloc's hybrid_free preserves errno * src/gmalloc.c (hybrid_free_1): New function, with the body of the previous 'hybrid_free'. (hybrid_free): Call 'hybrid_free_1' while preserving the value of 'errno'. Suggested by Paul Eggert . --- diff --git a/src/gmalloc.c b/src/gmalloc.c index dedd25fa22f..55ae7365d99 100644 --- a/src/gmalloc.c +++ b/src/gmalloc.c @@ -1726,8 +1726,8 @@ hybrid_calloc (size_t nmemb, size_t size) return gcalloc (nmemb, size); } -void -hybrid_free (void *ptr) +static void +hybrid_free_1 (void *ptr) { if (allocated_via_gmalloc (ptr)) gfree (ptr); @@ -1735,6 +1735,24 @@ hybrid_free (void *ptr) free (ptr); } +void +hybrid_free (void *ptr) +{ + /* Stolen from Gnulib, to make sure we preserve errno. */ +#if defined __GNUC__ && !defined __clang__ + int err[2]; + err[0] = errno; + err[1] = errno; + errno = 0; + hybrid_free_1 (ptr); + errno = err[errno == 0]; +#else + int err = errno; + hybrid_free_1 (ptr); + errno = err; +#endif +} + #if defined HAVE_ALIGNED_ALLOC || defined HAVE_POSIX_MEMALIGN void * hybrid_aligned_alloc (size_t alignment, size_t size)