[syslog-ng] [PATCH] jsonparser: Handle every kind of invalid input gracefully

Gergely Nagy algernon at balabit.hu
Fri May 18 15:43:32 CEST 2012


Previously, we used json_tokener_parse_ex(), which always returned
NULL in case of an error. A while ago, we switched to
json_tokener_parse() instead, which never returns NULL, and instead,
modifies the returned object to be an integer casted to pointer in
case of an error.

This resulted in the json-parser segfaulting whenever it encountered
invalid input.

The best course of action is to switch back to
json_tokener_parse_ex(), because that way we can easily see when an
error happened, and we even have easy access to the error message.

In the long run, we should probably move back the tokener to the
LogJSONParser object, and protect it with a mutex, for a bit better
performance.

Reported-by: Eun Kyung <ekyung01 at googlemail.com>
Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 modules/jsonparser/jsonparser.c |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)

diff --git a/modules/jsonparser/jsonparser.c b/modules/jsonparser/jsonparser.c
index 7331ad9..d95b0e2 100644
--- a/modules/jsonparser/jsonparser.c
+++ b/modules/jsonparser/jsonparser.c
@@ -166,6 +166,7 @@ log_json_parser_process (LogParser *s, LogMessage **pmsg, const LogPathOptions *
 {
   LogJSONParser *self = (LogJSONParser *) s;
   struct json_object *jso;
+  struct json_tokener *tok;
 
   if (self->marker)
     {
@@ -177,7 +178,16 @@ log_json_parser_process (LogParser *s, LogMessage **pmsg, const LogPathOptions *
         input++;
     }
 
-  jso = json_tokener_parse (input);
+  tok = json_tokener_new ();
+  jso = json_tokener_parse_ex (tok, input, -1);
+  if (tok->err != json_tokener_success)
+    {
+      msg_error ("Unparsable JSON stream encountered",
+                 evt_tag_str ("error", json_tokener_errors[tok->err]), NULL);
+      json_tokener_free (tok);
+      return FALSE;
+    }
+  json_tokener_free (tok);
 
   if (!jso)
     {
-- 
1.7.9




More information about the syslog-ng mailing list