From 3d65cfcb6403d169b52045a9e5c242ad081539a7 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 4 Sep 2019 21:17:48 +0200
Subject: Chosen the right strerror_r() function depending on macros settings.

---
 src/core/logs.h | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/core/logs.h b/src/core/logs.h
index fd6e777..8019382 100644
--- a/src/core/logs.h
+++ b/src/core/logs.h
@@ -78,12 +78,30 @@ void log_variadic_message(LogMessageType, const char *, ...);
 #define LOG_ERROR(tp, msg) \
     log_variadic_message(tp, "[%s:%u] %s", __FUNCTION__, __LINE__, msg)
 
+#if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
+
+#   define STRERROR_SAFE(buf, ptr)                                                                      \
+    do                                                                                                  \
+    {                                                                                                   \
+        strerror_r(errno, buf, sizeof(buf));                                                            \
+        ptr = buf;                                                                                      \
+    }                                                                                                   \
+    while (0)
+
+#else
+
+#   define STRERROR_SAFE(buf, ptr)                                                                      \
+    ptr = strerror_r(errno, buf, sizeof(buf))                                                           \
+
+#endif
+
 #define LOG_ERROR_N(func)                                                                               \
     do                                                                                                  \
     {                                                                                                   \
         char __msg[1024];                                                                               \
-        strerror_r(errno, __msg, sizeof(__msg));                                                        \
-        log_variadic_message(LMT_EXT_ERROR, "[%s:%u] %s: %s", __FUNCTION__, __LINE__, func, __msg);     \
+        const char *__msg_ptr;                                                                          \
+        STRERROR_SAFE(__msg, __msg_ptr);                                                                \
+        log_variadic_message(LMT_EXT_ERROR, "[%s:%u] %s: %s", __FUNCTION__, __LINE__, func, __msg_ptr); \
     }                                                                                                   \
     while (0)
 
@@ -93,10 +111,7 @@ void log_variadic_message(LogMessageType, const char *, ...);
         char __msg[1024];                                                                               \
         const char *__msg_ptr;                                                                          \
         if (errcode == EAI_SYSTEM)                                                                      \
-        {                                                                                               \
-            strerror_r(errno, __msg, sizeof(__msg));                                                    \
-            __msg_ptr = __msg;                                                                          \
-        }                                                                                               \
+            STRERROR_SAFE(__msg, __msg_ptr);                                                            \
         else                                                                                            \
             __msg_ptr = gai_strerror(errcode);                                                          \
         log_variadic_message(LMT_EXT_ERROR, "[%s:%u] %s: %s", __FUNCTION__, __LINE__, func, __msg_ptr); \
-- 
cgit v0.11.2-87-g4458