From: Markus Armbruster <armbru@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [PATCH v3 4/7] json-streamer: make brace/bracket count unsigned
Date: Mon, 15 Jun 2026 10:11:22 +0200 [thread overview]
Message-ID: <877bo0rzo5.fsf@pond.sub.org> (raw)
In-Reply-To: <20260525150503.393743-5-pbonzini@redhat.com> (Paolo Bonzini's message of "Mon, 25 May 2026 17:05:00 +0200")
Paolo Bonzini <pbonzini@redhat.com> writes:
> It makes no sense to let brace_count and bracket_count go negative,
> also because it immediately ends error recovery and sets them both
> back to zero. Instead set them to zero *before* choosing
> whether to process the token queue; this makes it possible to
> have the fields as unsigned.
>
> Note that JSON_END_OF_INPUT now forces the parentheses to appear
> balanced, so that the queue is emptied and an error is reported;
> hence, the "type != JSON_END_OF_INPUT" condition can be removed.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> include/qobject/json-parser.h | 4 ++--
> qobject/json-streamer.c | 19 ++++++++++++++++---
> 2 files changed, 18 insertions(+), 5 deletions(-)
>
> diff --git a/include/qobject/json-parser.h b/include/qobject/json-parser.h
> index 4c3d89f751f..0cf6932ecdc 100644
> --- a/include/qobject/json-parser.h
> +++ b/include/qobject/json-parser.h
> @@ -31,8 +31,8 @@ typedef struct JSONMessageParser {
> void *opaque;
> JSONLexer lexer;
> JSONParserContext parser;
> - int brace_count;
> - int bracket_count;
> + unsigned int brace_count;
> + unsigned int bracket_count;
> GQueue tokens;
> uint64_t token_size;
> } JSONMessageParser;
> diff --git a/qobject/json-streamer.c b/qobject/json-streamer.c
> index f3dfdcaea12..b0bf2083ca6 100644
> --- a/qobject/json-streamer.c
> +++ b/qobject/json-streamer.c
> @@ -41,12 +41,18 @@ void json_message_process_token(JSONLexer *lexer, GString *input,
> parser->brace_count++;
> break;
> case JSON_RCURLY:
> + if (parser->brace_count <= 0) {
Why <= 0? parser->brace_count is unsigned now...
> + goto end_error_recovery;
> + }
> parser->brace_count--;
> break;
> case JSON_LSQUARE:
> parser->bracket_count++;
> break;
> case JSON_RSQUARE:
> + if (parser->bracket_count <= 0) {
Likewise.
> + goto end_error_recovery;
> + }
> parser->bracket_count--;
> break;
> case JSON_ERROR:
> @@ -56,6 +62,15 @@ void json_message_process_token(JSONLexer *lexer, GString *input,
> if (g_queue_is_empty(&parser->tokens)) {
> return;
> }
> + end_error_recovery:
> + /*
> + * Cause error recovery to end immediately.
> + * If not in error recovery, the parser will raise an error
> + * (due to JSON_ERROR or unexpected JSON_R{CURLY,SQUARE})
What do you mean by "due to JSON_ERROR"?
We get here on unmatched } or ]: that's the gotos
you add above.
We also get here on end of non-empty input: that's when the if right
above doesn't return.
> + * but error recovery won't be entered at all.
> + */
This is the only comment on error recovery. Would a comment giving a
brief overview on error recovery be useful?
> + parser->brace_count = 0;
> + parser->bracket_count = 0;
> break;
> default:
> break;
> @@ -83,9 +98,7 @@ void json_message_process_token(JSONLexer *lexer, GString *input,
>
> g_queue_push_tail(&parser->tokens, token);
>
> - if ((parser->brace_count > 0 || parser->bracket_count > 0)
> - && parser->brace_count >= 0 && parser->bracket_count >= 0
> - && type != JSON_END_OF_INPUT) {
> + if (parser->brace_count > 0 || parser->bracket_count > 0) {
> return;
> }
We get here when
* no lexical error, and
* no limits exceeded, and
* the curlies and squares are balanced, either in fact or because
we passed through end_error_recovery.
Good.
/* Process all tokens in the queue */
while (!g_queue_is_empty(&parser->tokens)) {
next prev parent reply other threads:[~2026-06-15 8:11 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-25 15:04 [PATCH v3 0/7] qobject: switch JSON parser to push Paolo Bonzini
2026-05-25 15:04 ` [PATCH v3 1/7] json-parser: constify JSONToken Paolo Bonzini
2026-05-25 15:04 ` [PATCH v3 2/7] json-parser: replace with a push parser Paolo Bonzini
2026-06-12 14:21 ` Markus Armbruster
2026-06-12 15:08 ` Paolo Bonzini
2026-06-15 7:48 ` Markus Armbruster
2026-06-15 11:06 ` Markus Armbruster
2026-06-15 12:23 ` Markus Armbruster
2026-05-25 15:04 ` [PATCH v3 3/7] json-streamer: reuse parser Paolo Bonzini
2026-06-15 7:56 ` Markus Armbruster
2026-05-25 15:05 ` [PATCH v3 4/7] json-streamer: make brace/bracket count unsigned Paolo Bonzini
2026-06-15 8:11 ` Markus Armbruster [this message]
2026-05-25 15:05 ` [PATCH v3 5/7] json-streamer: remove token queue Paolo Bonzini
2026-06-15 10:58 ` Markus Armbruster
2026-06-15 13:33 ` Paolo Bonzini
2026-06-15 12:29 ` Markus Armbruster
2026-06-15 13:22 ` Paolo Bonzini
2026-05-25 15:05 ` [PATCH v3 6/7] json-streamer: do not heap-allocate JSONToken Paolo Bonzini
2026-05-25 15:05 ` [PATCH v3 7/7] json-parser: add location to JSON parsing errors Paolo Bonzini
2026-06-15 11:35 ` Markus Armbruster
2026-06-15 13:22 ` Paolo Bonzini
2026-06-02 8:58 ` [PATCH v3 0/7] qobject: switch JSON parser to push Paolo Bonzini
2026-06-15 17:01 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=877bo0rzo5.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.