From nobody Wed Nov 5 16:42:20 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 1535353514224321.38398932052235; Mon, 27 Aug 2018 00:05:14 -0700 (PDT) Received: from localhost ([::1]:51707 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fuBaG-0008MV-TQ for importer@patchew.org; Mon, 27 Aug 2018 03:05:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57812) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fuBVv-0005tl-5c for qemu-devel@nongnu.org; Mon, 27 Aug 2018 03:00:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fuBVr-00067W-Tq for qemu-devel@nongnu.org; Mon, 27 Aug 2018 03:00:39 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:53036 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 1fuBVr-00066t-LT for qemu-devel@nongnu.org; Mon, 27 Aug 2018 03:00:35 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 270CB4023461; Mon, 27 Aug 2018 07:00:35 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-97.ams2.redhat.com [10.36.116.97]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F12B22026E18; Mon, 27 Aug 2018 07:00:32 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id F0F931138603; Mon, 27 Aug 2018 09:00:21 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 27 Aug 2018 09:00:17 +0200 Message-Id: <20180827070021.11931-3-armbru@redhat.com> In-Reply-To: <20180827070021.11931-1-armbru@redhat.com> References: <20180827070021.11931-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Mon, 27 Aug 2018 07:00:35 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Mon, 27 Aug 2018 07:00:35 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH 2/6] json: Clean up how lexer consumes "end of input" 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" When the lexer isn't in its start state at the end of input, it's working on a token. To flush it out, it needs to transit to its start state on "end of input" lookahead. There are two ways to the start state, depending on the current state: * If the lexer is in a TERMINAL(JSON_FOO) state, it can emit a JSON_FOO token. * Else, it can go to IN_ERROR state, and emit a JSON_ERROR token. There are complications, however: * The transition to IN_ERROR state consumes the input character and adds it to the JSON_ERROR token. The latter is inappropriate for the "end of input" character, so we suppress that. See also recent commit "json: Fix lexer to include the bad character in JSON_ERROR token". * The transition to a TERMINAL(JSON_FOO) state doesn't consume the input character. In that case, the lexer normally loops until it is consumed. We have to suppress that for the "end of input" input character. If we didn't, the lexer would consume it by entering IN_ERROR state, emitting a bogus JSON_ERROR token. We fixed that in commit bd3924a33a6. However, simply breaking the loop this way assumes that the lexer needs exactly one state transition to reach its start state. That assumption is correct now, but it's unclean, and I'll soon break it. Clean up: instead of breaking the loop after one iteration, break it after it reached the start state. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- qobject/json-lexer.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c index 4867839f66..ec3aec726f 100644 --- a/qobject/json-lexer.c +++ b/qobject/json-lexer.c @@ -261,7 +261,8 @@ void json_lexer_init(JSONLexer *lexer, bool enable_inte= rpolation) =20 static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush) { - int char_consumed, new_state; + int new_state; + bool char_consumed =3D false; =20 lexer->x++; if (ch =3D=3D '\n') { @@ -269,11 +270,12 @@ static void json_lexer_feed_char(JSONLexer *lexer, ch= ar ch, bool flush) lexer->y++; } =20 - do { + while (flush ? lexer->state !=3D lexer->start_state : !char_consumed) { assert(lexer->state <=3D ARRAY_SIZE(json_lexer)); new_state =3D json_lexer[lexer->state][(uint8_t)ch]; - char_consumed =3D !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_sta= te); - if (char_consumed && !flush) { + char_consumed =3D !flush + && !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state); + if (char_consumed) { g_string_append_c(lexer->token, ch); } =20 @@ -318,7 +320,7 @@ static void json_lexer_feed_char(JSONLexer *lexer, char= ch, bool flush) break; } lexer->state =3D new_state; - } while (!char_consumed && !flush); + } =20 /* Do not let a single token grow to an arbitrarily large size, * this is a security consideration. @@ -342,9 +344,8 @@ void json_lexer_feed(JSONLexer *lexer, const char *buff= er, size_t size) =20 void json_lexer_flush(JSONLexer *lexer) { - if (lexer->state !=3D lexer->start_state) { - json_lexer_feed_char(lexer, 0, true); - } + json_lexer_feed_char(lexer, 0, true); + assert(lexer->state =3D=3D lexer->start_state); json_message_process_token(lexer, lexer->token, JSON_END_OF_INPUT, lexer->x, lexer->y); } --=20 2.17.1