Instead of allowing syntax errors in templates to trigger at runtime, catch them at config time by verifying the return value of log_template_compile() and propagating the error message upwards. Signed-off-by: Gergely Nagy <algernon@balabit.hu> --- lib/cfg-grammar.y | 20 ++++++++++++++++++-- lib/value-pairs.c | 27 ++++++++++++++++++++------- lib/value-pairs.h | 7 ++++--- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/lib/cfg-grammar.y b/lib/cfg-grammar.y index 9505c29..0d6ab2e 100644 --- a/lib/cfg-grammar.y +++ b/lib/cfg-grammar.y @@ -1066,8 +1066,24 @@ vp_options ; vp_option - : KW_PAIR '(' string ':' string ')' { value_pairs_add_pair(last_value_pairs, configuration, $3, $5); free($3); free($5); } - | KW_PAIR '(' string string ')' { value_pairs_add_pair(last_value_pairs, configuration, $3, $4); free($3); free($4); } + : KW_PAIR '(' string ':' string ')' + { + GError *error; + + CHECK_ERROR(value_pairs_add_pair(last_value_pairs, configuration, $3, $5, &error), + @5, "Error compiling template; %s", error->message); + free($3); + free($5); + } + | KW_PAIR '(' string string ')' + { + GError *error; + + CHECK_ERROR(value_pairs_add_pair(last_value_pairs, configuration, $3, $4, &error), + @4, "Error compiling template; %s", error->message); + free($3); + free($4); + } | KW_KEY '(' string KW_REKEY '(' { last_vp_transset = value_pairs_transform_set_new($3); diff --git a/lib/value-pairs.c b/lib/value-pairs.c index 689293c..0a53dc7 100644 --- a/lib/value-pairs.c +++ b/lib/value-pairs.c @@ -151,16 +151,28 @@ value_pairs_add_glob_pattern(ValuePairs *vp, const gchar *pattern, vp->patterns[i] = p; } -void -value_pairs_add_pair(ValuePairs *vp, GlobalConfig *cfg, const gchar *key, const gchar *value) +gboolean +value_pairs_add_pair(ValuePairs *vp, GlobalConfig *cfg, const gchar *key, + const gchar *value, GError **error) { - VPPairConf *p = g_new(VPPairConf, 1); + VPPairConf *p; + LogTemplate *template; + + template = log_template_new(cfg, NULL); + if (!log_template_compile(template, value, error)) + { + log_template_unref(template); + return FALSE; + } + + p = g_new(VPPairConf, 1); p->name = g_strdup(key); - p->template = log_template_new(cfg, NULL); - log_template_compile(p->template, value, NULL); + p->template = template; g_ptr_array_add(vp->vpairs, p); + + return TRUE; } static gchar * @@ -730,6 +742,7 @@ vp_cmdline_parse_pair (const gchar *option_name, const gchar *value, ValuePairs *vp = (ValuePairs *) args[1]; GlobalConfig *cfg = (GlobalConfig *) args[0]; gchar **kv; + gboolean success; vp_cmdline_parse_rekey_finish (data); if (!g_strstr_len (value, strlen (value), "=")) @@ -740,13 +753,13 @@ vp_cmdline_parse_pair (const gchar *option_name, const gchar *value, } kv = g_strsplit(value, "=", 2); - value_pairs_add_pair (vp, cfg, kv[0], kv[1]); + success = value_pairs_add_pair (vp, cfg, kv[0], kv[1], error); g_free (kv[0]); g_free (kv[1]); g_free (kv); - return TRUE; + return success; } static ValuePairsTransformSet * diff --git a/lib/value-pairs.h b/lib/value-pairs.h index a6c9f34..bddddb5 100644 --- a/lib/value-pairs.h +++ b/lib/value-pairs.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2011-2012 BalaBit IT Ltd, Budapest, Hungary - * Copyright (c) 2011-2012 Gergely Nagy <algernon@balabit.hu> + * Copyright (c) 2011-2013 BalaBit IT Ltd, Budapest, Hungary + * Copyright (c) 2011-2013 Gergely Nagy <algernon@balabit.hu> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -41,7 +41,8 @@ typedef gboolean (*VPWalkCallbackFunc)(const gchar *name, gboolean value_pairs_add_scope(ValuePairs *vp, const gchar *scope); void value_pairs_add_glob_pattern(ValuePairs *vp, const gchar *pattern, gboolean include); -void value_pairs_add_pair(ValuePairs *vp, GlobalConfig *cfg, const gchar *key, const gchar *value); +gboolean value_pairs_add_pair(ValuePairs *vp, GlobalConfig *cfg, const gchar *key, + const gchar *value, GError **error); void value_pairs_add_transforms(ValuePairs *vp, gpointer vpts); -- 1.7.10.4