[json-glib] parser: Always perform UTF-8 validation



commit a40e07510208456eeb52776a5ab1d8eb90625fbc
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sat Oct 26 20:43:36 2013 +0100

    parser: Always perform UTF-8 validation
    
    JSON is defined to be encoded using UTF-8, so we should not rely on
    the documentation saying so when parsing, but validate the input and
    eventually provide a recoverable parsing error.

 json-glib/json-parser.c |    9 +++++++++
 json-glib/json-parser.h |    2 ++
 2 files changed, 11 insertions(+), 0 deletions(-)
---
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c
index 12c3ebf..edc13f0 100644
--- a/json-glib/json-parser.c
+++ b/json-glib/json-parser.c
@@ -878,6 +878,15 @@ json_parser_load (JsonParser   *parser,
 
   json_parser_clear (parser);
 
+  if (!g_utf8_validate (data, -1, NULL))
+    {
+      g_set_error_literal (error, JSON_PARSER_ERROR,
+                           JSON_PARSER_ERROR_INVALID_DATA,
+                           _("JSON data must be UTF-8 encoded"));
+      g_signal_emit (parser, parser_signals[ERROR], 0, *error);
+      return FALSE;
+    }
+
   scanner = json_scanner_create (parser);
   json_scanner_input_text (scanner, data, length);
 
diff --git a/json-glib/json-parser.h b/json-glib/json-parser.h
index fc58b0a..894a80b 100644
--- a/json-glib/json-parser.h
+++ b/json-glib/json-parser.h
@@ -55,6 +55,7 @@ typedef struct _JsonParserClass         JsonParserClass;
  * @JSON_PARSER_ERROR_MISSING_COLON: expected colon
  * @JSON_PARSER_ERROR_INVALID_BAREWORD: invalid bareword
  * @JSON_PARSER_ERROR_EMPTY_MEMBER_NAME: empty member name (Since: 0.16)
+ * @JSON_PARSER_ERROR_INVALID_DATA: invalid data (Since: 0.18)
  * @JSON_PARSER_ERROR_UNKNOWN: unknown error
  *
  * Error enumeration for #JsonParser
@@ -68,6 +69,7 @@ typedef enum {
   JSON_PARSER_ERROR_MISSING_COLON,
   JSON_PARSER_ERROR_INVALID_BAREWORD,
   JSON_PARSER_ERROR_EMPTY_MEMBER_NAME,
+  JSON_PARSER_ERROR_INVALID_DATA,
 
   JSON_PARSER_ERROR_UNKNOWN
 } JsonParserError;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]