errno-util: avoid double evaluation in STRERROR_OR_EOF()

Follow-up for f69ae8585f.
This commit is contained in:
Yu Watanabe
2025-11-19 05:09:02 +09:00
parent 18f280a478
commit 459000e8c5
3 changed files with 14 additions and 4 deletions

10
src/basic/errno-util.c Normal file
View File

@@ -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";
}

View File

@@ -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? */

View 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',