[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v2 10/18] qjson: report an error if there are multip
From: |
Marc-André Lureau |
Subject: |
[Qemu-devel] [PATCH v2 10/18] qjson: report an error if there are multiple results |
Date: |
Thu, 19 Jul 2018 20:41:03 +0200 |
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 <address@hidden>
---
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
- [Qemu-devel] [PATCH v2 00/18] monitor: various code simplification and fixes, Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 03/18] qmp: constify qmp_is_oob(), Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 01/18] tests: change /0.15/* tests to /qmp/*, Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 02/18] monitor: consitify qmp_send_response() QDict argument, Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 04/18] Revert "qmp: isolate responses into io thread", Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 05/18] monitor: no need to save need_resume, Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 11/18] qjson: report error on unterminated string, Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 08/18] json-parser: simplify and avoid JSONParserContext allocation, Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 10/18] qjson: report an error if there are multiple results,
Marc-André Lureau <=
- [Qemu-devel] [PATCH v2 09/18] json-parser: further simplify freeing JSONParserContext, Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 06/18] qga: process_event() simplification and leak fix, Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 07/18] qmp: drop json_parser_parse() wrapper, Marc-André Lureau, 2018/07/19
- [Qemu-devel] [PATCH v2 13/18] json-parser: set an error if parsing returned NULL, Marc-André Lureau, 2018/07/19