]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Error out on unterminated arrays and objects in JSON parser
authorJán Tomko <jtomko@redhat.com>
Mon, 4 Nov 2013 13:50:11 +0000 (14:50 +0100)
committerJán Tomko <jtomko@redhat.com>
Wed, 20 Nov 2013 11:35:44 +0000 (12:35 +0100)
src/util/virjson.c
tests/jsontest.c

index 2bb73242c2c098540d1b6a734f68d3a38ea125f3..f0a06abfa6025e3e220189113c7c6c599036e64d 100644 (file)
@@ -1004,7 +1004,14 @@ virJSONValuePtr virJSONValueFromString(const char *jsonstring)
         goto cleanup;
     }
 
-    ret = parser.head;
+    if (parser.nstate != 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("cannot parse json %s: unterminated string/map/array"),
+                       jsonstring);
+        virJSONValueFree(parser.head);
+    } else {
+        ret = parser.head;
+    }
 
 cleanup:
     yajl_free(hand);
index 73d8b0290f26f0e3a8a81498703a358a76dca563..933c41e11bd6932a81c694d2e0a97f5f463526dd 100644 (file)
@@ -203,10 +203,16 @@ mymain(void)
     DO_TEST_PARSE_FAIL("float with garbage", "[ 0.0314159ee+100 ]");
 
     DO_TEST_PARSE("string", "[ \"The meaning of life\" ]");
+    DO_TEST_PARSE_FAIL("unterminated string", "[ \"The meaning of lif ]");
+
 
     DO_TEST_PARSE_FAIL("object with numeric keys", "{ 1:1, 2:1, 3:2 }");
+    DO_TEST_PARSE_FAIL("unterminated object", "{ \"1\":1, \"2\":1, \"3\":2");
+    DO_TEST_PARSE_FAIL("unterminated array of objects",
+                       "[ {\"name\": \"John\"}, {\"name\": \"Paul\"}, ");
     DO_TEST_PARSE_FAIL("array of an object with an array as a key",
                        "[ {[\"key1\", \"key2\"]: \"value\"} ]");
+    DO_TEST_PARSE_FAIL("object with unterminated key", "{ \"key:7 }");
 
     return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 }