diff options
| author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2025-07-08 15:50:20 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-08 15:50:20 -0400 |
| commit | d90493618ce34a732c5411d1670be57d4dd9db4e (patch) | |
| tree | 99719cf91d7faaf8ad37ef70d3baaaf3ce316176 /src/lib/core | |
| parent | e90140a7628d24edef090e548179c64170d3338b (diff) | |
| parent | 1e572f3a653ddd6385a0ea49cacfbf2f3a32ce87 (diff) | |
| download | stasis-d90493618ce34a732c5411d1670be57d4dd9db4e.tar.gz | |
Merge pull request #116 from jhunkeler/breakdown
Breakdown & Bug fixes
Diffstat (limited to 'src/lib/core')
| -rw-r--r-- | src/lib/core/conda.c | 1 | ||||
| -rw-r--r-- | src/lib/core/download.c | 6 | ||||
| -rw-r--r-- | src/lib/core/environment.c | 50 | ||||
| -rw-r--r-- | src/lib/core/include/core.h | 2 | ||||
| -rw-r--r-- | src/lib/core/template.c | 4 |
5 files changed, 45 insertions, 18 deletions
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; } } 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; } diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index f5e8566..7ece5e6 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); @@ -178,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: * @@ -245,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; @@ -285,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)); @@ -336,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. @@ -348,9 +356,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 @@ -400,13 +406,28 @@ 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; } + const ssize_t key_offset = runtime_contains(env, _key); 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); @@ -415,6 +436,7 @@ void runtime_set(RuntimeEnv *env, const char *_key, char *_value) { } guard_free(now); guard_free(key); + guard_free(value); } /** @@ -424,6 +446,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); } 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" 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; } |
