[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v2 45/60] json: Fix streamer not to ignore trailing
From: |
Markus Armbruster |
Subject: |
[Qemu-devel] [PATCH v2 45/60] json: Fix streamer not to ignore trailing unterminated structures |
Date: |
Fri, 17 Aug 2018 17:05:44 +0200 |
json_message_process_token() accumulates tokens until it got the
sequence of tokens that comprise a single JSON value (it counts curly
braces and square brackets to decide). It feeds those token sequences
to json_parser_parse(). If a non-empty sequence of tokens remains at
the end of the parse, it's silently ignored. check-qjson.c cases
unterminated_array(), unterminated_array_comma(), unterminated_dict(),
unterminated_dict_comma() demonstrate this bug.
Fix as follows. Introduce a JSON_END_OF_INPUT token. When the
streamer receives it, it feeds the accumulated tokens to
json_parser_parse().
Signed-off-by: Markus Armbruster <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
---
include/qapi/qmp/json-lexer.h | 1 +
qobject/json-lexer.c | 2 ++
qobject/json-streamer.c | 8 ++++++++
tests/check-qjson.c | 8 ++++----
4 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/include/qapi/qmp/json-lexer.h b/include/qapi/qmp/json-lexer.h
index 5586d12f26..8058695e40 100644
--- a/include/qapi/qmp/json-lexer.h
+++ b/include/qapi/qmp/json-lexer.h
@@ -30,6 +30,7 @@ typedef enum json_token_type {
JSON_INTERPOL,
JSON_SKIP,
JSON_ERROR,
+ JSON_END_OF_INPUT
} JSONTokenType;
typedef struct JSONLexer {
diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c
index 6a3b89c855..ef29e2782d 100644
--- a/qobject/json-lexer.c
+++ b/qobject/json-lexer.c
@@ -353,6 +353,8 @@ void json_lexer_flush(JSONLexer *lexer)
if (lexer->state != lexer->start_state) {
json_lexer_feed_char(lexer, 0, true);
}
+ json_message_process_token(lexer, lexer->token, JSON_END_OF_INPUT,
+ lexer->x, lexer->y);
}
void json_lexer_destroy(JSONLexer *lexer)
diff --git a/qobject/json-streamer.c b/qobject/json-streamer.c
index e372ecc895..674dfe6e85 100644
--- a/qobject/json-streamer.c
+++ b/qobject/json-streamer.c
@@ -60,6 +60,13 @@ void json_message_process_token(JSONLexer *lexer, GString
*input,
case JSON_ERROR:
error_setg(&err, "JSON parse error, stray '%s'", input->str);
goto out_emit;
+ case JSON_END_OF_INPUT:
+ if (g_queue_is_empty(parser->tokens)) {
+ return;
+ }
+ json = json_parser_parse(parser->tokens, parser->ap, &err);
+ parser->tokens = NULL;
+ goto out_emit;
default:
break;
}
@@ -137,6 +144,7 @@ void json_message_parser_feed(JSONMessageParser *parser,
void json_message_parser_flush(JSONMessageParser *parser)
{
json_lexer_flush(&parser->lexer);
+ assert(g_queue_is_empty(parser->tokens));
}
void json_message_parser_destroy(JSONMessageParser *parser)
diff --git a/tests/check-qjson.c b/tests/check-qjson.c
index 22a3225358..a670746ca7 100644
--- a/tests/check-qjson.c
+++ b/tests/check-qjson.c
@@ -1360,7 +1360,7 @@ static void unterminated_array(void)
{
Error *err = NULL;
QObject *obj = qobject_from_json("[32", &err);
- g_assert(!err); /* BUG */
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
@@ -1368,7 +1368,7 @@ static void unterminated_array_comma(void)
{
Error *err = NULL;
QObject *obj = qobject_from_json("[32,", &err);
- g_assert(!err); /* BUG */
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
@@ -1384,7 +1384,7 @@ static void unterminated_dict(void)
{
Error *err = NULL;
QObject *obj = qobject_from_json("{'abc':32", &err);
- g_assert(!err); /* BUG */
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
@@ -1392,7 +1392,7 @@ static void unterminated_dict_comma(void)
{
Error *err = NULL;
QObject *obj = qobject_from_json("{'abc':32,", &err);
- g_assert(!err); /* BUG */
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
--
2.17.1
- Re: [Qemu-devel] [PATCH v2 60/60] json: Support %% in JSON strings when interpolating, (continued)
- [Qemu-devel] [PATCH v2 26/60] json: Simplify parse_string(), Markus Armbruster, 2018/08/17
- [Qemu-devel] [PATCH v2 33/60] json: Redesign the callback to consume JSON values, Markus Armbruster, 2018/08/17
- [Qemu-devel] [PATCH v2 11/60] check-qjson: Cover UTF-8 in single quoted strings, Markus Armbruster, 2018/08/17
- [Qemu-devel] [PATCH v2 50/60] json: Unbox tokens queue in JSONMessageParser, Markus Armbruster, 2018/08/17
- [Qemu-devel] [PATCH v2 32/60] json: Have lexer call streamer directly, Markus Armbruster, 2018/08/17
- [Qemu-devel] [PATCH v2 53/60] json: Make JSONToken opaque outside json-parser.c, Markus Armbruster, 2018/08/17
- [Qemu-devel] [PATCH v2 59/60] json: Improve safety of qobject_from_jsonf_nofail() & friends, Markus Armbruster, 2018/08/17
- [Qemu-devel] [PATCH v2 46/60] json: Assert json_parser_parse() consumes all tokens on success, Markus Armbruster, 2018/08/17
- [Qemu-devel] [PATCH v2 45/60] json: Fix streamer not to ignore trailing unterminated structures,
Markus Armbruster <=
- [Qemu-devel] [PATCH v2 41/60] json: Nicer recovery from invalid leading zero, Markus Armbruster, 2018/08/17
[Qemu-devel] [PATCH v2 40/60] json: Replace %I64d, %I64u by %PRId64, %PRIu64, Markus Armbruster, 2018/08/17
[Qemu-devel] [PATCH v2 49/60] json: Streamline json_message_process_token(), Markus Armbruster, 2018/08/17
[Qemu-devel] [PATCH v2 39/60] json: Leave rejecting invalid interpolation to parser, Markus Armbruster, 2018/08/17
[Qemu-devel] [PATCH v2 51/60] json: Eliminate lexer state IN_ERROR and pseudo-token JSON_MIN, Markus Armbruster, 2018/08/17
[Qemu-devel] [PATCH v2 43/60] qjson: Fix qobject_from_json() & friends for multiple values, Markus Armbruster, 2018/08/17