From nobody Wed Nov 5 10:47:35 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1533732186097882.4095944600498; Wed, 8 Aug 2018 05:43:06 -0700 (PDT) Received: from localhost ([::1]:43399 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fnNns-0007HL-S0 for importer@patchew.org; Wed, 08 Aug 2018 08:43:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52606) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fnNBq-0006lR-Ju for qemu-devel@nongnu.org; Wed, 08 Aug 2018 08:03:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fnNBl-00006w-8N for qemu-devel@nongnu.org; Wed, 08 Aug 2018 08:03:46 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47732 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fnNBk-0008Vn-N3 for qemu-devel@nongnu.org; Wed, 08 Aug 2018 08:03:40 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0634172629; Wed, 8 Aug 2018 12:03:40 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-117-62.ams2.redhat.com [10.36.117.62]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B4AD71C71D; Wed, 8 Aug 2018 12:03:39 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 01C3711385D9; Wed, 8 Aug 2018 14:03:35 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Wed, 8 Aug 2018 14:03:23 +0200 Message-Id: <20180808120334.10970-46-armbru@redhat.com> In-Reply-To: <20180808120334.10970-1-armbru@redhat.com> References: <20180808120334.10970-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Wed, 08 Aug 2018 12:03:40 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Wed, 08 Aug 2018 12:03:40 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'armbru@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 45/56] json: Fix streamer not to ignore trailing unterminated structures X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: marcandre.lureau@redhat.com, mdroth@linux.vnet.ibm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 --- 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; =20 typedef struct JSONLexer { diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c index 733ce3f5ba..823db3aef8 100644 --- a/qobject/json-lexer.c +++ b/qobject/json-lexer.c @@ -369,6 +369,8 @@ void json_lexer_flush(JSONLexer *lexer) if (lexer->state !=3D 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); } =20 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, GStrin= g *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 =3D json_parser_parse(parser->tokens, parser->ap, &err); + parser->tokens =3D 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)); } =20 void json_message_parser_destroy(JSONMessageParser *parser) diff --git a/tests/check-qjson.c b/tests/check-qjson.c index 30b1b037d3..833d220654 100644 --- a/tests/check-qjson.c +++ b/tests/check-qjson.c @@ -1305,7 +1305,7 @@ static void unterminated_array(void) { Error *err =3D NULL; QObject *obj =3D qobject_from_json("[32", &err); - g_assert(!err); /* BUG */ + error_free_or_abort(&err); g_assert(obj =3D=3D NULL); } =20 @@ -1313,7 +1313,7 @@ static void unterminated_array_comma(void) { Error *err =3D NULL; QObject *obj =3D qobject_from_json("[32,", &err); - g_assert(!err); /* BUG */ + error_free_or_abort(&err); g_assert(obj =3D=3D NULL); } =20 @@ -1329,7 +1329,7 @@ static void unterminated_dict(void) { Error *err =3D NULL; QObject *obj =3D qobject_from_json("{'abc':32", &err); - g_assert(!err); /* BUG */ + error_free_or_abort(&err); g_assert(obj =3D=3D NULL); } =20 @@ -1337,7 +1337,7 @@ static void unterminated_dict_comma(void) { Error *err =3D NULL; QObject *obj =3D qobject_from_json("{'abc':32,", &err); - g_assert(!err); /* BUG */ + error_free_or_abort(&err); g_assert(obj =3D=3D NULL); } =20 --=20 2.17.1