summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--qobject/json-parser.c12
-rw-r--r--tests/check-qjson.c9
2 files changed, 15 insertions, 6 deletions
diff --git a/qobject/json-parser.c b/qobject/json-parser.c
index d083810d37..c0f521b56b 100644
--- a/qobject/json-parser.c
+++ b/qobject/json-parser.c
@@ -257,8 +257,9 @@ static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
*/
static int parse_pair(JSONParserContext *ctxt, QDict *dict)
{
+ QObject *key_obj = NULL;
+ QString *key;
QObject *value;
- QString *key = NULL;
JSONToken *peek, *token;
peek = parser_context_peek_token(ctxt);
@@ -267,7 +268,8 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict)
goto out;
}
- key = qobject_to(QString, parse_value(ctxt));
+ key_obj = parse_value(ctxt);
+ key = qobject_to(QString, key_obj);
if (!key) {
parse_error(ctxt, peek, "key is not a string in object");
goto out;
@@ -297,13 +299,11 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict)
qdict_put_obj(dict, qstring_get_str(key), value);
- qobject_unref(key);
-
+ qobject_unref(key_obj);
return 0;
out:
- qobject_unref(key);
-
+ qobject_unref(key_obj);
return -1;
}
diff --git a/tests/check-qjson.c b/tests/check-qjson.c
index 07a773e653..9a02079099 100644
--- a/tests/check-qjson.c
+++ b/tests/check-qjson.c
@@ -1415,6 +1415,14 @@ static void invalid_dict_comma(void)
g_assert(obj == NULL);
}
+static void invalid_dict_key(void)
+{
+ Error *err = NULL;
+ QObject *obj = qobject_from_json("{32:'abc'}", &err);
+ error_free_or_abort(&err);
+ g_assert(obj == NULL);
+}
+
static void unterminated_literal(void)
{
Error *err = NULL;
@@ -1500,6 +1508,7 @@ int main(int argc, char **argv)
g_test_add_func("/errors/unterminated/dict_comma", unterminated_dict_comma);
g_test_add_func("/errors/invalid_array_comma", invalid_array_comma);
g_test_add_func("/errors/invalid_dict_comma", invalid_dict_comma);
+ g_test_add_func("/errors/invalid_dict_key", invalid_dict_key);
g_test_add_func("/errors/unterminated/literal", unterminated_literal);
g_test_add_func("/errors/limits/nesting", limits_nesting);
g_test_add_func("/errors/multiple_values", multiple_values);