From fa159d8c3d48bb877976ca00b94dab91f344cb7c Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 28 Oct 2022 23:30:15 +0300 Subject: [PATCH 32/32] Make fallback version of fc_vsnprintf() thread safe See osdn #45905 Signed-off-by: Marko Lindqvist --- utility/support.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/utility/support.c b/utility/support.c index 88182c6f0a..84faaad989 100644 --- a/utility/support.c +++ b/utility/support.c @@ -104,6 +104,7 @@ /* utility */ #include "fciconv.h" #include "fcintl.h" +#include "fcthread.h" #include "log.h" #include "mem.h" #include "netintf.h" @@ -117,6 +118,7 @@ fc_mutex icu_buffer_mutex; #ifndef HAVE_WORKING_VSNPRINTF static char *vsnprintf_buf = NULL; +fc_mutex vsnprintf_mutex; #endif /* HAVE_WORKING_VSNPRINTF */ /************************************************************************//** @@ -909,6 +911,7 @@ int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap) } if (vsnprintf_buf == NULL) { + fc_init_mutex(&vsnprintf_mutex); vsnprintf_buf = malloc(VSNP_BUF_SIZE); if (vsnprintf_buf == NULL) { @@ -918,6 +921,8 @@ int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap) } } + fc_allocate_mutex(&vsnprintf_mutex); + vsnprintf_buf[VSNP_BUF_SIZE - 1] = '\0'; #ifdef HAVE_VSNPRINTF @@ -940,6 +945,8 @@ int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap) str[n - 1] = '\0'; } + fc_release_mutex(&vsnprintf_mutex); + return len; } #endif /* HAVE_WORKING_VSNPRINTF */ @@ -1311,6 +1318,7 @@ void fc_support_free(void) if (vsnprintf_buf != NULL) { free(vsnprintf_buf); vsnprintf_buf = NULL; + fc_destroy_mutex(&vsnprintf_mutex); } #endif /* HAVE_WORKING_VSNPRINTF */ } -- 2.35.1