aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-03-14 15:35:55 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-03-14 15:35:55 -0400
commit914ddc68511be45c8de9fa7f7e4e468fed05b88d (patch)
tree16f0216aa9c59f9cd3f055be2381cea988f0a2e8 /src
parent045fe106f59207e12ba6aa3f2e3d1c4f3f1f8838 (diff)
downloadstasis-914ddc68511be45c8de9fa7f7e4e468fed05b88d.tar.gz
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
Diffstat (limited to 'src')
-rw-r--r--src/system.c11
1 files changed, 6 insertions, 5 deletions
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;