naccyde / rpms / systemd

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