[PATCH] Fixed BSD timestamp parsing when the day is padded with spaces in front (fixes: D-03717)

Balazs Scheidler bazsi at balabit.hu
Sun Apr 10 10:28:31 CEST 2011


Although the bugreport contained info that it'd be 'no-hostname' related, in
reality it isn't. The unit tests couldn't cover this case as the
timestamp that can typically contain such fields do not contain year information.

This patch fills this whole in the unit test and also fixes the
problem itself. It was caused by one of the performance improvement
patches, but doesn't affect PE 4.0 or older OSE versions.

Signed-off-by: Balazs Scheidler <bazsi at balabit.hu>
---
 lib/str-format.c           |    3 +-
 tests/unit/test_msgparse.c |   54 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletions(-)

diff --git a/lib/str-format.c b/lib/str-format.c
index 27a292a..a033b43 100644
--- a/lib/str-format.c
+++ b/lib/str-format.c
@@ -1,6 +1,7 @@
 #include "str-format.h"
 
 #include <string.h>
+#include <ctype.h>
 
 static gchar digits[] = "0123456789abcdef";
 
@@ -82,7 +83,7 @@ scan_uint32(const gchar **buf, gint *left, gint field_width, guint32 *num)
     {
       if ((**buf) >= '0' && (**buf) <= '9')
         result = result * 10 + ((**buf) - '0');
-      else
+      else if (!isspace(**buf))
         return FALSE;
       (*buf)++;
       (*left)--;
diff --git a/tests/unit/test_msgparse.c b/tests/unit/test_msgparse.c
index 3a60175..069cc37 100644
--- a/tests/unit/test_msgparse.c
+++ b/tests/unit/test_msgparse.c
@@ -59,6 +59,33 @@ check_value(gchar *msg, LogMessage *logmsg, NVHandle handle, const gchar *expect
   TEST_ASSERT(strcmp(p, expected) == 0, "%s", p, expected);
 }
 
+/* This function determines the year that syslog-ng would find out
+ * given the timestamp has no year information. Then returns the UTC
+ * representation of "January 1st 00:00:00" of that year. This is to
+ * be used for testcases that lack year information. ts_month is the 0
+ * based month in the timestamp being parsed.
+ */
+time_t
+get_bsd_year_utc(int ts_month)
+{
+  struct tm *tm;
+  time_t t;
+
+  time(&t);
+  tm = localtime(&t);
+
+  if (tm->tm_mon > ts_month + 1)
+    tm->tm_year++;
+
+  tm->tm_hour = 0;
+  tm->tm_min = 0;
+  tm->tm_sec = 0;
+  tm->tm_mday = 1;
+  tm->tm_mon = 0;
+  tm->tm_isdst = -1;
+  return mktime(tm);
+}
+
 int
 testcase(gchar *msg,
          gint parse_flags,
@@ -182,6 +209,33 @@ main(int argc G_GNUC_UNUSED, char *argv[] G_GNUC_UNUSED)
            NULL, "2499", NULL, NULL
            );
 
+  testcase("<15>Jan  1 01:00:00 bzorp openvpn[2499]: PTHREAD support initialized", LP_EXPECT_HOSTNAME, NULL,
+           15, 			// pri
+           get_bsd_year_utc(0) + 3600, 0, 3600,		// timestamp (sec/usec/zone)
+           "bzorp",		// host
+           "openvpn",		// openvpn
+           "PTHREAD support initialized", // msg
+           NULL, "2499", NULL, NULL
+           );
+
+  testcase("<15>Jan 10 01:00:00 bzorp openvpn[2499]: PTHREAD support initialized", LP_EXPECT_HOSTNAME, NULL,
+           15, 			// pri
+           get_bsd_year_utc(0) + 3600 + 9 * 24 * 3600, 0, 3600,		// timestamp (sec/usec/zone)
+           "bzorp",		// host
+           "openvpn",		// openvpn
+           "PTHREAD support initialized", // msg
+           NULL, "2499", NULL, NULL
+           );
+
+  testcase("<13>Jan  1 14:40:51 alma korte: message", 0, NULL,
+	   13,
+	   get_bsd_year_utc(0) + 60 * 60 * 14 + 40 * 60 + 51, 0, 3600,
+	   "",
+	   "alma",
+	   "korte: message",
+	   NULL, NULL, NULL, NULL
+	   );
+
   testcase("<7>2006-11-10T10:43:21.156+02:00 bzorp openvpn[2499]: PTHREAD support initialized", LP_EXPECT_HOSTNAME, NULL,
            7, 			// pri
            1163148201, 156000, 7200,	// timestamp (sec/usec/zone)
-- 
1.7.0.4


--=-=-=--


More information about the syslog-ng mailing list