summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2021-05-20 09:18:49 +0200
committerWerner Koch <wk@gnupg.org>2021-05-20 09:19:44 +0200
commit448bf7b01cade87f45fb39f455f37a6aadeeceda (patch)
tree27a07f52ef637cd6937fbdebf2ea2f3ecf2247e5
parentad062b0a5b7d598081405ecfb71b51540281a1b7 (diff)
downloadlibgpg-error-448bf7b01cade87f45fb39f455f37a6aadeeceda.tar.gz
core: Make gpgrt_free robust against legacy free implementations.
* src/init.c (_gpgrt_free): Shortcut NULL and save ERRNO. Signed-off-by: Werner Koch <wk@gnupg.org>
-rw-r--r--src/init.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/init.c b/src/init.c
index 6239682..2d9f7ab 100644
--- a/src/init.c
+++ b/src/init.c
@@ -407,7 +407,19 @@ _gpgrt_strconcat (const char *s1, ...)
void
_gpgrt_free (void *a)
{
+ int save_errno;
+
+ if (!a)
+ return; /* Shortcut */
+
+ /* In case ERRNO is set we better save it so that the free machinery
+ * may not accidentally change ERRNO. We restore it only if it was
+ * already set to comply with the usual C semantic for ERRNO.
+ * See also https://dev.gnupg.org/T5393#146261 */
+ save_errno = errno;
_gpgrt_realloc (a, 0);
+ if (save_errno && save_errno != errno)
+ _gpg_err_set_errno (save_errno);
}