aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-03-04 12:58:43 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-03-04 12:58:43 -0500
commitc8a2afef14d93d35a5cef27c02e9dc977495642b (patch)
tree83d66e54faa341ad7f778a67d9f3b60c6035c5a5 /src
parent6e4b8bc37591cbc52e9eaacac31e897b428c0fef (diff)
downloadstasis-c8a2afef14d93d35a5cef27c02e9dc977495642b.tar.gz
Fix memory corruption in runtime_set and tpl_render
Diffstat (limited to 'src')
-rw-r--r--src/deliverable.c8
-rw-r--r--src/environment.c6
-rw-r--r--src/template.c13
3 files changed, 11 insertions, 16 deletions
diff --git a/src/deliverable.c b/src/deliverable.c
index f9c53ba..abef3f3 100644
--- a/src/deliverable.c
+++ b/src/deliverable.c
@@ -29,9 +29,8 @@ extern char **environ;
if (tplop) { \
X->DEST = tplop; \
} else { \
- X->DEST = val.as_char_p; \
+ X->DEST = NULL; \
} \
- guard_free(rtevnop) \
}
#define conv_str_noexpand(X, DEST) if (val.as_char_p) X->DEST = strdup(val.as_char_p);
@@ -48,7 +47,6 @@ extern char **environ;
} else { \
rtevnop = NULL; \
} \
- guard_free(rtevnop) \
}
#define conv_bool(X, DEST) X->DEST = val.as_bool;
@@ -59,9 +57,8 @@ extern char **environ;
if (tplop) { \
X.DEST = tplop; \
} else { \
- X.DEST = val.as_char_p; \
+ X.DEST = NULL; \
} \
- guard_free(rtevnop) \
}
#define conv_strlist_stackvar(X, DEST, TOK) { \
@@ -76,7 +73,6 @@ extern char **environ;
} else { \
rtevnop = NULL; \
} \
- guard_free(rtevnop); \
}
#define conv_bool_stackvar(X, DEST) X.DEST = val.as_bool;
diff --git a/src/environment.c b/src/environment.c
index 7315281..391807f 100644
--- a/src/environment.c
+++ b/src/environment.c
@@ -284,9 +284,10 @@ char *runtime_expand_var(RuntimeEnv *env, char *input) {
return NULL;
}
- // If there's no environment variables to process return a copy of the input string
+ // If there's no environment variables to process return the input string
if (strchr(input, delim) == NULL) {
- return strdup(input);
+ //return strdup(input);
+ return input;
}
expanded = calloc(OMC_BUFSIZ, sizeof(char));
@@ -417,7 +418,6 @@ void runtime_set(RuntimeEnv *env, const char *_key, char *_value) {
}
guard_free(now)
guard_free(key)
- guard_free(value)
}
/**
diff --git a/src/template.c b/src/template.c
index c78c885..2314dbb 100644
--- a/src/template.c
+++ b/src/template.c
@@ -66,7 +66,6 @@ static int grow(size_t z, size_t *output_bytes, char **output) {
perror("realloc failed");
return -1;
}
- memset(tmp + strlen(tmp), 0, new_size - strlen(tmp));
*output = tmp;
*output_bytes = new_size;
}
@@ -77,7 +76,7 @@ char *tpl_render(char *str) {
if (!str) {
return NULL;
}
- size_t output_bytes = strlen(str) * 2;
+ size_t output_bytes = 1024 + strlen(str); // TODO: Is grow working correctly?
char *output = NULL;
char *b_close = NULL;
char *pos = NULL;
@@ -127,10 +126,8 @@ char *tpl_render(char *str) {
// Find closing brace
b_close = strstr(pos, "}}");
if (!b_close) {
- grow(z + strlen(pos), &output_bytes, &output);
- strcpy(output, pos);
- z += strlen(pos);
- continue;
+ fprintf(stderr, "error while templating '%s'\n\nunbalanced brace at position %zu\n", str, z);
+ return NULL;
}
// Jump past closing brace
pos = b_close + 2;
@@ -144,7 +141,7 @@ char *tpl_render(char *str) {
value = strdup(env_val ? env_val : "");
} else {
// Read replacement value
- value = tpl_getval(key);
+ value = strdup(tpl_getval(key) ? tpl_getval(key) : "");
}
}
@@ -155,6 +152,8 @@ char *tpl_render(char *str) {
// Append replacement value
grow(z, &output_bytes, &output);
strcat(output, value);
+ guard_free(value);
+ output[z] = 0;
}
#ifdef DEBUG