[syslog-ng] Regex subpattern macros [1.9.9]

Balazs Scheidler bazsi at balabit.hu
Mon Mar 13 18:28:16 CET 2006


The previous patch is crap (it leaks all memory used by substitution
space), use this one:


--- orig/src/filter.c
+++ mod/src/filter.c
@@ -30,11 +30,8 @@
 #include <string.h>


-gchar *re_matches[RE_MAX_MATCHES];
-
 static void log_filter_rule_free(LogFilterRule *self);

-
 gboolean
 log_filter_rule_eval(LogFilterRule *self, LogMessage *msg)
 {
@@ -304,26 +301,22 @@ filter_re_compile(const char *re, regex_
 }

 static gboolean
-filter_re_eval(FilterRE *self, gchar *str)
+filter_re_eval(FilterRE *self, LogMessage *msg, gchar *str)
 {
   regmatch_t matches[RE_MAX_MATCHES];
   gboolean rc;
   gint i;
-
-  for (i = 0; i < RE_MAX_MATCHES; i++)
-    {
-      g_free(re_matches[i]);
-      re_matches[i] = NULL;
-    }
+
+  log_msg_clear_matches(msg);
   rc = !regexec(&self->regex, str, RE_MAX_MATCHES, matches, 0);
   if (rc)
     {
       for (i = 0; i < RE_MAX_MATCHES && matches[i].rm_so != -1; i++)
         {
           gint length = matches[i].rm_eo - matches[i].rm_so;
-          re_matches[i] = g_malloc(length + 1);
-          memcpy(re_matches[i], &str[matches[i].rm_so], length);
-          re_matches[i][length] = 0;
+          msg->re_matches[i] = g_malloc(length + 1);
+          memcpy(msg->re_matches[i], &str[matches[i].rm_so], length);
+          msg->re_matches[i][length] = 0;
         }
     }
   return rc ^ self->super.comp;
@@ -341,7 +334,7 @@ filter_re_free(FilterExprNode *s)
 static gboolean
 filter_prog_eval(FilterExprNode *s, LogMessage *msg)
 {
-  return filter_re_eval((FilterRE *) s, msg->program->str);
+  return filter_re_eval((FilterRE *) s, msg, msg->program->str);
 }

 FilterExprNode *
@@ -363,7 +356,7 @@ filter_prog_new(gchar *prog)
 static gboolean
 filter_host_eval(FilterExprNode *s, LogMessage *msg)
 {
-  return filter_re_eval((FilterRE *) s, msg->host->str);
+  return filter_re_eval((FilterRE *) s, msg, msg->host->str);
 }

 FilterExprNode *
@@ -385,7 +378,7 @@ filter_host_new(gchar *host)
 static gboolean
 filter_match_eval(FilterExprNode *s, LogMessage *msg)
 {
-  return filter_re_eval((FilterRE *) s, msg->msg->str);
+  return filter_re_eval((FilterRE *) s, msg, msg->msg->str);
 }

 FilterExprNode *


--- orig/src/filter.h
+++ mod/src/filter.h
@@ -31,10 +31,6 @@
 struct _LogFilterRule;
 struct _GlobalConfig;

-/* regex substitutions from the last match */
-#define RE_MAX_MATCHES 10
-extern gchar *re_matches[RE_MAX_MATCHES];
-
 typedef struct _FilterExprNode
 {
   gboolean comp;


--- orig/src/logmsg.c
+++ mod/src/logmsg.c
@@ -417,6 +417,19 @@ log_msg_parse(LogMessage *self, gchar *d
   g_string_assign_len(self->msg, src, left);
 }

+void
+log_msg_clear_matches(LogMessage *self)
+{
+  gint i;
+
+  for (i = 0; i < RE_MAX_MATCHES; i++)
+    {
+      if (self->re_matches[i])
+        g_free(self->re_matches[i]);
+      self->re_matches[i] = NULL;
+    }
+}
+
 /**
  * log_msg_free:
  * @self: LogMessage instance
@@ -432,6 +445,7 @@ log_msg_free(LogMessage *self)
   g_string_free(self->host_from, TRUE);
   g_string_free(self->program, TRUE);
   g_string_free(self->msg, TRUE);
+  log_msg_clear_matches(self);
   g_free(self);
 }



--- orig/src/logmsg.h
+++ mod/src/logmsg.h
@@ -72,6 +72,8 @@ typedef struct _LogStamp

 void log_stamp_format(LogStamp *stamp, GString *target, gint ts_format,
glong zone_offset, gint frac_digits);

+#define RE_MAX_MATCHES 10
+
 typedef struct _LogMessage
 {
   guint ref_cnt;
@@ -87,6 +89,7 @@ typedef struct _LogMessage
   LogStamp stamp;
   LogStamp recvd;
   GString *date, *host, *host_from, *program, *msg;
+  gchar *re_matches[RE_MAX_MATCHES];
 } LogMessage;

 LogMessage *log_msg_ref(LogMessage *m);


--- orig/src/macros.c
+++ mod/src/macros.c
@@ -142,8 +142,8 @@ log_macro_expand(GString *result, gint i
     {
       gint ndx = id - M_MATCH_REF_OFS;
       /* match reference */
-      if (re_matches[ndx])
-        result_append(result, re_matches[ndx],
strlen(re_matches[ndx]), !!(flags & MF_ESCAPE_RESULT));
+      if (msg->re_matches[ndx])
+        result_append(result, msg->re_matches[ndx],
strlen(msg->re_matches[ndx]), !!(flags & MF_ESCAPE_RESULT));

       return TRUE;
     }




-- 
Bazsi



More information about the syslog-ng mailing list