]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fileio: minor read_full_stream_full() optimization
authorLennart Poettering <lennart@poettering.net>
Thu, 4 Mar 2021 14:59:46 +0000 (15:59 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 4 Mar 2021 17:55:02 +0000 (18:55 +0100)
If we shall read as much of a file/stream as we can, then it makes sense
to use the full malloc()ed memory, not just the part we asked for.

src/basic/fileio.c

index 15f0ffbdfa7b34db418fa0483c3b991063ecc47e..596946ccf4982d6a6fc87e23413bdea34b48e2e5 100644 (file)
@@ -548,7 +548,9 @@ int read_full_stream_full(
                 }
 
                 buf = t;
-                n = n_next;
+                /* Unless a size has been explicitly specified, try to read as much as fits into the memory
+                 * we allocated (minus 1, to leave one byte for the safety NUL byte) */
+                n = size == SIZE_MAX ? malloc_usable_size(buf) - 1 : n_next;
 
                 errno = 0;
                 k = fread(buf + l, 1, n - l, f);