From 343287ffda88f521951d7c0d2db3bdb073f9fe88 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 28 Oct 2022 23:34:12 +0300 Subject: [PATCH 24/24] 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 2714f7c866..730742461f 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" @@ -802,6 +803,7 @@ size_t fc_strlcpy(char *dest, const char *src, size_t n) #ifndef HAVE_WORKING_VSNPRINTF static char *vsnprintf_buf = NULL; +fc_mutex vsnprintf_mutex; #endif /* HAVE_WORKING_VSNPRINTF */ /************************************************************************//** @@ -905,6 +907,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) { @@ -914,6 +917,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 @@ -936,6 +941,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 */ @@ -1305,6 +1312,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