[syslog-ng] [PATCH (3.5) 1/3] rewrite: Catch template syntax errors at config time

Gergely Nagy algernon at balabit.hu
Mon Jun 10 17:58:33 CEST 2013


If there is a syntax error in the template part of set() or subst(),
catch it at config time, and report it appropriately.

We do this by adding an error output variable to the constructors, and
checking the constructors' return value in the grammar.

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 lib/rewrite/rewrite-expr-grammar.ym |   12 +++++++++---
 lib/rewrite/rewrite-set.c           |   16 ++++++++++++----
 lib/rewrite/rewrite-set.h           |    2 +-
 lib/rewrite/rewrite-subst.c         |   15 +++++++++++----
 lib/rewrite/rewrite-subst.h         |    2 +-
 5 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/lib/rewrite/rewrite-expr-grammar.ym b/lib/rewrite/rewrite-expr-grammar.ym
index c28e4c8..99adddc 100644
--- a/lib/rewrite/rewrite-expr-grammar.ym
+++ b/lib/rewrite/rewrite-expr-grammar.ym
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002-2012 BalaBit IT Ltd, Budapest, Hungary
+ * Copyright (c) 2002-2013 BalaBit IT Ltd, Budapest, Hungary
  * Copyright (c) 1998-2012 Balázs Scheidler
  *
  * This library is free software; you can redistribute it and/or
@@ -76,7 +76,10 @@ rewrite_expr_list
 rewrite_expr
         : KW_SUBST '(' string string
           {
-            last_rewrite = log_rewrite_subst_new($4);
+            GError *error;
+
+            last_rewrite = log_rewrite_subst_new($4, &error);
+            CHECK_ERROR(last_rewrite, @4, "Error compiling template; %s", error->message);
             free($4);
           }
           rewrite_subst_opts ')'
@@ -87,7 +90,10 @@ rewrite_expr
           }
         | KW_SET '(' string
           {
-            last_rewrite = log_rewrite_set_new($3);
+            GError *error;
+
+            last_rewrite = log_rewrite_set_new($3, &error);
+            CHECK_ERROR(last_rewrite, @3, "Error compiling template; %s", error->message);
             free($3);
           }
           rewrite_expr_opts ')'                 { $$ = last_rewrite; }
diff --git a/lib/rewrite/rewrite-set.c b/lib/rewrite/rewrite-set.c
index 978e0fa..69b93f8 100644
--- a/lib/rewrite/rewrite-set.c
+++ b/lib/rewrite/rewrite-set.c
@@ -56,7 +56,7 @@ log_rewrite_set_clone(LogPipe *s)
   LogRewriteSet *self = (LogRewriteSet *) s;
   LogRewriteSet *cloned;
 
-  cloned = (LogRewriteSet *) log_rewrite_set_new(self->value_template->template);
+  cloned = (LogRewriteSet *) log_rewrite_set_new(self->value_template->template, NULL);
   cloned->super.value_handle = self->super.value_handle;
   cloned->super.condition = self->super.condition;
   return &cloned->super.super;
@@ -72,16 +72,24 @@ log_rewrite_set_free(LogPipe *s)
 }
 
 LogRewrite *
-log_rewrite_set_new(const gchar *new_value)
+log_rewrite_set_new(const gchar *new_value, GError **error)
 {
   LogRewriteSet *self = g_new0(LogRewriteSet, 1);
+  LogTemplate *value_template;
+
+  value_template = log_template_new(configuration, NULL);
+  if (!log_template_compile(value_template, new_value, error))
+    {
+      log_template_unref(value_template);
+      return NULL;
+    }
 
   log_rewrite_init(&self->super);
   self->super.super.free_fn = log_rewrite_set_free;
   self->super.super.clone = log_rewrite_set_clone;
   self->super.process = log_rewrite_set_process;
 
-  self->value_template = log_template_new(configuration, NULL);
-  log_template_compile(self->value_template, new_value, NULL);
+  self->value_template = value_template;
+
   return &self->super;
 }
diff --git a/lib/rewrite/rewrite-set.h b/lib/rewrite/rewrite-set.h
index 72de581..e3427d8 100644
--- a/lib/rewrite/rewrite-set.h
+++ b/lib/rewrite/rewrite-set.h
@@ -28,6 +28,6 @@
 #include "rewrite/rewrite-expr.h"
 
 /* LogRewriteSet */
-LogRewrite *log_rewrite_set_new(const gchar *new_value);
+LogRewrite *log_rewrite_set_new(const gchar *new_value, GError **error);
 
 #endif
diff --git a/lib/rewrite/rewrite-subst.c b/lib/rewrite/rewrite-subst.c
index 86a9447..937a4a2 100644
--- a/lib/rewrite/rewrite-subst.c
+++ b/lib/rewrite/rewrite-subst.c
@@ -101,7 +101,7 @@ log_rewrite_subst_clone(LogPipe *s)
   LogRewriteSubst *self = (LogRewriteSubst *) s;
   LogRewriteSubst *cloned;
 
-  cloned = (LogRewriteSubst *) log_rewrite_subst_new(self->replacement->template);
+  cloned = (LogRewriteSubst *) log_rewrite_subst_new(self->replacement->template, NULL);
   cloned->matcher = log_matcher_ref(self->matcher);
   cloned->super.value_handle = self->super.value_handle;
   cloned->super.condition = self->super.condition;
@@ -120,9 +120,17 @@ log_rewrite_subst_free(LogPipe *s)
 }
 
 LogRewrite *
-log_rewrite_subst_new(const gchar *replacement)
+log_rewrite_subst_new(const gchar *replacement, GError **error)
 {
   LogRewriteSubst *self = g_new0(LogRewriteSubst, 1);
+  LogTemplate *replacement_template;
+
+  replacement_template = log_template_new(configuration, NULL);
+  if (!log_template_compile(replacement_template, replacement, error))
+    {
+      log_template_unref(replacement_template);
+      return NULL;
+    }
 
   log_rewrite_init(&self->super);
 
@@ -130,7 +138,6 @@ log_rewrite_subst_new(const gchar *replacement)
   self->super.super.clone = log_rewrite_subst_clone;
   self->super.process = log_rewrite_subst_process;
 
-  self->replacement = log_template_new(configuration, NULL);
-  log_template_compile(self->replacement, replacement, NULL);
+  self->replacement = replacement_template;
   return &self->super;
 }
diff --git a/lib/rewrite/rewrite-subst.h b/lib/rewrite/rewrite-subst.h
index 4d4ddc3..a2aecbd 100644
--- a/lib/rewrite/rewrite-subst.h
+++ b/lib/rewrite/rewrite-subst.h
@@ -33,6 +33,6 @@ gboolean log_rewrite_subst_set_regexp(LogRewrite *s, const gchar *regexp);
 void log_rewrite_subst_set_matcher(LogRewrite *s, LogMatcher *matcher);
 void log_rewrite_subst_set_flags(LogRewrite *s, gint flags);
 
-LogRewrite *log_rewrite_subst_new(const gchar *replacement);
+LogRewrite *log_rewrite_subst_new(const gchar *replacement, GError **error);
 
 #endif
-- 
1.7.10.4




More information about the syslog-ng mailing list