From 70c1ba3962166853fc7a1e4f2bb1d637104312b1 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 22 Jun 2026 11:52:45 -0400 Subject: General fixes (#149) * Fix missing LF * Return when item is NULL * Explicitly state error condition * strlist_contains: Allow index result argument to be NULL * BUGFIX: strlist_contains now uses strstr instead of strcmp * The match was never intended to be exact * BUGFIX: log_msgv now prints a LF to the same stream as the message * BUGFIX: env_manipulate_pathstr PM_ONCE logic did opposite of what was intended * When strstr finds the path in the system path, it's supposed to free the new system path and exit. In this case it was freeing the path when the path wasn't found that lead to several instances of PATH not being updated correctly * Free result when shell_output fails * join: Fix segfault on memory error * join_ex: Fix leak on memory error * Initialize installer_url and python_override_version to zero * Remove redundant calls to memset --- src/cli/stasis/stasis_main.c | 21 +++++---------------- src/lib/core/environment.c | 9 +++++++-- src/lib/core/log.c | 2 +- src/lib/core/str.c | 9 +++++++++ src/lib/core/strlist.c | 10 +++++++--- src/lib/core/utils.c | 3 ++- tests/test_version_compare.c | 2 +- 7 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/cli/stasis/stasis_main.c b/src/cli/stasis/stasis_main.c index e660f6b..ef7bf26 100644 --- a/src/cli/stasis/stasis_main.c +++ b/src/cli/stasis/stasis_main.c @@ -521,32 +521,21 @@ static void transfer_artifacts(struct Delivery *ctx) { } } -int main(int argc, char *argv[]) { - struct Delivery ctx; - struct Process proc = { - .f_stdout = "", - .f_stderr = "", - .redirect_stderr = 0, - }; +int main(const int argc, char *argv[]) { + struct Delivery ctx = {0}; + char env_name[STASIS_NAME_MAX] = {0}; char env_name_testing[STASIS_NAME_MAX] = {0}; char *delivery_input = NULL; char *config_input = NULL; - char installer_url[PATH_MAX]; - char python_override_version[STASIS_NAME_MAX]; + char installer_url[PATH_MAX] = {0}; + char python_override_version[STASIS_NAME_MAX] = {0}; int user_disabled_docker = false; globals.cpu_limit = get_cpu_count(); if (globals.cpu_limit > 1) { globals.cpu_limit--; // max - 1 } - memset(env_name, 0, sizeof(env_name)); - memset(env_name_testing, 0, sizeof(env_name_testing)); - memset(installer_url, 0, sizeof(installer_url)); - memset(python_override_version, 0, sizeof(python_override_version)); - memset(&proc, 0, sizeof(proc)); - memset(&ctx, 0, sizeof(ctx)); - setup_sysconfdir(); int c; diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index 4623db8..b4ab66e 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -445,9 +445,14 @@ void runtime_set(RuntimeEnv *env, const char *_key, char *_value) { */ void runtime_apply(RuntimeEnv *env) { for (size_t i = 0; i < strlist_count(env); i++) { - char **pair = split(strlist_item(env, i), "=", 1); + const char *item = strlist_item(env, i); + if (!item) { + SYSERROR("failed to read from env list"); + return; + } + char **pair = split((char *) item, "=", 1); if (!pair) { - SYSERROR("unable to allocate memory for runtime_apply"); + SYSERROR("unable to allocate memory for key/value pair"); return; } setenv(pair[0], pair[1], 1); diff --git a/src/lib/core/log.c b/src/lib/core/log.c index b1cab4c..8f24702 100644 --- a/src/lib/core/log.c +++ b/src/lib/core/log.c @@ -89,7 +89,7 @@ int log_msgv(FILE *stream, const struct ExecPoint ep, const char *preface_color, SYSERROR("\nvfprintf failed"); return len; } - fprintf(stderr, LINE_SEP); + fprintf(stream, LINE_SEP); return len; } diff --git a/src/lib/core/str.c b/src/lib/core/str.c index a04293f..84a325b 100644 --- a/src/lib/core/str.c +++ b/src/lib/core/str.c @@ -171,6 +171,10 @@ char *join(char **arr, const char *separator) { total_bytes += (records * strlen(separator)) + 1; result = (char *)calloc(total_bytes, sizeof(char)); + if (!result) { + return NULL; + } + for (int i = 0; i < records; i++) { safe_strncat(result, arr[i], total_bytes); if (i < (records - 1)) { @@ -225,6 +229,11 @@ char *join_ex(char *separator, ...) { // Generate output string result = calloc(size + 1, sizeof(char)); + if (!result) { + guard_array_free_by_count(argv, argc); + return NULL; + } + for (size_t i = 0; i < argc; i++) { // Append argument to string safe_strncat(result, argv[i], size + 1); // no -1 because +1 above diff --git a/src/lib/core/strlist.c b/src/lib/core/strlist.c index 60f3a1f..e209bdf 100644 --- a/src/lib/core/strlist.c +++ b/src/lib/core/strlist.c @@ -171,11 +171,15 @@ int strlist_contains(struct StrList *pStrList, const char *value, size_t *index_ for (size_t i = 0; i < strlist_count(pStrList); i++) { const char *item = strlist_item(pStrList, i); if (!item) { - *index_of = 0; + if (index_of) { + *index_of = 0; + } break; } - if (!strcmp(item, value)) { - *index_of = i; + if (strstr(item, value)) { + if (index_of) { + *index_of = i; + } return 1; } } diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index 152c5c5..31208ad 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -611,6 +611,7 @@ int xml_pretty_print_in_place(const char *filename, const char *pretty_print_pro snprintf(cmd, sizeof(cmd), "%s %s %s", pretty_print_prog, pretty_print_args, filename); result = shell_output(cmd, &status); if (status || !result) { + guard_free(result); return status; } @@ -920,7 +921,7 @@ int env_manipulate_pathstr(const char *key, char *path, int mode) { } if (mode & PM_ONCE) { - if (!strstr(system_path_old, path)) { + if (strstr(system_path_old, path)) { guard_free(system_path_new); return 0; } diff --git a/tests/test_version_compare.c b/tests/test_version_compare.c index 2a3458f..1502b38 100644 --- a/tests/test_version_compare.c +++ b/tests/test_version_compare.c @@ -147,7 +147,7 @@ void run_cases_version_compare(void) { result = version_compare(op, test->a, test->b); STASIS_ASSERT(test->expected == result, "unexpected result"); - fprintf(stderr, "'%s' '%s' '%s' is %s (%d)", + fprintf(stderr, "'%s' '%s' '%s' is %s (%d)\n", test->a ? test->a : "NULL", test->op ? test->op : "NULL", test->b ? test->b : "NULL", -- cgit