]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/errno-util: add helper to protect and set errno in one step
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 6 Oct 2022 12:41:50 +0000 (14:41 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 10 Oct 2022 07:12:21 +0000 (09:12 +0200)
This pattern is used in a few places. Those are changed in this patch.
Subsequent patches will add more.

src/basic/errno-util.h
src/basic/log.c

index 648de50eb497aeddbadc046d476e4214ac2c4e5a..a71864ca600d55a3ae9bf2b03788bd9c96b618b0 100644 (file)
@@ -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();"
index 2f5353c739d81470b3b9cce17cf5baef2980d459..39d08b092895c5da40b781023f54a7b6d15e28c9 100644 (file)
@@ -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) {