diff --git a/src/util/virjson.c b/src/util/virjson.c index 2bb73242c2c098540d1b6a734f68d3a38ea125f3..f0a06abfa6025e3e220189113c7c6c599036e64d 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 73d8b0290f26f0e3a8a81498703a358a76dca563..933c41e11bd6932a81c694d2e0a97f5f463526dd 100644 --- a/tests/jsontest.c +++ b/tests/jsontest.c @@ -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; }