]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fileio: read_virtual_file(): exit once the max_size limit is reached
authorLennart Poettering <lennart@poettering.net>
Fri, 21 May 2021 14:31:52 +0000 (16:31 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 21 May 2021 19:54:19 +0000 (21:54 +0200)
Alternative to: #19682

src/basic/fileio.c

index 7bb1e8a8c6ec812c591713d9b2844f0884a3afe1..cb8f5b0b1841cfc228072629bbe92b638bcbbca8 100644 (file)
@@ -450,6 +450,13 @@ 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;
+                        truncated = true;
+                        break;
+                }
+
                 /* Hmm... either we read too few bytes from /proc or less likely the content
                  * of the file might have been changed (and is now bigger) while we were
                  * processing, let's try again either with a bigger guessed size or the new