From 81b1cb4d4e4d4490d953b41ee32c34de594df0d9 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 8 Oct 2022 03:05:49 +0300 Subject: [PATCH 35/35] Free internal fc_vsnprintf() buffer See osdn #45771 Signed-off-by: Marko Lindqvist --- common/fc_interface.c | 1 + utility/support.c | 37 ++++++++++++++++++++++++++----------- utility/support.h | 2 ++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/common/fc_interface.c b/common/fc_interface.c index 1a872b7c46..e88c732b60 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 5099310634..60f2c97c99 100644 --- a/utility/support.c +++ b/utility/support.c @@ -115,6 +115,10 @@ static UChar *icu_buffer1 = NULL; static UChar *icu_buffer2 = NULL; fc_mutex icu_buffer_mutex; +#ifndef HAVE_WORKING_VSNPRINTF +static char *vsnprintf_buf = NULL; +#endif /* HAVE_WORKING_VSNPRINTF */ + /************************************************************************//** Initial allocation of string comparison buffers. ****************************************************************************/ @@ -901,8 +905,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) { @@ -911,25 +913,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) { + vsnprintf_buf = malloc(VSNP_BUF_SIZE); - if (!buf) { + 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(vsnprintf_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!" @@ -937,10 +939,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; } @@ -1300,3 +1302,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 206f4de2bc..a3cbb393bd 100644 --- a/utility/support.h +++ b/utility/support.h @@ -132,6 +132,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