teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

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

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