b11b5f
From 9c95d8dda42de288a57638a44dd5ea967469063d Mon Sep 17 00:00:00 2001
b11b5f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
b11b5f
Date: Fri, 7 Oct 2022 12:28:31 +0200
b11b5f
Subject: [PATCH] basic: add STRERROR() wrapper for strerror_r()
b11b5f
b11b5f
(cherry picked from commit 2c5d05b3cd986568105d67891e4010b868dea24f)
b11b5f
b11b5f
Related: #2155520
b11b5f
---
b11b5f
 src/basic/util.h     | 10 ++++++++++
b11b5f
 src/test/test-util.c | 40 ++++++++++++++++++++++++++++++++++++++++
b11b5f
 2 files changed, 50 insertions(+)
b11b5f
b11b5f
diff --git a/src/basic/util.h b/src/basic/util.h
b11b5f
index 76b76d7e91..195f02cf5f 100644
b11b5f
--- a/src/basic/util.h
b11b5f
+++ b/src/basic/util.h
b11b5f
@@ -153,6 +153,16 @@ static inline void _reset_errno_(int *saved_errno) {
b11b5f
         errno = *saved_errno;
b11b5f
 }
b11b5f
 
b11b5f
+/* strerror(3) says that glibc uses a maximum length of 1024 bytes. */
b11b5f
+#define ERRNO_BUF_LEN 1024
b11b5f
+
b11b5f
+/* Note: the lifetime of the compound literal is the immediately surrounding block,
b11b5f
+ * see C11 §6.5.2.5, and
b11b5f
+ * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks
b11b5f
+ *
b11b5f
+ * Note that we use the GNU variant of strerror_r() here. */
b11b5f
+#define STRERROR(errnum) strerror_r(abs(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN)
b11b5f
+
b11b5f
 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
b11b5f
 
b11b5f
 #define UNPROTECT_ERRNO                         \
b11b5f
diff --git a/src/test/test-util.c b/src/test/test-util.c
b11b5f
index df60d89115..c93eaf7fc6 100644
b11b5f
--- a/src/test/test-util.c
b11b5f
+++ b/src/test/test-util.c
b11b5f
@@ -12,6 +12,7 @@
b11b5f
 #include "process-util.h"
b11b5f
 #include "raw-clone.h"
b11b5f
 #include "rm-rf.h"
b11b5f
+#include "stdio-util.h"
b11b5f
 #include "string-util.h"
b11b5f
 #include "util.h"
b11b5f
 
b11b5f
@@ -321,6 +322,42 @@ static void test_system_tasks_max_scale(void) {
b11b5f
         assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX);
b11b5f
 }
b11b5f
 
b11b5f
+static void test_strerror_not_threadsafe(void) {
b11b5f
+        /* Just check that strerror really is not thread-safe. */
b11b5f
+        log_info("strerror(%d) → %s", 200, strerror(200));
b11b5f
+        log_info("strerror(%d) → %s", 201, strerror(201));
b11b5f
+        log_info("strerror(%d) → %s", INT_MAX, strerror(INT_MAX));
b11b5f
+
b11b5f
+        log_info("strerror(%d), strerror(%d) → %p, %p", 200, 201, strerror(200), strerror(201));
b11b5f
+
b11b5f
+        /* This call is not allowed, because the first returned string becomes invalid when
b11b5f
+         * we call strerror the second time:
b11b5f
+         *
b11b5f
+         * log_info("strerror(%d), strerror(%d) → %s, %s", 200, 201, strerror(200), strerror(201));
b11b5f
+         */
b11b5f
+}
b11b5f
+
b11b5f
+static void test_STRERROR(void) {
b11b5f
+        /* Just check that STRERROR really is thread-safe. */
b11b5f
+        log_info("STRERROR(%d) → %s", 200, STRERROR(200));
b11b5f
+        log_info("STRERROR(%d) → %s", 201, STRERROR(201));
b11b5f
+        log_info("STRERROR(%d), STRERROR(%d) → %s, %s", 200, 201, STRERROR(200), STRERROR(201));
b11b5f
+
b11b5f
+        const char *a = STRERROR(200), *b = STRERROR(201);
b11b5f
+        assert_se(strstr(a, "200"));
b11b5f
+        assert_se(strstr(b, "201"));
b11b5f
+
b11b5f
+        /* Check with negative values */
b11b5f
+        assert_se(streq(a, STRERROR(-200)));
b11b5f
+        assert_se(streq(b, STRERROR(-201)));
b11b5f
+
b11b5f
+        const char *c = STRERROR(INT_MAX);
b11b5f
+        char buf[DECIMAL_STR_MAX(int)];
b11b5f
+        xsprintf(buf, "%d", INT_MAX);  /* INT_MAX is hexadecimal, use printf to convert to decimal */
b11b5f
+        log_info("STRERROR(%d) → %s", INT_MAX, c);
b11b5f
+        assert_se(strstr(c, buf));
b11b5f
+}
b11b5f
+
b11b5f
 int main(int argc, char *argv[]) {
b11b5f
         log_parse_environment();
b11b5f
         log_open();
b11b5f
@@ -340,5 +377,8 @@ int main(int argc, char *argv[]) {
b11b5f
         test_system_tasks_max();
b11b5f
         test_system_tasks_max_scale();
b11b5f
 
b11b5f
+        test_strerror_not_threadsafe();
b11b5f
+        test_STRERROR();
b11b5f
+
b11b5f
         return 0;
b11b5f
 }