aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2026-04-22 12:07:48 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2026-04-22 12:07:48 -0400
commitfff9d1997f7a1094c8c5ede672d750643bd08d01 (patch)
tree6eee392be8021662f21a4d7ae98365d2e33aa2ea
parenta8e1217917fc18651f3fdfe8f7a122f1e1b2c238 (diff)
downloadstasis-fff9d1997f7a1094c8c5ede672d750643bd08d01.tar.gz
template: handle allocation errors appropriately
-rw-r--r--src/lib/core/template.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/core/template.c b/src/lib/core/template.c
index 623b811..3a1b759 100644
--- a/src/lib/core/template.c
+++ b/src/lib/core/template.c
@@ -28,7 +28,16 @@ extern void tpl_reset() {
void tpl_register_func(char *key, void *tplfunc_ptr, int argc, void *data_in) {
struct tplfunc_frame *frame = calloc(1, sizeof(*frame));
+ if (!frame) {
+ SYSERROR("%s", "unable to allocate memory for function frame");
+ exit(1);
+ }
+
frame->key = strdup(key);
+ if (!frame->key) {
+ SYSERROR("%s", "unable to allocate memory for function frame key");
+ exit(1);
+ }
frame->argc = argc;
frame->func = tplfunc_ptr;
frame->data_in = data_in;
@@ -71,7 +80,15 @@ void tpl_register(char *key, char **ptr) {
} else {
SYSDEBUG("%s", "Creating new item");
item = calloc(1, sizeof(*item));
+ if (!item) {
+ SYSERROR("%s", "unable to allocate memory for new item");
+ exit(1);
+ }
item->key = strdup(key);
+ if (!key) {
+ SYSERROR("%s", "unable to allocate memory for new key");
+ exit(1);
+ }
}
if (!item) {
@@ -240,6 +257,11 @@ char *tpl_render(char *str) {
for (params_count = 0; params[params_count] != NULL; params_count++) {}
struct tplfunc_frame *frame = tpl_getfunc(k);
+ if (!frame) {
+ SYSERROR("no function named '%s'", k);
+ guard_array_n_free(params, (size_t) params_count);
+ return NULL;
+ }
if (params_count > frame->argc || params_count < frame->argc) {
fprintf(stderr, "At position %zu in %s\nIncorrect number of arguments for function: %s (expected %d, got %d)\n", off, key, frame->key, frame->argc, params_count);
value = strdup("");