]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fileio: try to read one byte too much in read_full_stream()
authorLennart Poettering <lennart@poettering.net>
Fri, 22 Sep 2017 19:03:33 +0000 (21:03 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 22 Sep 2017 19:03:33 +0000 (21:03 +0200)
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.

src/basic/fileio.c

index 7d69fae709037a7c9b76d1cbce486c54a2b6da53..e7cd03b939f406f0211e78de2efa267c5c278a55 100644 (file)
@@ -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;