[libvirt] [PATCH 2/2] Error out on unterminated arrays and objects in JSON parser

Ján Tomko jtomko at redhat.com
Tue Nov 5 14:59:56 UTC 2013


---
 src/util/virjson.c | 9 ++++++++-
 tests/jsontest.c   | 5 +++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/util/virjson.c b/src/util/virjson.c
index 2bb7324..f0a06ab 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -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);
diff --git a/tests/jsontest.c b/tests/jsontest.c
index 808a2ea..e806b6f 100644
--- a/tests/jsontest.c
+++ b/tests/jsontest.c
@@ -197,9 +197,14 @@ mymain(void)
     DO_TEST_PARSE_FAIL("number with garbage", "2345b45");
     DO_TEST_PARSE_FAIL("float with garbage", "0.0314159ee+100");
     DO_TEST_PARSE_FAIL("unterminated string", "\"The meaning of lif");
+    DO_TEST_PARSE_FAIL("unterminated array", "[ 1, 2, 3");
     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;
 }
-- 
1.8.1.5




More information about the libvir-list mailing list