teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0836-basic-add-STRERROR-wrapper-for-strerror_r.patch

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