summaryrefslogtreecommitdiff
path: root/src/basic/fileio.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2017-09-22 21:03:33 +0200
committerLennart Poettering <lennart@poettering.net>2017-09-22 21:03:33 +0200
commitff0e7e05c9904b4120868e96216b123c1b798aa4 (patch)
treedd76fba9a62135d4b0356cbc6d654b22b8148a60 /src/basic/fileio.c
parent9dd1b1e8694c3cbfaf64bc9d93ff6e7a61694a49 (diff)
downloadsystemd-ff0e7e05c9904b4120868e96216b123c1b798aa4.tar.gz
systemd-ff0e7e05c9904b4120868e96216b123c1b798aa4.tar.bz2
systemd-ff0e7e05c9904b4120868e96216b123c1b798aa4.zip
fileio: try to read one byte too much in read_full_stream()
Let's read one byte more than the file size we read from stat() on the first fread() invocation. That way, the first read() will already be short and indicate eof to fread(). This is a minor optimization, and replaces #3908.
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r--src/basic/fileio.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 7d69fae709..e7cd03b939 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -270,11 +270,11 @@ int read_full_stream(FILE *f, char **contents, size_t *size) {
if (st.st_size > READ_FULL_BYTES_MAX)
return -E2BIG;
- /* Start with the right file size, but be prepared for
- * files from /proc which generally report a file size
- * of 0 */
+ /* Start with the right file size, but be prepared for files from /proc which generally report a file
+ * size of 0. Note that we increase the size to read here by one, so that the first read attempt
+ * already makes us notice the EOF. */
if (st.st_size > 0)
- n = st.st_size;
+ n = st.st_size + 1;
}
l = 0;