From: Lennart Poettering Date: Fri, 10 Feb 2012 23:27:12 +0000 (+0100) Subject: util: fix handling of empty files in read_one_line_file() X-Git-Tag: v42~5 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4099a281bb1e7bbb941c55de559dbfb9abf5897b;p=systemd util: fix handling of empty files in read_one_line_file() https://bugs.freedesktop.org/show_bug.cgi?id=45362 --- diff --git a/src/util.c b/src/util.c index 11f77abd..33299229 100644 --- a/src/util.c +++ b/src/util.c @@ -705,15 +705,22 @@ int read_one_line_file(const char *fn, char **line) { assert(fn); assert(line); - if (!(f = fopen(fn, "re"))) + f = fopen(fn, "re"); + if (!f) return -errno; - if (!(fgets(t, sizeof(t), f))) { - r = feof(f) ? -EIO : -errno; - goto finish; + if (!fgets(t, sizeof(t), f)) { + + if (ferror(f)) { + r = -errno; + goto finish; + } + + t[0] = 0; } - if (!(c = strdup(t))) { + c = strdup(t); + if (!c) { r = -ENOMEM; goto finish; }