naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
ff6046
From b05795bd3d8afcdcb765639a636ada8d36a2ee79 Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Fri, 18 Jan 2019 20:04:13 +0100
ff6046
Subject: [PATCH] util.h: add new UNPROTECT_ERRNO macro
ff6046
ff6046
THis is inspired by #11395, but much simpler.
ff6046
ff6046
(cherry picked from commit 840f606d88fef2f5d240b2d759ce7b951354d5bb)
ff6046
ff6046
Resolves: #1691691
ff6046
---
ff6046
 src/basic/util.h        |  9 +++++++++
ff6046
 src/test/test-fs-util.c |  6 +++---
ff6046
 src/test/test-util.c    | 25 +++++++++++++++++++++++++
ff6046
 3 files changed, 37 insertions(+), 3 deletions(-)
ff6046
ff6046
diff --git a/src/basic/util.h b/src/basic/util.h
ff6046
index 9699d228f9..27b5a09782 100644
ff6046
--- a/src/basic/util.h
ff6046
+++ b/src/basic/util.h
ff6046
@@ -134,11 +134,20 @@ static inline void *mempset(void *s, int c, size_t n) {
ff6046
 }
ff6046
 
ff6046
 static inline void _reset_errno_(int *saved_errno) {
ff6046
+        if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */
ff6046
+                return;
ff6046
+
ff6046
         errno = *saved_errno;
ff6046
 }
ff6046
 
ff6046
 #define PROTECT_ERRNO _cleanup_(_reset_errno_) __attribute__((unused)) int _saved_errno_ = errno
ff6046
 
ff6046
+#define UNPROTECT_ERRNO                         \
ff6046
+        do {                                    \
ff6046
+                errno = _saved_errno_;          \
ff6046
+                _saved_errno_ = -1;             \
ff6046
+        } while (false)
ff6046
+
ff6046
 static inline int negative_errno(void) {
ff6046
         /* This helper should be used to shut up gcc if you know 'errno' is
ff6046
          * negative. Instead of "return -errno;", use "return negative_errno();"
ff6046
diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c
ff6046
index fc650b513e..7b7990bb70 100644
ff6046
--- a/src/test/test-fs-util.c
ff6046
+++ b/src/test/test-fs-util.c
ff6046
@@ -322,11 +322,11 @@ static void test_unlink_noerrno(void) {
ff6046
 
ff6046
         {
ff6046
                 PROTECT_ERRNO;
ff6046
-                errno = -42;
ff6046
+                errno = 42;
ff6046
                 assert_se(unlink_noerrno(name) >= 0);
ff6046
-                assert_se(errno == -42);
ff6046
+                assert_se(errno == 42);
ff6046
                 assert_se(unlink_noerrno(name) < 0);
ff6046
-                assert_se(errno == -42);
ff6046
+                assert_se(errno == 42);
ff6046
         }
ff6046
 }
ff6046
 
ff6046
diff --git a/src/test/test-util.c b/src/test/test-util.c
ff6046
index 4d3e5c5b94..df60d89115 100644
ff6046
--- a/src/test/test-util.c
ff6046
+++ b/src/test/test-util.c
ff6046
@@ -164,6 +164,30 @@ static void test_protect_errno(void) {
ff6046
         assert_se(errno == 12);
ff6046
 }
ff6046
 
ff6046
+static void test_unprotect_errno_inner_function(void) {
ff6046
+        PROTECT_ERRNO;
ff6046
+
ff6046
+        errno = 2222;
ff6046
+}
ff6046
+
ff6046
+static void test_unprotect_errno(void) {
ff6046
+        log_info("/* %s */", __func__);
ff6046
+
ff6046
+        errno = 4711;
ff6046
+
ff6046
+        PROTECT_ERRNO;
ff6046
+
ff6046
+        errno = 815;
ff6046
+
ff6046
+        UNPROTECT_ERRNO;
ff6046
+
ff6046
+        assert_se(errno == 4711);
ff6046
+
ff6046
+        test_unprotect_errno_inner_function();
ff6046
+
ff6046
+        assert_se(errno == 4711);
ff6046
+}
ff6046
+
ff6046
 static void test_in_set(void) {
ff6046
         assert_se(IN_SET(1, 1));
ff6046
         assert_se(IN_SET(1, 1, 2, 3, 4));
ff6046
@@ -307,6 +331,7 @@ int main(int argc, char *argv[]) {
ff6046
         test_div_round_up();
ff6046
         test_u64log2();
ff6046
         test_protect_errno();
ff6046
+        test_unprotect_errno();
ff6046
         test_in_set();
ff6046
         test_log2i();
ff6046
         test_raw_clone();