From nobody Wed Nov 5 22:58:03 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 1537806150365737.180550924392; Mon, 24 Sep 2018 09:22:30 -0700 (PDT) Received: from localhost ([::1]:46025 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g4Tcz-00046Z-2y for importer@patchew.org; Mon, 24 Sep 2018 12:22:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g4Tat-0002uz-1o for qemu-devel@nongnu.org; Mon, 24 Sep 2018 12:20:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g4Tas-0004Vz-5p for qemu-devel@nongnu.org; Mon, 24 Sep 2018 12:20:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36988) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g4Tar-0004Vh-U5 for qemu-devel@nongnu.org; Mon, 24 Sep 2018 12:20:18 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4C1A98830F for ; Mon, 24 Sep 2018 16:20:17 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-98.ams2.redhat.com [10.36.116.98]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E15D012A58 for ; Mon, 24 Sep 2018 16:20:13 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 5C4E7113860E; Mon, 24 Sep 2018 18:20:07 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 24 Sep 2018 18:20:04 +0200 Message-Id: <20180924162007.3084-4-armbru@redhat.com> In-Reply-To: <20180924162007.3084-1-armbru@redhat.com> References: <20180924162007.3084-1-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 24 Sep 2018 16:20:17 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 3/6] json: Make lexer's "character consumed" logic less confusing 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: , 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" The lexer uses macro TERMINAL_NEEDED_LOOKAHEAD() to decide whether a state transition consumes the input character. It returns true when the state transition is defined with the TERMINAL() macro. To detect that, it checks whether input '\0' would have resulted in the same state transition, and the new state is not IN_ERROR. Why does that even work? For all states, the new state on input '\0' is either IN_ERROR or defined with TERMINAL(). If the state transition equals the one we'd get for input '\0', it goes to IN_ERROR or to the argument of TERMINAL(). We never use TERMINAL(IN_ERROR), because it makes no sense. Thus, if it doesn't go to IN_ERROR, it must be defined with TERMINAL(). Since this isn't quite confusing enough, we negate the result to get @char_consumed, and ignore it when @flush is true. Instead of deriving the lookahead bit from the state transition, make it explicit. This is easier to understand, and a bit more flexible, too. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Message-Id: <20180831075841.13363-4-armbru@redhat.com> --- qobject/json-lexer.c | 27 ++++++++++++++++----------- qobject/json-parser-int.h | 1 + 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c index ec3aec726f..28582e17d9 100644 --- a/qobject/json-lexer.c +++ b/qobject/json-lexer.c @@ -121,15 +121,11 @@ enum json_lexer_state { }; =20 QEMU_BUILD_BUG_ON((int)JSON_MIN <=3D (int)IN_START_INTERP); +QEMU_BUILD_BUG_ON(JSON_MAX >=3D 0x80); QEMU_BUILD_BUG_ON(IN_START_INTERP !=3D IN_START + 1); =20 -#define TERMINAL(state) [0 ... 0xFF] =3D (state) - -/* Return whether TERMINAL is a terminal state and the transition to it - from OLD_STATE required lookahead. This happens whenever the table - below uses the TERMINAL macro. */ -#define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \ - (terminal !=3D IN_ERROR && json_lexer[(old_state)][0] =3D=3D (terminal= )) +#define LOOKAHEAD 0x80 +#define TERMINAL(state) [0 ... 0xFF] =3D ((state) | LOOKAHEAD) =20 static const uint8_t json_lexer[][256] =3D { /* Relies on default initialization to IN_ERROR! */ @@ -251,6 +247,17 @@ static const uint8_t json_lexer[][256] =3D { [IN_START_INTERP]['%'] =3D IN_INTERP, }; =20 +static inline uint8_t next_state(JSONLexer *lexer, char ch, bool flush, + bool *char_consumed) +{ + uint8_t next; + + assert(lexer->state <=3D ARRAY_SIZE(json_lexer)); + next =3D json_lexer[lexer->state][(uint8_t)ch]; + *char_consumed =3D !flush && !(next & LOOKAHEAD); + return next & ~LOOKAHEAD; +} + void json_lexer_init(JSONLexer *lexer, bool enable_interpolation) { lexer->start_state =3D lexer->state =3D enable_interpolation @@ -271,11 +278,9 @@ static void json_lexer_feed_char(JSONLexer *lexer, cha= r ch, bool flush) } =20 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 !flush - && !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state); + new_state =3D next_state(lexer, ch, flush, &char_consumed); if (char_consumed) { + assert(!flush); g_string_append_c(lexer->token, ch); } =20 diff --git a/qobject/json-parser-int.h b/qobject/json-parser-int.h index ceaa890ec6..abeec63af5 100644 --- a/qobject/json-parser-int.h +++ b/qobject/json-parser-int.h @@ -33,6 +33,7 @@ typedef enum json_token_type { JSON_SKIP, JSON_ERROR, JSON_END_OF_INPUT, + JSON_MAX =3D JSON_END_OF_INPUT } JSONTokenType; =20 typedef struct JSONToken JSONToken; --=20 2.17.1