From dec6933405df0155a60c376e5345f71250b649b0 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 7 Jul 2025 17:31:55 -0400 Subject: Convert sprintf to snprintf --- src/lib/core/environment.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/lib/core') diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index f5e8566..407c76c 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -106,14 +106,13 @@ void runtime_export(RuntimeEnv *env, char **keys) { if (keys != NULL) { for (size_t j = 0; keys[j] != NULL; j++) { if (strcmp(keys[j], key) == 0) { - //sprintf(output, "%s=\"%s\"\n%s %s", key, value ? value : "", export_command, key); - sprintf(output, "%s %s=\"%s\"", export_command, key, value ? value : ""); + snprintf(output, sizeof(output), "%s %s=\"%s\"", export_command, key, value ? value : ""); puts(output); } } } else { - sprintf(output, "%s %s=\"%s\"", export_command, key, value ? value : ""); + snprintf(output, sizeof(output), "%s %s=\"%s\"", export_command, key, value ? value : ""); puts(output); } guard_free(value); -- cgit From c3907905403c783aca3a02ca38cde6dc9f1a7cb7 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 7 Jul 2025 17:32:06 -0400 Subject: Grammar --- src/lib/core/environment.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib/core') diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index 407c76c..cb5a7a3 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -177,7 +177,7 @@ int runtime_replace(RuntimeEnv **dest, char **src) { } /** - * Determine whether or not a key exists in the runtime environment + * Determine whether a key exists in the runtime environment * * Example: * -- cgit From 6447853b3cf1cdf327758d847a09733e45e28dad Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 7 Jul 2025 17:38:16 -0400 Subject: Memory safety --- src/lib/core/environment.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/lib/core') diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index cb5a7a3..bd3ac1b 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -244,7 +244,14 @@ char *runtime_get(RuntimeEnv *env, const char *key) { ssize_t key_offset = runtime_contains(env, key); if (key_offset != -1) { char **pair = split(strlist_item(env, key_offset), "=", 0); + if (!pair) { + return NULL; + } result = join(&pair[1], "="); + if (!result) { + guard_array_free(pair); + return NULL; + } guard_array_free(pair); } return result; @@ -347,9 +354,7 @@ char *runtime_expand_var(RuntimeEnv *env, char *input) { } // Append expanded environment variable to output strncat(expanded, tmp, STASIS_BUFSIZ - 1); - if (env) { - guard_free(tmp); - } + guard_free(tmp); } // Nothing to do so append input to output @@ -403,9 +408,22 @@ void runtime_set(RuntimeEnv *env, const char *_key, char *_value) { return; } char *key = strdup(_key); - ssize_t key_offset = runtime_contains(env, key); + if (!key) { + SYSERROR("%s", "unable to allocate memory for key"); + exit(1); + } char *value = runtime_expand_var(env, _value); - char *now = join((char *[]) {key, value, NULL}, "="); + if (!value) { + SYSERROR("%s", "unable to allocate memory for value"); + exit(1); + } + + lstrip(value); + char *now = join((char *[]) {key, value, NULL}, sep); + if (!now) { + SYSERROR("%s", "unable to allocate memory for join"); + exit(1); + } if (key_offset < 0) { strlist_append(&env, now); @@ -423,6 +441,10 @@ 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); + if (!pair) { + SYSERROR("%s", "unable to allocate memory for runtime_apply"); + return; + } setenv(pair[0], pair[1], 1); guard_array_free(pair); } -- cgit From de3e615c1971d844b8ab0c44332fa18466dca150 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 7 Jul 2025 17:38:57 -0400 Subject: Separator as variable --- src/lib/core/environment.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/lib/core') diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index bd3ac1b..057d89e 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -404,6 +404,7 @@ char *runtime_expand_var(RuntimeEnv *env, char *input) { * @param _value New environment variable value */ void runtime_set(RuntimeEnv *env, const char *_key, char *_value) { + const char *sep = "="; if (_key == NULL) { return; } -- cgit From fada0caa442c433a133735c79fd697f52696b705 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 7 Jul 2025 17:39:17 -0400 Subject: Return a copy of the input, not the input pointer --- src/lib/core/environment.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/lib/core') diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index 057d89e..79dd559 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -291,8 +291,7 @@ char *runtime_expand_var(RuntimeEnv *env, char *input) { // If there's no environment variables to process return the input string if (strchr(input, delim) == NULL) { - //return strdup(input); - return input; + return strdup(input); } expanded = calloc(STASIS_BUFSIZ, sizeof(char)); -- cgit From dc6bbddab79bab20c5c2d2994cf6eae66bcd767e Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 7 Jul 2025 17:39:55 -0400 Subject: Use a copy of the environment variable if its present --- src/lib/core/environment.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/lib/core') diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index 79dd559..02bbf82 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -341,7 +341,10 @@ char *runtime_expand_var(RuntimeEnv *env, char *input) { if (env) { tmp = runtime_get(env, var); } else { - tmp = getenv(var); + const char *v = getenv(var); + if (v) { + tmp = strdup(v); + } } if (tmp == NULL) { // This mimics shell behavior in general. -- cgit From b02d868a4f74194f910f8ce173cb5586bc62a31a Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 7 Jul 2025 17:40:28 -0400 Subject: Offset is constant --- src/lib/core/environment.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/lib/core') diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index 02bbf82..39d8116 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -410,6 +410,7 @@ void runtime_set(RuntimeEnv *env, const char *_key, char *_value) { if (_key == NULL) { return; } + const ssize_t key_offset = runtime_contains(env, _key); char *key = strdup(_key); if (!key) { SYSERROR("%s", "unable to allocate memory for key"); -- cgit From 7f8e8c82da33e64bd0a1c4376ea7bc184bded3ec Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 7 Jul 2025 19:06:27 -0400 Subject: Fix leak --- src/lib/core/environment.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/lib/core') diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index 39d8116..7ece5e6 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -436,6 +436,7 @@ void runtime_set(RuntimeEnv *env, const char *_key, char *_value) { } guard_free(now); guard_free(key); + guard_free(value); } /** -- cgit From 76f50b85171ef007672a112da6faeeb4bb35da03 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 8 Jul 2025 13:54:26 -0400 Subject: Disable noisy debug statements --- src/lib/core/template.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/lib/core') diff --git a/src/lib/core/template.c b/src/lib/core/template.c index 68d20c9..dd3c7a2 100644 --- a/src/lib/core/template.c +++ b/src/lib/core/template.c @@ -280,8 +280,8 @@ char *tpl_render(char *str) { output[z] = pos[off]; z++; } - SYSDEBUG("template output length: %zu", strlen(output)); - SYSDEBUG("template output bytes: %zu", output_bytes); + //SYSDEBUG("template output length: %zu", strlen(output)); + //SYSDEBUG("template output bytes: %zu", output_bytes); return output; } -- cgit From 83f9592c4bc0f8e5a6a8e73ba9e3daa1da107262 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 8 Jul 2025 13:57:10 -0400 Subject: Write error message correctly to the buffer * If errmsg is NULL, allocate and write string * If errmsg is not NULL, write string --- src/lib/core/download.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/lib/core') diff --git a/src/lib/core/download.c b/src/lib/core/download.c index c3f8dca..b021860 100644 --- a/src/lib/core/download.c +++ b/src/lib/core/download.c @@ -41,10 +41,10 @@ long download(char *url, const char *filename, char **errmsg) { CURLcode curl_code = curl_easy_perform(c); SYSDEBUG("curl status code: %d", curl_code); if (curl_code != CURLE_OK) { - if (errmsg) { - strcpy(*errmsg, curl_easy_strerror(curl_code)); + if (!*errmsg) { + *errmsg = strdup(curl_easy_strerror(curl_code)); } else { - fprintf(stderr, "\nCURL ERROR: %s\n", curl_easy_strerror(curl_code)); + strncpy(*errmsg, curl_easy_strerror(curl_code), strlen(curl_easy_strerror(curl_code) + 1)); } goto failed; } -- cgit From 1fd91d5449004c8d02c58c05b9a303f520ee746e Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 8 Jul 2025 13:58:26 -0400 Subject: Bugfix: HTTP_ERROR did not check for negative values --- src/lib/core/include/core.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib/core') diff --git a/src/lib/core/include/core.h b/src/lib/core/include/core.h index 35a9506..92969d2 100644 --- a/src/lib/core/include/core.h +++ b/src/lib/core/include/core.h @@ -15,7 +15,7 @@ #define STASIS_NAME_MAX 255 #define STASIS_DIRSTACK_MAX 1024 #define STASIS_TIME_STR_MAX 128 -#define HTTP_ERROR(X) X >= 400 +#define HTTP_ERROR(X) (X >= 400 || X < 0) #include "config.h" #include "core_mem.h" -- cgit From 783a11a8537bc4fc6f211649c33d7e22e54d2dc0 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 8 Jul 2025 13:58:43 -0400 Subject: Free errmsg after use --- src/lib/core/conda.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/lib/core') diff --git a/src/lib/core/conda.c b/src/lib/core/conda.c index 268b433..de6130f 100644 --- a/src/lib/core/conda.c +++ b/src/lib/core/conda.c @@ -28,6 +28,7 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) { const long http_code = download(url, installer_path, &errmsg); if (HTTP_ERROR(http_code)) { fprintf(stderr, "download failed: %ld: %s\n", http_code, errmsg); + guard_free(errmsg); return -1; } } -- cgit