5fc298
From f42e15fdfba61fe6dee0bfb0a6a7f44fd9ca9dd3 Mon Sep 17 00:00:00 2001
5fc298
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
5fc298
Date: Fri, 7 Oct 2022 12:28:31 +0200
5fc298
Subject: [PATCH] basic: add STRERROR() wrapper for strerror_r()
5fc298
5fc298
(cherry picked from commit 2c5d05b3cd986568105d67891e4010b868dea24f)
5fc298
5fc298
Related: #2155516
5fc298
---
5fc298
 src/basic/errno-util.h     | 10 +++++++++
5fc298
 src/test/meson.build       |  2 ++
5fc298
 src/test/test-errno-util.c | 44 ++++++++++++++++++++++++++++++++++++++
5fc298
 3 files changed, 56 insertions(+)
5fc298
 create mode 100644 src/test/test-errno-util.c
5fc298
5fc298
diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h
5fc298
index 09abf0b751..a2d9876c15 100644
5fc298
--- a/src/basic/errno-util.h
5fc298
+++ b/src/basic/errno-util.h
5fc298
@@ -6,6 +6,16 @@
5fc298
 
5fc298
 #include "macro.h"
5fc298
 
5fc298
+/* strerror(3) says that glibc uses a maximum length of 1024 bytes. */
5fc298
+#define ERRNO_BUF_LEN 1024
5fc298
+
5fc298
+/* Note: the lifetime of the compound literal is the immediately surrounding block,
5fc298
+ * see C11 §6.5.2.5, and
5fc298
+ * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks
5fc298
+ *
5fc298
+ * Note that we use the GNU variant of strerror_r() here. */
5fc298
+#define STRERROR(errnum) strerror_r(abs(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN)
5fc298
+
5fc298
 static inline void _reset_errno_(int *saved_errno) {
5fc298
         if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
5fc298
                 return;
5fc298
diff --git a/src/test/meson.build b/src/test/meson.build
5fc298
index 14b7939b1f..032800dd85 100644
5fc298
--- a/src/test/meson.build
5fc298
+++ b/src/test/meson.build
5fc298
@@ -580,6 +580,8 @@ tests += [
5fc298
         [files('test-errno-list.c') +
5fc298
          generated_gperf_headers],
5fc298
 
5fc298
+        [files('test-errno-util.c')],
5fc298
+
5fc298
         [files('test-ip-protocol-list.c') +
5fc298
          shared_generated_gperf_headers],
5fc298
 
5fc298
diff --git a/src/test/test-errno-util.c b/src/test/test-errno-util.c
5fc298
new file mode 100644
5fc298
index 0000000000..284f451002
5fc298
--- /dev/null
5fc298
+++ b/src/test/test-errno-util.c
5fc298
@@ -0,0 +1,44 @@
5fc298
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
5fc298
+
5fc298
+#include "errno-util.h"
5fc298
+#include "stdio-util.h"
5fc298
+#include "string-util.h"
5fc298
+#include "tests.h"
5fc298
+
5fc298
+TEST(strerror_not_threadsafe) {
5fc298
+        /* Just check that strerror really is not thread-safe. */
5fc298
+        log_info("strerror(%d) → %s", 200, strerror(200));
5fc298
+        log_info("strerror(%d) → %s", 201, strerror(201));
5fc298
+        log_info("strerror(%d) → %s", INT_MAX, strerror(INT_MAX));
5fc298
+
5fc298
+        log_info("strerror(%d), strerror(%d) → %p, %p", 200, 201, strerror(200), strerror(201));
5fc298
+
5fc298
+        /* This call is not allowed, because the first returned string becomes invalid when
5fc298
+         * we call strerror the second time:
5fc298
+         *
5fc298
+         * log_info("strerror(%d), strerror(%d) → %s, %s", 200, 201, strerror(200), strerror(201));
5fc298
+         */
5fc298
+}
5fc298
+
5fc298
+TEST(STRERROR) {
5fc298
+        /* Just check that STRERROR really is thread-safe. */
5fc298
+        log_info("STRERROR(%d) → %s", 200, STRERROR(200));
5fc298
+        log_info("STRERROR(%d) → %s", 201, STRERROR(201));
5fc298
+        log_info("STRERROR(%d), STRERROR(%d) → %s, %s", 200, 201, STRERROR(200), STRERROR(201));
5fc298
+
5fc298
+        const char *a = STRERROR(200), *b = STRERROR(201);
5fc298
+        assert_se(strstr(a, "200"));
5fc298
+        assert_se(strstr(b, "201"));
5fc298
+
5fc298
+        /* Check with negative values */
5fc298
+        assert_se(streq(a, STRERROR(-200)));
5fc298
+        assert_se(streq(b, STRERROR(-201)));
5fc298
+
5fc298
+        const char *c = STRERROR(INT_MAX);
5fc298
+        char buf[DECIMAL_STR_MAX(int)];
5fc298
+        xsprintf(buf, "%d", INT_MAX);  /* INT_MAX is hexadecimal, use printf to convert to decimal */
5fc298
+        log_info("STRERROR(%d) → %s", INT_MAX, c);
5fc298
+        assert_se(strstr(c, buf));
5fc298
+}
5fc298
+
5fc298
+DEFINE_TEST_MAIN(LOG_INFO);