diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-09-16 12:20:09 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2021-09-16 12:23:25 +0200 |
commit | 00bd9a4a82ed57bc0c7f158da4564fc1eab808b4 (patch) | |
tree | 59dfc058383279c2876f076fcb55a482894a0c79 /src/basic/fileio.c | |
parent | journal,network,timesync: fix segfault on 32bit timeval/timespec systems (diff) | |
download | systemd-00bd9a4a82ed57bc0c7f158da4564fc1eab808b4.tar.xz systemd-00bd9a4a82ed57bc0c7f158da4564fc1eab808b4.zip |
fileio: fix truncated read handling in read_virtual_file()
We mishandled the case where the size we read from the file actually
matched the maximum size fully. In that case we cannot really make a
determination whether the file was fully read or only partially. In that
case let's do another loop, so that we operate with a buffer, and
we can detect the EOF (which will be signalled to us via a short read).
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r-- | src/basic/fileio.c | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c index e69b888315..60a9885108 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -470,9 +470,14 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents if (n <= size) break; - /* If a maximum size is specified and we already read as much, no need to try again */ - if (max_size != SIZE_MAX && n >= max_size) { - n = max_size; + /* If a maximum size is specified and we already read more we know the file is larger, and + * can handle this as truncation case. Note that if the size of what we read equals the + * maximum size then this doesn't mean truncation, the file might or might not end on that + * byte. We need to rerun the loop in that case, with a larger buffer size, so that we read + * at least one more byte to be able to distinguish EOF from truncation. */ + if (max_size != SIZE_MAX && n > max_size) { + n = size; /* Make sure we never use more than what we sized the buffer for (so that + * we have one free byte in it for the trailing NUL we add below).*/ truncated = true; break; } |