From: Yu Watanabe Date: Tue, 18 Nov 2025 20:09:02 +0000 (+0900) Subject: errno-util: avoid double evaluation in STRERROR_OR_EOF() X-Git-Tag: v259-rc2~68^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=459000e8c51b8979a8eb1dfa7f6b241ffe89f7a9;p=thirdparty%2Fsystemd.git errno-util: avoid double evaluation in STRERROR_OR_EOF() Follow-up for f69ae8585f5ce6cd8d1e6f3ccd6c9c2cf153e846. --- diff --git a/src/basic/errno-util.c b/src/basic/errno-util.c new file mode 100644 index 00000000000..cc07276a278 --- /dev/null +++ b/src/basic/errno-util.c @@ -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"; +} diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h index db13cd364aa..eb2941253dd 100644 --- a/src/basic/errno-util.h +++ b/src/basic/errno-util.h @@ -15,10 +15,9 @@ * 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? */ diff --git a/src/basic/meson.build b/src/basic/meson.build index c4427ee0375..d05d9581f2f 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -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',