1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-19 22:50:17 +03:00

fileio: don't use realloc() in read_full_virtual_file()

We aren't interested in the data previousl read, hence free() followed
by malloc() is typically better since it means libc doesn't have to
restore the contained data needlessly.
This commit is contained in:
Lennart Poettering 2021-03-17 18:46:56 +01:00
parent be81e45c74
commit b235b03138

View File

@ -368,7 +368,6 @@ int read_full_virtual_file(const char *filename, char **ret_contents, size_t *re
struct stat st;
size_t n, size;
int n_retries;
char *p;
assert(ret_contents);
@ -413,10 +412,9 @@ int read_full_virtual_file(const char *filename, char **ret_contents, size_t *re
if (size > READ_FULL_BYTES_MAX)
return -E2BIG;
p = realloc(buf, size + 1);
if (!p)
buf = malloc(size + 1);
if (!buf)
return -ENOMEM;
buf = TAKE_PTR(p);
for (;;) {
ssize_t k;
@ -445,12 +443,18 @@ int read_full_virtual_file(const char *filename, char **ret_contents, size_t *re
if (lseek(fd, 0, SEEK_SET) < 0)
return -errno;
buf = mfree(buf);
}
if (n < size) {
char *p;
/* Return rest of the buffer to libc */
p = realloc(buf, n + 1);
if (!p)
return -ENOMEM;
buf = TAKE_PTR(p);
}