From 1fa58f49e6f89480764fd10833977eb815ac1183 Mon Sep 17 00:00:00 2001 From: Igor Kulaychuk Date: Thu, 13 Dec 2018 15:13:29 +0300 Subject: [PATCH] Fix strerror thread-safety issue - use strerror_r --- logging.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/logging.c b/logging.c index 1464253..ef85499 100644 --- a/logging.c +++ b/logging.c @@ -152,9 +152,19 @@ static void log_prefixed_system_error(FILE *file, int error_code, const char *pr if (prefix != NULL) { fputs(prefix, file); } + + char buf[1024] = {0}; + char *errno_msg; +#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE + int ret = strerror_r(error_code, buf, sizeof(buf)); + errno_msg = ret == 0 ? buf : ""; +#else + errno_msg = strerror_r(errno, buf, sizeof(buf)); +#endif + log_current_data(); vfprintf(file, fmt, arg); - fprintf(file, ": %s\n", strerror(error_code)); + fprintf(file, ": %s\n", errno_msg); fflush(file); pthread_mutex_unlock(&log_lock); } -- 2.7.4