util: fix handling of empty files in read_one_line_file()

https://bugs.freedesktop.org/show_bug.cgi?id=45362
This commit is contained in:
Lennart Poettering 2012-02-11 00:27:12 +01:00
parent 89f134406a
commit 4099a281bb
1 changed files with 12 additions and 5 deletions

View File

@ -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;
}