From 47f489c3ad16c3630804ef91960b4da1531785e2 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 8 Oct 2022 03:20:45 +0300 Subject: [PATCH 2/2] 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 3e830a2def..06fadca60e 100644 --- a/common/fc_interface.c +++ b/common/fc_interface.c @@ -74,4 +74,5 @@ void free_libfreeciv(void) free_multicast_group(); free_user_home_dir(); free_fileinfo_data(); + fc_support_free(); } diff --git a/utility/support.c b/utility/support.c index d3f2c722d6..09da27c67b 100644 --- a/utility/support.c +++ b/utility/support.c @@ -107,6 +107,10 @@ #include "support.h" +#ifndef HAVE_WORKING_VSNPRINTF +static char *vsnprintf_buf = NULL; +#endif /* HAVE_WORKING_VSNPRINTF */ + /*************************************************************** Compare strings like strcmp(), but ignoring case. ***************************************************************/ @@ -801,8 +805,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) { @@ -811,25 +813,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) { - 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(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!" @@ -837,10 +839,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; } @@ -1200,3 +1202,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 ef2d30ec71..e71fb4af26 100644 --- a/utility/support.h +++ b/utility/support.h @@ -124,6 +124,8 @@ int fc_strcasecmp(const char *str0, const char *str1); int fc_strncasecmp(const char *str0, const char *str1, size_t n); int fc_strncasequotecmp(const char *str0, const char *str1, size_t n); +void fc_support_free(void); + size_t effectivestrlenquote(const char *str); char *fc_strcasestr(const char *haystack, const char *needle); -- 2.35.1