From: Zbigniew Jędrzejewski-Szmek Date: Thu, 6 Oct 2022 12:41:50 +0000 (+0200) Subject: basic/errno-util: add helper to protect and set errno in one step X-Git-Tag: v252-rc2~70^2~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=50c5b991df302e7d143e8a859dd9d95c419d60fc;p=thirdparty%2Fsystemd.git basic/errno-util: add helper to protect and set errno in one step This pattern is used in a few places. Those are changed in this patch. Subsequent patches will add more. --- diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h index 648de50eb49..a71864ca600 100644 --- a/src/basic/errno-util.h +++ b/src/basic/errno-util.h @@ -22,6 +22,10 @@ static inline void _reset_errno_(int *saved_errno) { _saved_errno_ = -1; \ } while (false) +#define LOCAL_ERRNO(value) \ + PROTECT_ERRNO; \ + errno = abs(value) + static inline int negative_errno(void) { /* This helper should be used to shut up gcc if you know 'errno' is * negative. Instead of "return -errno;", use "return negative_errno();" diff --git a/src/basic/log.c b/src/basic/log.c index 2f5353c739d..39d08b09289 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -732,14 +732,12 @@ int log_internalv( const char *format, va_list ap) { - char buffer[LINE_MAX]; - PROTECT_ERRNO; - if (_likely_(LOG_PRI(level) > log_max_level)) return -ERRNO_VALUE(error); /* Make sure that %m maps to the specified error (or "Success"). */ - errno = ERRNO_VALUE(error); + char buffer[LINE_MAX]; + LOCAL_ERRNO(ERRNO_VALUE(error)); (void) vsnprintf(buffer, sizeof buffer, format, ap); @@ -777,14 +775,13 @@ int log_object_internalv( const char *format, va_list ap) { - PROTECT_ERRNO; char *buffer, *b; if (_likely_(LOG_PRI(level) > log_max_level)) return -ERRNO_VALUE(error); /* Make sure that %m maps to the specified error (or "Success"). */ - errno = ERRNO_VALUE(error); + LOCAL_ERRNO(ERRNO_VALUE(error)); /* Prepend the object name before the message */ if (object) {