Blame SOURCES/0585-fileio-use-_cleanup_-for-FILE-unlocking.patch

17b0f1
From d750826683aaad1cf33b16290c7daa8b0a669f4c Mon Sep 17 00:00:00 2001
17b0f1
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
17b0f1
Date: Sat, 23 Sep 2017 10:48:09 +0200
17b0f1
Subject: [PATCH] fileio: use _cleanup_ for FILE unlocking
17b0f1
17b0f1
(cherry picked from commit f858e5148e4f36335555dfaac812197ebd3ef036)
17b0f1
17b0f1
Resolves: #1503106
17b0f1
---
17b0f1
 src/shared/fileio.c | 57 +++++++++++++++++++++------------------------
17b0f1
 1 file changed, 27 insertions(+), 30 deletions(-)
17b0f1
17b0f1
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
17b0f1
index be775f982a..4880a4941f 100644
17b0f1
--- a/src/shared/fileio.c
17b0f1
+++ b/src/shared/fileio.c
17b0f1
@@ -802,10 +802,13 @@ int get_status_field(const char *filename, const char *pattern, char **field) {
17b0f1
         return 0;
17b0f1
 }
17b0f1
 
17b0f1
+static inline void funlockfilep(FILE **f) {
17b0f1
+        funlockfile(*f);
17b0f1
+}
17b0f1
+
17b0f1
 int read_line(FILE *f, size_t limit, char **ret) {
17b0f1
         _cleanup_free_ char *buffer = NULL;
17b0f1
         size_t n = 0, allocated = 0, count = 0;
17b0f1
-        int r;
17b0f1
 
17b0f1
         assert(f);
17b0f1
 
17b0f1
@@ -827,48 +830,42 @@ int read_line(FILE *f, size_t limit, char **ret) {
17b0f1
                         return -ENOMEM;
17b0f1
         }
17b0f1
 
17b0f1
-        flockfile(f);
17b0f1
+        {
17b0f1
+                _cleanup_(funlockfilep) FILE *flocked = f;
17b0f1
+                flockfile(f);
17b0f1
 
17b0f1
-        for (;;) {
17b0f1
-                int c;
17b0f1
+                for (;;) {
17b0f1
+                        int c;
17b0f1
 
17b0f1
-                if (n >= limit) {
17b0f1
-                        funlockfile(f);
17b0f1
-                        return -ENOBUFS;
17b0f1
-                }
17b0f1
+                        if (n >= limit)
17b0f1
+                                return -ENOBUFS;
17b0f1
+
17b0f1
+                        errno = 0;
17b0f1
+                        c = fgetc_unlocked(f);
17b0f1
+                        if (c == EOF) {
17b0f1
+                                /* if we read an error, and have no data to return, then propagate the error */
17b0f1
+                                if (ferror_unlocked(f) && n == 0)
17b0f1
+                                        return errno > 0 ? -errno : -EIO;
17b0f1
 
17b0f1
-                errno = 0;
17b0f1
-                c = fgetc_unlocked(f);
17b0f1
-                if (c == EOF) {
17b0f1
-                        /* if we read an error, and have no data to return, then propagate the error */
17b0f1
-                        if (ferror_unlocked(f) && n == 0) {
17b0f1
-                                r = errno > 0 ? -errno : -EIO;
17b0f1
-                                funlockfile(f);
17b0f1
-                                return r;
17b0f1
+                                break;
17b0f1
                         }
17b0f1
 
17b0f1
-                        break;
17b0f1
-                }
17b0f1
+                        count++;
17b0f1
 
17b0f1
-                count++;
17b0f1
+                        if (IN_SET(c, '\n', 0)) /* Reached a delimiter */
17b0f1
+                                break;
17b0f1
 
17b0f1
-                if (IN_SET(c, '\n', 0)) /* Reached a delimiter */
17b0f1
-                        break;
17b0f1
+                        if (ret) {
17b0f1
+                                if (!GREEDY_REALLOC(buffer, allocated, n + 2))
17b0f1
+                                        return -ENOMEM;
17b0f1
 
17b0f1
-                if (ret) {
17b0f1
-                        if (!GREEDY_REALLOC(buffer, allocated, n + 2)) {
17b0f1
-                                funlockfile(f);
17b0f1
-                                return -ENOMEM;
17b0f1
+                                buffer[n] = (char) c;
17b0f1
                         }
17b0f1
 
17b0f1
-                        buffer[n] = (char) c;
17b0f1
+                        n++;
17b0f1
                 }
17b0f1
-
17b0f1
-                n++;
17b0f1
         }
17b0f1
 
17b0f1
-        funlockfile(f);
17b0f1
-
17b0f1
         if (ret) {
17b0f1
                 buffer[n] = 0;
17b0f1