qobject_from_jsonv() returns a single object. Let's make sure that during parsing we don't leak an intermediary object. Instead of returning the last object, set a parsing error.
Also, the lexer/parser keeps consuming all the data. There might be an error set earlier. Let's keep it and not call json_parser_parse() with the remaining data. Eventually, we may teach the message parser to stop consuming the data. Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com> --- qobject/qjson.c | 16 +++++++++++++++- tests/check-qjson.c | 11 +++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/qobject/qjson.c b/qobject/qjson.c index ee04e61096..8a9d116150 100644 --- a/qobject/qjson.c +++ b/qobject/qjson.c @@ -22,6 +22,7 @@ #include "qapi/qmp/qlist.h" #include "qapi/qmp/qnum.h" #include "qapi/qmp/qstring.h" +#include "qapi/qmp/qerror.h" #include "qemu/unicode.h" typedef struct JSONParsingState @@ -36,7 +37,20 @@ static void parse_json(JSONMessageParser *parser, GQueue *tokens) { JSONParsingState *s = container_of(parser, JSONParsingState, parser); - s->result = json_parser_parse(tokens, s->ap, &s->err); + if (s->result || s->err) { + if (s->result) { + qobject_unref(s->result); + s->result = NULL; + if (!s->err) { + error_setg(&s->err, QERR_JSON_PARSING); + } + } + if (tokens) { + g_queue_free_full(tokens, g_free); + } + } else { + s->result = json_parser_parse(tokens, s->ap, &s->err); + } } QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp) diff --git a/tests/check-qjson.c b/tests/check-qjson.c index da582df3e9..7d3547e0cc 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -1417,6 +1417,16 @@ static void limits_nesting(void) g_assert(obj == NULL); } +static void multiple_objects(void) +{ + Error *err = NULL; + QObject *obj; + + obj = qobject_from_json("{} {}", &err); + g_assert(obj == NULL); + error_free_or_abort(&err); +} + int main(int argc, char **argv) { g_test_init(&argc, &argv, NULL); @@ -1454,6 +1464,7 @@ int main(int argc, char **argv) g_test_add_func("/errors/invalid_dict_comma", invalid_dict_comma); g_test_add_func("/errors/unterminated/literal", unterminated_literal); g_test_add_func("/errors/limits/nesting", limits_nesting); + g_test_add_func("/errors/multiple_objects", multiple_objects); return g_test_run(); } -- 2.18.0.129.ge3331758f1