aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-04-02 13:44:10 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-04-02 18:55:46 -0400
commit977399bb759aacd2a18ba76df78078ee4cee6e67 (patch)
treee1436edde5df9f092ee6c43f48b02c76391c652d
parentcfdf0e333d240526436aff5f886a1da6a0959bce (diff)
downloadstasis-977399bb759aacd2a18ba76df78078ee4cee6e67.tar.gz
Changes to strlist_*() functions:
Functions that modify the input StrList have been refactored to use `struct StrList **` instead of `struct StrList *`. * Fixes realloc error handling
-rw-r--r--include/strlist.h6
-rw-r--r--src/conda.c4
-rw-r--r--src/deliverable.c20
-rw-r--r--src/environment.c8
-rw-r--r--src/strlist.c92
5 files changed, 71 insertions, 59 deletions
diff --git a/include/strlist.h b/include/strlist.h
index d76d069..2d3c3cf 100644
--- a/include/strlist.h
+++ b/include/strlist.h
@@ -31,17 +31,17 @@ unsigned char strlist_item_as_uchar(struct StrList *pStrList, size_t index);
char strlist_item_as_char(struct StrList *pStrList, size_t index);
char *strlist_item_as_str(struct StrList *pStrList, size_t index);
char *strlist_item(struct StrList *pStrList, size_t index);
-void strlist_set(struct StrList *pStrList, size_t index, char *value);
+void strlist_set(struct StrList **pStrList, size_t index, char *value);
size_t strlist_count(struct StrList *pStrList);
void strlist_reverse(struct StrList *pStrList);
void strlist_sort(struct StrList *pStrList, unsigned int mode);
int strlist_append_file(struct StrList *pStrList, char *path, ReaderFn *readerFn);
void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2);
-void strlist_append(struct StrList *pStrList, char *str);
+void strlist_append(struct StrList **pStrList, char *str);
void strlist_append_array(struct StrList *pStrList, char **arr);
void strlist_append_tokenize(struct StrList *pStrList, char *str, char *delim);
struct StrList *strlist_copy(struct StrList *pStrList);
int strlist_cmp(struct StrList *a, struct StrList *b);
-void strlist_free(struct StrList *pStrList);
+void strlist_free(struct StrList **pStrList);
#endif //OMC_STRLIST_H
diff --git a/src/conda.c b/src/conda.c
index 332049d..8b370a8 100644
--- a/src/conda.c
+++ b/src/conda.c
@@ -186,11 +186,11 @@ int conda_check_required() {
}
if (found < (sizeof(tools) / sizeof(*tools)) - 1) {
guard_free(cmd_out);
- guard_strlist_free(result);
+ guard_strlist_free(&result);
return 1;
}
guard_free(cmd_out);
- guard_strlist_free(result);
+ guard_strlist_free(&result);
} else {
msg(OMC_MSG_ERROR | OMC_MSG_L2, "The base package requirement check could not be performed\n");
return 2;
diff --git a/src/deliverable.c b/src/deliverable.c
index fb159f7..3d638cb 100644
--- a/src/deliverable.c
+++ b/src/deliverable.c
@@ -184,13 +184,13 @@ void delivery_free(struct Delivery *ctx) {
guard_free(ctx->deploy.docker.test_script);
guard_free(ctx->deploy.docker.registry);
guard_free(ctx->deploy.docker.image_compression);
- guard_strlist_free(ctx->deploy.docker.tags);
- guard_strlist_free(ctx->deploy.docker.build_args);
+ guard_strlist_free(&ctx->deploy.docker.tags);
+ guard_strlist_free(&ctx->deploy.docker.build_args);
for (size_t i = 0; i < sizeof(ctx->deploy.jfrog) / sizeof(ctx->deploy.jfrog[0]); i++) {
guard_free(ctx->deploy.jfrog[i].repo);
guard_free(ctx->deploy.jfrog[i].dest);
- guard_strlist_free(ctx->deploy.jfrog[i].files);
+ guard_strlist_free(&ctx->deploy.jfrog[i].files);
}
}
@@ -887,7 +887,7 @@ struct StrList *delivery_build_wheels(struct Delivery *ctx) {
{
if (python_exec("-m build -w ")) {
fprintf(stderr, "failed to generate wheel package for %s-%s\n", ctx->tests[i].name, ctx->tests[i].version);
- guard_strlist_free(result);
+ strlist_free(&result);
return NULL;
} else {
DIR *dp;
@@ -895,13 +895,13 @@ struct StrList *delivery_build_wheels(struct Delivery *ctx) {
dp = opendir("dist");
if (!dp) {
fprintf(stderr, "wheel artifact directory does not exist: %s\n", ctx->storage.wheel_artifact_dir);
- guard_strlist_free(result);
+ strlist_free(&result);
return NULL;
}
while ((rec = readdir(dp)) != NULL) {
if (strstr(rec->d_name, ctx->tests[i].name)) {
- strlist_append(result, rec->d_name);
+ strlist_append(&result, rec->d_name);
}
}
closedir(dp);
@@ -1222,10 +1222,10 @@ void delivery_defer_packages(struct Delivery *ctx, int type) {
}
printf("BUILD FOR HOST\n");
- strlist_append(deferred, name);
+ strlist_append(&deferred, name);
} else {
printf("USE EXISTING\n");
- strlist_append(filtered, name);
+ strlist_append(&filtered, name);
}
}
@@ -1233,10 +1233,10 @@ void delivery_defer_packages(struct Delivery *ctx, int type) {
msg(OMC_MSG_WARN | OMC_MSG_L2, "No %s packages were filtered by test definitions\n", mode);
} else {
if (DEFER_CONDA == type) {
- strlist_free(ctx->conda.conda_packages);
+ strlist_free(&ctx->conda.conda_packages);
ctx->conda.conda_packages = strlist_copy(filtered);
} else if (DEFER_PIP == type) {
- strlist_free(ctx->conda.pip_packages);
+ strlist_free(&ctx->conda.pip_packages);
ctx->conda.pip_packages = strlist_copy(filtered);
}
}
diff --git a/src/environment.c b/src/environment.c
index 525e1da..824b447 100644
--- a/src/environment.c
+++ b/src/environment.c
@@ -149,7 +149,7 @@ RuntimeEnv *runtime_copy(char **env) {
rt = strlist_init();
for (size_t i = 0; i < env_count; i++) {
- strlist_append(rt, env[i]);
+ strlist_append(&rt, env[i]);
}
return rt;
}
@@ -411,9 +411,9 @@ void runtime_set(RuntimeEnv *env, const char *_key, char *_value) {
char *now = join((char *[]) {key, value, NULL}, "=");
if (key_offset < 0) {
- strlist_append(env, now);
+ strlist_append(&env, now);
} else {
- strlist_set(env, key_offset, now);
+ strlist_set(&env, key_offset, now);
}
guard_free(now);
guard_free(key);
@@ -439,5 +439,5 @@ void runtime_free(RuntimeEnv *env) {
if (env == NULL) {
return;
}
- strlist_free(env);
+ strlist_free(&env);
}
diff --git a/src/strlist.c b/src/strlist.c
index 50bf2de..f0c616e 100644
--- a/src/strlist.c
+++ b/src/strlist.c
@@ -10,19 +10,20 @@
*
* @param pStrList `StrList`
*/
-void strlist_free(struct StrList *pStrList) {
- if (pStrList == NULL) {
+void strlist_free(struct StrList **pStrList) {
+ if (!(*pStrList)) {
return;
}
- for (size_t i = 0; i < pStrList->num_inuse; i++) {
- if (pStrList->data[i]) {
- guard_free(pStrList->data[i]);
+
+ for (size_t i = 0; i < (*pStrList)->num_inuse; i++) {
+ if ((*pStrList)->data[i]) {
+ guard_free((*pStrList)->data[i]);
}
}
- if (pStrList->data) {
- guard_free(pStrList->data);
+ if ((*pStrList)->data) {
+ guard_free((*pStrList)->data);
}
- guard_free(pStrList);
+ guard_free((*pStrList));
}
/**
@@ -30,26 +31,26 @@ void strlist_free(struct StrList *pStrList) {
* @param pStrList `StrList`
* @param str
*/
-void strlist_append(struct StrList *pStrList, char *str) {
+void strlist_append(struct StrList **pStrList, char *str) {
char **tmp = NULL;
if (pStrList == NULL) {
return;
}
- tmp = realloc(pStrList->data, (pStrList->num_alloc + 1) * sizeof(char *));
+ tmp = realloc((*pStrList)->data, ((*pStrList)->num_alloc + 1) * sizeof(char *));
if (tmp == NULL) {
guard_strlist_free(pStrList);
perror("failed to append to array");
exit(1);
- } else if (tmp != pStrList->data) {
- pStrList->data = tmp;
- }
- pStrList->data[pStrList->num_inuse] = strdup(str);
- pStrList->data[pStrList->num_alloc] = NULL;
- strcpy(pStrList->data[pStrList->num_inuse], str);
- pStrList->num_inuse++;
- pStrList->num_alloc++;
+ } else if (tmp != (*pStrList)->data) {
+ (*pStrList)->data = tmp;
+ }
+ (*pStrList)->data[(*pStrList)->num_inuse] = strdup(str);
+ (*pStrList)->data[(*pStrList)->num_alloc] = NULL;
+ strcpy((*pStrList)->data[(*pStrList)->num_inuse], str);
+ (*pStrList)->num_inuse++;
+ (*pStrList)->num_alloc++;
}
static int reader_strlist_append_file(size_t lineno, char **line) {
@@ -97,7 +98,7 @@ int strlist_append_file(struct StrList *pStrList, char *_path, ReaderFn *readerF
}
for (size_t record = 0; data[record] != NULL; record++) {
- strlist_append(pStrList, data[record]);
+ strlist_append(&pStrList, data[record]);
guard_free(data[record]);
}
guard_free(data);
@@ -128,7 +129,7 @@ void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2
count = strlist_count(pStrList2);
for (size_t i = 0; i < count; i++) {
char *item = strlist_item(pStrList2, i);
- strlist_append(pStrList1, item);
+ strlist_append(&pStrList1, item);
}
}
@@ -142,7 +143,7 @@ void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2
return;
}
for (size_t i = 0; arr[i] != NULL; i++) {
- strlist_append(pStrList, arr[i]);
+ strlist_append(&pStrList, arr[i]);
}
}
@@ -161,7 +162,7 @@ void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2
token = split(str, delim, 0);
if (token) {
for (size_t i = 0; token[i] != NULL; i++) {
- strlist_append(pStrList, token[i]);
+ strlist_append(&pStrList, token[i]);
}
GENERIC_ARRAY_FREE(token);
}
@@ -173,13 +174,18 @@ void strlist_append_strlist(struct StrList *pStrList1, struct StrList *pStrList2
* @return `StrList` copy
*/
struct StrList *strlist_copy(struct StrList *pStrList) {
- struct StrList *result = strlist_init();
- if (pStrList == NULL || result == NULL) {
+ struct StrList *result;
+ if (pStrList == NULL) {
+ return NULL;
+ }
+
+ result = strlist_init();
+ if (!result) {
return NULL;
}
for (size_t i = 0; i < strlist_count(pStrList); i++) {
- strlist_append(result, strlist_item(pStrList, i));
+ strlist_append(&result, strlist_item(pStrList, i));
}
return result;
}
@@ -278,7 +284,13 @@ void strlist_reverse(struct StrList *pStrList) {
* @return
*/
size_t strlist_count(struct StrList *pStrList) {
- return pStrList->num_inuse;
+ size_t result;
+ if (pStrList != NULL) {
+ result = pStrList->num_inuse;
+ } else {
+ result = 0;
+ }
+ return result;
}
/**
@@ -287,26 +299,26 @@ size_t strlist_count(struct StrList *pStrList) {
* @param value string
* @return
*/
-void strlist_set(struct StrList *pStrList, size_t index, char *value) {
+void strlist_set(struct StrList **pStrList, size_t index, char *value) {
char *tmp = NULL;
char *item = NULL;
- if (pStrList == NULL || index > strlist_count(pStrList)) {
- return;
- }
- if ((item = strlist_item(pStrList, index)) == NULL) {
+ if (*pStrList == NULL || index > strlist_count(*pStrList)) {
return;
}
+
if (value == NULL) {
- pStrList->data[index] = NULL;
+ (*pStrList)->data[index] = NULL;
} else {
- if ((tmp = realloc(pStrList->data[index], strlen(value) + 1)) == NULL) {
+ tmp = realloc((*pStrList)->data[index], (strlen(value) + 1) * sizeof(char *));
+ if (!tmp) {
perror("realloc strlist_set replacement value");
return;
+ } else if (tmp != (*pStrList)->data[index]) {
+ (*pStrList)->data[index] = tmp;
}
- pStrList->data[index] = tmp;
- memset(pStrList->data[index], '\0', strlen(value) + 1);
- strncpy(pStrList->data[index], value, strlen(value));
+ memset((*pStrList)->data[index], '\0', strlen(value) + 1);
+ strncpy((*pStrList)->data[index], value, strlen(value));
}
}
@@ -317,10 +329,10 @@ void strlist_set(struct StrList *pStrList, size_t index, char *value) {
* @return string
*/
char *strlist_item(struct StrList *pStrList, size_t index) {
- if (pStrList == NULL || index > strlist_count(pStrList)) {
- return NULL;
+ if (pStrList && pStrList->data && pStrList->data[index]) {
+ return pStrList->data[index];
}
- return pStrList->data[index];
+ return NULL;
}
/**
@@ -471,7 +483,7 @@ struct StrList *strlist_init() {
struct StrList *pStrList = calloc(1, sizeof(struct StrList));
if (pStrList == NULL) {
perror("failed to allocate array");
- exit(errno);
+ return NULL;
}
pStrList->num_inuse = 0;
pStrList->num_alloc = 1;