From 11163c8f885d5e247eac53e6e5ce228a322144e5 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 8 Oct 2022 03:13:24 +0300 Subject: [PATCH 20/20] Free internal fc_vsnprintf() buffer See osdn #45771 Signed-off-by: Marko Lindqvist --- common/fc_interface.c | 1 + utility/support.c | 43 +++++++++++++++++++++++++++++-------------- utility/support.h | 2 ++ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/common/fc_interface.c b/common/fc_interface.c index 43e472f021..a90b66e538 100644 --- a/common/fc_interface.c +++ b/common/fc_interface.c @@ -88,4 +88,5 @@ void free_libfreeciv(void) free_fileinfo_data(); netfile_free(); fc_strAPI_free(); + fc_support_free(); } diff --git a/utility/support.c b/utility/support.c index fc970a336f..fbed3b22f5 100644 --- a/utility/support.c +++ b/utility/support.c @@ -800,6 +800,10 @@ size_t fc_strlcpy(char *dest, const char *src, size_t n) return dlen; } +#ifndef HAVE_WORKING_VSNPRINTF +static char *vsnprintf_buf = NULL; +#endif /* HAVE_WORKING_VSNPRINTF */ + /************************************************************************//** fc_strlcat() provides utf-8 version of (non-standard) function strlcat() It is intended as more user-friendly version of strncat(), in particular @@ -897,8 +901,6 @@ int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap) { /* Don't use fc_malloc() or log_*() here, since they may call fc_vsnprintf() if it fails. */ - - static char *buf; size_t len; if (n > VSNP_BUF_SIZE) { @@ -907,25 +909,25 @@ int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap) exit(EXIT_FAILURE); } - if (!buf) { - buf = malloc(VSNP_BUF_SIZE); + if (vsnprintf_buf == NULL) { + vsnprinf_buf = malloc(VSNP_BUF_SIZE); - if (!buf) { - fprintf(stderr, "Could not allocate %i bytes for vsnprintf() " - "replacement.", VSNP_BUF_SIZE); - exit(EXIT_FAILURE); + if (vsnprintf_buf == NULL) { + fprintf(stderr, "Could not allocate %i bytes for vsnprintf() " + "replacement.", VSNP_BUF_SIZE); + exit(EXIT_FAILURE); } } - buf[VSNP_BUF_SIZE - 1] = '\0'; + vsnprintf_buf[VSNP_BUF_SIZE - 1] = '\0'; #ifdef HAVE_VSNPRINTF - vsnprintf(buf, n, format, ap); + vsnprintf(vsnprinf_buf, n, format, ap); #else - vsprintf(buf, format, ap); + vsprintf(vsnprintf_buf, format, ap); #endif /* HAVE_VSNPRINTF */ - len = strlen(buf); + len = strlen(vsnprintf_buf); if (len >= VSNP_BUF_SIZE - 1) { fprintf(stderr, "Overflow in vsnprintf replacement!" @@ -933,10 +935,10 @@ int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap) abort(); } if (n >= len + 1) { - memcpy(str, buf, len+1); + memcpy(str, vsnprintf_buf, len + 1); return len; } else { - memcpy(str, buf, n-1); + memcpy(str, vsnprintf_buf, n - 1); str[n - 1] = '\0'; return -1; } @@ -1294,3 +1296,16 @@ int fc_at_quick_exit(void (*func)(void)) return -1; #endif /* HAVE_AT_QUICK_EXIT */ } + +/***************************************************************** + Free misc resources allocated by the support module. +*****************************************************************/ +void fc_support_free(void) +{ +#ifndef HAVE_WORKING_VSNPRINTF + if (vsnprintf_buf != NULL) { + free(vsnprintf_buf); + vsnprintf_buf = NULL; + } +#endif /* HAVE_WORKING_VSNPRINTF */ +} diff --git a/utility/support.h b/utility/support.h index 778ec1fdad..4330db41c7 100644 --- a/utility/support.h +++ b/utility/support.h @@ -126,6 +126,8 @@ int fc_strncasequotecmp(const char *str0, const char *str1, size_t n); void fc_strAPI_init(void); void fc_strAPI_free(void); +void fc_support_free(void); + size_t effectivestrlenquote(const char *str); char *fc_strcasestr(const char *haystack, const char *needle); -- 2.35.1