From efaf94d0fde9fcd2ed890f4f8674c0c456d14610 Mon Sep 17 00:00:00 2001
From: "A. Wilcox" <AWilcox@Wilcox-Tech.com>
Date: Sun, 18 Aug 2024 02:01:56 -0500
Subject: [PATCH 02/34] Use XSI strerror_r instead of GNU strerror_r
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

This cannot go upstream.

Signed-off-by: Alexander Miroshnichenko <alex@millerson.name>
---
 src/basic/errno-util.h                   |  2 +-
 src/libsystemd/sd-bus/bus-error.c        | 56 ++++++++----------------
 src/libsystemd/sd-bus/test-bus-error.c   |  1 -
 src/libsystemd/sd-journal/journal-send.c |  7 +--
 4 files changed, 20 insertions(+), 46 deletions(-)

diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h
index 48b76e4bf70d..c48679c55cfd 100644
--- a/src/basic/errno-util.h
+++ b/src/basic/errno-util.h
@@ -15,7 +15,7 @@
  * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks
  *
  * Note that we use the GNU variant of strerror_r() here. */
-#define STRERROR(errnum) strerror_r(abs(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN)
+#define STRERROR(errnum) strerror(abs(errnum))
 
 /* A helper to print an error message or message for functions that return 0 on EOF.
  * Note that we can't use ({ … }) to define a temporary variable, so errnum is
diff --git a/src/libsystemd/sd-bus/bus-error.c b/src/libsystemd/sd-bus/bus-error.c
index f415797700ef..34bc7307bbcf 100644
--- a/src/libsystemd/sd-bus/bus-error.c
+++ b/src/libsystemd/sd-bus/bus-error.c
@@ -403,15 +403,13 @@ static void bus_error_strerror(sd_bus_error *e, int error) {
         assert(e);
 
         for (;;) {
-                char *x;
-
                 m = new(char, k);
                 if (!m)
                         return;
 
                 errno = 0;
-                x = strerror_r(error, m, k);
-                if (errno == ERANGE || strlen(x) >= k - 1) {
+                strerror_r(error, m, k);
+                if (errno == ERANGE) {
                         free(m);
                         k *= 2;
                         continue;
@@ -422,43 +420,24 @@ static void bus_error_strerror(sd_bus_error *e, int error) {
                         return;
                 }
 
-                if (x == m) {
-                        if (e->_need_free > 0) {
-                                /* Error is already dynamic, let's just update the message */
-                                free((char*) e->message);
-                                e->message = x;
-
-                        } else {
-                                char *t;
-                                /* Error was const so far, let's make it dynamic, if we can */
-
-                                t = strdup(e->name);
-                                if (!t) {
-                                        free(m);
-                                        return;
-                                }
+                if (e->_need_free > 0) {
+                        /* Error is already dynamic, let's just update the message */
+                        free((char*) e->message);
+                        e->message = m;
 
-                                e->_need_free = 1;
-                                e->name = t;
-                                e->message = x;
-                        }
                 } else {
-                        free(m);
-
-                        if (e->_need_free > 0) {
-                                char *t;
-
-                                /* Error is dynamic, let's hence make the message also dynamic */
-                                t = strdup(x);
-                                if (!t)
-                                        return;
+                        char *t;
+                        /* Error was const so far, let's make it dynamic, if we can */
 
-                                free((char*) e->message);
-                                e->message = t;
-                        } else {
-                                /* Error is const, hence we can just override */
-                                e->message = x;
+                        t = strdup(e->name);
+                        if (!t) {
+                                free(m);
+                                return;
                         }
+
+                        e->_need_free = 1;
+                        e->name = t;
+                        e->message = m;
                 }
 
                 return;
@@ -596,7 +575,8 @@ const char* _bus_error_message(const sd_bus_error *e, int error, char buf[static
         if (e && e->message)
                 return e->message;
 
-        return strerror_r(abs(error), buf, ERRNO_BUF_LEN);
+        strerror_r(abs(error), buf, ERRNO_BUF_LEN);
+        return buf;
 }
 
 static bool map_ok(const sd_bus_error_map *map) {
diff --git a/src/libsystemd/sd-bus/test-bus-error.c b/src/libsystemd/sd-bus/test-bus-error.c
index 91045c06c2ae..af3332d29a23 100644
--- a/src/libsystemd/sd-bus/test-bus-error.c
+++ b/src/libsystemd/sd-bus/test-bus-error.c
@@ -232,7 +232,6 @@ TEST(sd_bus_error_set_errnof) {
         errno = EACCES;
         assert_se(asprintf(&str, "%m") >= 0);
         assert_se(streq(error.message, str));
-        assert_se(error._need_free == 0);
 
         str = mfree(str);
         sd_bus_error_free(&error);
diff --git a/src/libsystemd/sd-journal/journal-send.c b/src/libsystemd/sd-journal/journal-send.c
index 7d02b57d7b42..1eea1e885620 100644
--- a/src/libsystemd/sd-journal/journal-send.c
+++ b/src/libsystemd/sd-journal/journal-send.c
@@ -2,7 +2,6 @@
 
 #include <errno.h>
 #include <fcntl.h>
-#include <printf.h>
 #include <stddef.h>
 #include <sys/un.h>
 #include <unistd.h>
@@ -358,16 +357,12 @@ static int fill_iovec_perror_and_send(const char *message, int skip, struct iove
 
         for (;;) {
                 char buffer[n];
-                char* j;
 
                 errno = 0;
-                j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
+                strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
                 if (errno == 0) {
                         char error[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
 
-                        if (j != buffer + 8 + k)
-                                memmove(buffer + 8 + k, j, strlen(j)+1);
-
                         memcpy(buffer, "MESSAGE=", 8);
 
                         if (k > 0) {
-- 
2.41.0

