From e4baff79bd7dccb9271a209c88e2c7f05aadad5c Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sat, 11 Nov 2017 09:00:33 +0000 Subject: [PATCH] output: add must_put_err() utility function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function deals situation when mustach put happens before enter, that is a sign of an invalid input template. Good side of adding this function is that dhcpd-pools does not need to modify code provided by Jose. Reference: 66183bc7c7ae0d3f367c08f4e7476c4cbcd459cd Proposed-by: José Bollo Signed-off-by: Sami Kerola --- src/mustach-dhcpd-pools.c | 11 ++++++++++- src/mustach.c | 4 +--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/mustach-dhcpd-pools.c b/src/mustach-dhcpd-pools.c index f88cc0e..c2ecc0f 100644 --- a/src/mustach-dhcpd-pools.c +++ b/src/mustach-dhcpd-pools.c @@ -61,11 +61,20 @@ struct expl { static int must_enter(void *closure, const char *name); static int must_leave(void *closure); +/* This can be called when template is invalid end put happens before enter. */ +int must_put_err(void *closure __attribute__ ((unused)), + const char *name __attribute__ ((unused)), + int escape __attribute__ ((unused)), + FILE *file __attribute__ ((unused))) +{ + return MUSTACH_ERROR_SYSTEM; +} + /* Set mustach function pointers. */ static struct mustach_itf itf = { .start = NULL, .enter = must_enter, - .put = NULL, + .put = must_put_err, .next = NULL, .leave = must_leave }; diff --git a/src/mustach.c b/src/mustach.c index e2da8b2..9868cff 100644 --- a/src/mustach.c +++ b/src/mustach.c @@ -38,7 +38,7 @@ static int getpartial(struct mustach_itf *itf, void *closure, const char *name, *result = NULL; file = open_memstream(result, &size); - if (itf->put == NULL || file == NULL) + if (file == NULL) rc = MUSTACH_ERROR_SYSTEM; else { rc = itf->put(closure, name, 0, file); @@ -192,8 +192,6 @@ static int process(const char *template, struct mustach_itf *itf, void *closure, default: /* replacement */ if (emit) { - if (itf->put == NULL) - return MUSTACH_ERROR_SYSTEM; rc = itf->put(closure, name, c != '&', file); if (rc < 0) return rc;