From bbe0096367618026fe5ebb81c266fd50084f113a Mon Sep 17 00:00:00 2001
From: Chen Qi <Qi.Chen@windriver.com>
Date: Tue, 10 Jul 2018 15:40:17 +0800
Subject: [PATCH 08/16] distinguish XSI-compliant strerror_r from GNU-specific
 strerror_r

XSI-compliant strerror_r and GNU-specific strerror_r are different.

       int strerror_r(int errnum, char *buf, size_t buflen);
                   /* XSI-compliant */

       char *strerror_r(int errnum, char *buf, size_t buflen);
                   /* GNU-specific */

We need to distinguish between them. Otherwise, we'll get an int value
assigned to (char *) variable, resulting in segment fault.

Upstream-Status: Inappropriate [musl specific]

Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
---
 src/libsystemd/sd-bus/bus-error.c        | 11 ++++++++++-
 src/libsystemd/sd-journal/journal-send.c |  5 +++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/libsystemd/sd-bus/bus-error.c b/src/libsystemd/sd-bus/bus-error.c
index bc0164c1b4..e69ffe4f75 100644
--- a/src/libsystemd/sd-bus/bus-error.c
+++ b/src/libsystemd/sd-bus/bus-error.c
@@ -400,7 +400,12 @@ static void bus_error_strerror(sd_bus_error *e, int error) {
                         return;
 
                 errno = 0;
+#ifndef __GLIBC__
+                strerror_r(error, m, k);
+                x = m;
+#else
                 x = strerror_r(error, m, k);
+#endif
                 if (errno == ERANGE || strlen(x) >= k - 1) {
                         free(m);
                         k *= 2;
@@ -585,8 +590,12 @@ const char* _bus_error_message(const sd_bus_error *e, int error, char buf[static
 
         if (e && e->message)
                 return e->message;
-
+#ifndef __GLIBC__
+        strerror_r(ABS(error), buf, ERRNO_BUF_LEN);
+        return buf;
+#else
         return strerror_r(ABS(error), buf, ERRNO_BUF_LEN);
+#endif
 }
 
 static bool map_ok(const sd_bus_error_map *map) {
diff --git a/src/libsystemd/sd-journal/journal-send.c b/src/libsystemd/sd-journal/journal-send.c
index e838e7e0d7..28b6e014c0 100644
--- a/src/libsystemd/sd-journal/journal-send.c
+++ b/src/libsystemd/sd-journal/journal-send.c
@@ -338,7 +338,12 @@ static int fill_iovec_perror_and_send(const char *message, int skip, struct iove
                 char* j;
 
                 errno = 0;
+#ifndef __GLIBC__
+                strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
+                j = buffer + 8 + k;
+#else
                 j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
+#endif
                 if (errno == 0) {
                         char error[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
 
-- 
2.34.1

