From f858e5148e4f36335555dfaac812197ebd3ef036 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sat, 23 Sep 2017 10:48:09 +0200 Subject: [PATCH] fileio: use _cleanup_ for FILE unlocking --- src/basic/fileio.c | 57 ++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 4d0a295..ae5536a 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -1513,10 +1513,13 @@ int mkdtemp_malloc(const char *template, char **ret) { return 0; } +static inline void funlockfilep(FILE **f) { + funlockfile(*f); +} + int read_line(FILE *f, size_t limit, char **ret) { _cleanup_free_ char *buffer = NULL; size_t n = 0, allocated = 0, count = 0; - int r; assert(f); @@ -1538,48 +1541,42 @@ int read_line(FILE *f, size_t limit, char **ret) { return -ENOMEM; } - flockfile(f); + { + _cleanup_(funlockfilep) FILE *flocked = f; + flockfile(f); - for (;;) { - int c; + for (;;) { + int c; - if (n >= limit) { - funlockfile(f); - return -ENOBUFS; - } + if (n >= limit) + return -ENOBUFS; - errno = 0; - c = fgetc_unlocked(f); - if (c == EOF) { - /* if we read an error, and have no data to return, then propagate the error */ - if (ferror_unlocked(f) && n == 0) { - r = errno > 0 ? -errno : -EIO; - funlockfile(f); - return r; + errno = 0; + c = fgetc_unlocked(f); + if (c == EOF) { + /* if we read an error, and have no data to return, then propagate the error */ + if (ferror_unlocked(f) && n == 0) + return errno > 0 ? -errno : -EIO; + + break; } - break; - } + count++; - count++; + if (IN_SET(c, '\n', 0)) /* Reached a delimiter */ + break; - if (IN_SET(c, '\n', 0)) /* Reached a delimiter */ - break; + if (ret) { + if (!GREEDY_REALLOC(buffer, allocated, n + 2)) + return -ENOMEM; - if (ret) { - if (!GREEDY_REALLOC(buffer, allocated, n + 2)) { - funlockfile(f); - return -ENOMEM; + buffer[n] = (char) c; } - buffer[n] = (char) c; + n++; } - - n++; } - funlockfile(f); - if (ret) { buffer[n] = 0; -- 2.7.4