ryantimwilson / rpms / systemd

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