ryantimwilson / rpms / systemd

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