From ad82be9beabfc3346c72095db558d5be629891e5 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 18 May 2023 11:17:46 +0200 Subject: fix realloc usage Remember, this is always a mistake: ptr = realloc(ptr, size); You still need to free() the original ptr if realloc fails. --- src/file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/file.c b/src/file.c index bdc0e22..4aa0003 100644 --- a/src/file.c +++ b/src/file.c @@ -69,11 +69,12 @@ char *my_readlink(const char *path) char *buf = NULL; while (1) { - buf = realloc(buf, current_size); - if (!buf) { + char *tmp_buf = realloc(buf, current_size); + if (!tmp_buf) { log_errno("realloc"); goto free; } + buf = tmp_buf; ssize_t res = readlink(path, buf, current_size); if (res < 0) { @@ -111,9 +112,13 @@ int file_read(int fd, char **_contents, size_t *_size) size_t size = 0; while (1) { - contents = realloc(contents, alloc_size); - if (!contents) + char *tmp_contents = realloc(contents, alloc_size); + if (!tmp_contents) { + log_errno("realloc"); + free(contents); return -1; + } + contents = tmp_contents; ssize_t read_size = read(fd, contents + size, alloc_size - size - 1); -- cgit v1.2.3