From 6f7b08944398c7aafb2c3c785c668aeaa82935ca Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 28 Oct 2022 23:35:51 +0300 Subject: [PATCH 11/11] 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 4435259106..9745bdf9a2 100644 --- a/utility/support.c +++ b/utility/support.c @@ -101,6 +101,7 @@ /* utility */ #include "fciconv.h" #include "fcintl.h" +#include "fcthread.h" #include "log.h" #include "mem.h" #include "netintf.h" @@ -109,6 +110,7 @@ #ifndef HAVE_WORKING_VSNPRINTF static char *vsnprintf_buf = NULL; +fc_mutex vsnprintf_mutex; #endif /* HAVE_WORKING_VSNPRINTF */ /*************************************************************** @@ -809,6 +811,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) { @@ -818,6 +821,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 @@ -840,6 +845,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 */ @@ -1207,6 +1214,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