]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
errno-util: avoid double evaluation in STRERROR_OR_EOF()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Nov 2025 20:09:02 +0000 (05:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 20 Nov 2025 00:04:35 +0000 (09:04 +0900)
Follow-up for f69ae8585f5ce6cd8d1e6f3ccd6c9c2cf153e846.

src/basic/errno-util.c [new file with mode: 0644]
src/basic/errno-util.h
src/basic/meson.build

diff --git a/src/basic/errno-util.c b/src/basic/errno-util.c
new file mode 100644 (file)
index 0000000..cc07276
--- /dev/null
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "errno-util.h"
+
+const char* strerror_or_eof(int errnum, char *buf, size_t buflen) {
+        if (errnum != 0)
+                return strerror_r(ABS(errnum), buf, buflen);
+
+        return "Unexpected EOF";
+}
index db13cd364aa846b367d13c3acd5655f76d449579..eb2941253dd73e04bfedb83c8cffc359731116ac 100644 (file)
  * 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)
 
-/* 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
- * evaluated twice. */
-#define STRERROR_OR_EOF(errnum) ((errnum) != 0 ? STRERROR(errnum) : "Unexpected EOF")
+/* A helper to print an error message or message for functions that return 0 on EOF. */
+const char* strerror_or_eof(int errnum, char *buf, size_t buflen);
+#define STRERROR_OR_EOF(errnum) strerror_or_eof(errnum, (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN)
 
 static inline void _reset_errno_(int *saved_errno) {
         if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
index c4427ee03756520e0415ab6cb8d4d42079fce16e..d05d9581f2fefd08b14908cb3cb8777a324588bc 100644 (file)
@@ -31,6 +31,7 @@ basic_sources = files(
         'env-file.c',
         'env-util.c',
         'errno-list.c',
+        'errno-util.c',
         'escape.c',
         'ether-addr-util.c',
         'extract-word.c',