From 914ddc68511be45c8de9fa7f7e4e468fed05b88d Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Thu, 14 Mar 2024 15:35:55 -0400 Subject: Bugfix * When the result string grew larger than the initial_size, the function would return NULL even if realloc succeeded * Replaces fgets with fread to ensure all data returned by the program is consumed. * The line buffer is zeroed for each iteration --- src/system.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/system.c') diff --git a/src/system.c b/src/system.c index 43236a1..bfce59e 100644 --- a/src/system.c +++ b/src/system.c @@ -152,7 +152,9 @@ char *shell_output(const char *command, int *status) { *status = 1; } result = calloc(initial_size, sizeof(result)); - while (fgets(line, sizeof(line) - 1, pp) != NULL) { + memset(line, 0, sizeof(line)); + while (fread(line, sizeof(char), sizeof(line) - 1, pp) != 0) { + //while (fgets(line, sizeof(line) - 1, pp) != NULL) { size_t result_len = strlen(result); size_t need_realloc = (result_len + strlen(line)) > current_size; if (need_realloc) { @@ -160,14 +162,13 @@ char *shell_output(const char *command, int *status) { char *tmp = realloc(result, sizeof(*result) * current_size); if (!tmp) { return NULL; - } else if (tmp != result) { + } + if (tmp != result) { result = tmp; - } else { - fprintf(SYSERROR); - return NULL; } } strcat(result, line); + memset(line, 0, sizeof(line)); } pclose(pp); return result; -- cgit