tmpl

Simple key-value templator
git clone https://git.sinitax.com/sinitax/tmpl
Log | Files | Refs | README | LICENSE | sfeed.txt

commit daaedb5c876ce1a31b0510382abec68b4c81fdb4
parent fed613ed3d2fb9e60644f829d9df0521b41b319e
Author: Louis Burda <quent.burda@gmail.com>
Date:   Wed,  2 Aug 2023 23:38:40 +0200

Properly allocate variable strings when assigning from file

Diffstat:
Mtmpl.c | 14+++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/tmpl.c b/tmpl.c @@ -71,6 +71,10 @@ assign(char *line) sep = strchr(line, '='); if (!sep) die("invalid assign '%s'", line); + + line = strdup(line); + if (!line) die("strdup:"); + sep = strchr(line, '='); *sep = '\0'; var = malloc(sizeof(struct var)); @@ -88,7 +92,7 @@ template(char *line) { const char *value; char *open, *close; - char *sep, *dup; + char *sep; lineno++; @@ -130,9 +134,7 @@ template(char *line) sep = strchr(line + 8, ' '); if (!sep) die("invalid #default\n%lu: %s", lineno, line); *sep = '='; - dup = strdup(line + 8); - if (!dup) die("strdup:"); - assign(dup); + assign(line + 8); } else if (!strncmp(line, "#default ", 9)) { if (stack_top && !stack[stack_top-1].active) return; @@ -143,9 +145,7 @@ template(char *line) value = getvar(line + 9); if (!value) { *sep = '='; - dup = strdup(line + 9); - if (!dup) die("strdup:"); - assign(dup); + assign(line + 9); } } else if (!strncmp(line, "#-- ", 4)) { return; /* comment */