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