Fix strerror thread-safety issue - use strerror_r
authorIgor Kulaychuk <i.kulaychuk@samsung.com>
Thu, 13 Dec 2018 12:13:29 +0000 (15:13 +0300)
committerIgor Kulaychuk <i.kulaychuk@samsung.com>
Thu, 13 Dec 2018 12:23:39 +0000 (15:23 +0300)
logging.c

index 1464253fe8a235fb5aaa791f3e61698346f38efc..ef85499a823e834f81e7148488738f9eea60fed8 100644 (file)
--- 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 : "<strerror_r failure>";
+#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);
 }