qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 38/58] json: Treat unwanted interpolation as lexical error
Date: Fri, 24 Aug 2018 21:31:46 +0200	[thread overview]
Message-ID: <20180824193206.25475-39-armbru@redhat.com> (raw)
In-Reply-To: <20180824193206.25475-1-armbru@redhat.com>

The JSON parser optionally supports interpolation.  The lexer
recognizes interpolation tokens unconditionally.  The parser rejects
them when interpolation is disabled, in parse_interpolation().
However, it neglects to set an error then, which can make
json_parser_parse() fail without setting an error.

Move the check for unwanted interpolation from the parser's
parse_interpolation() into the lexer's finite state machine.  When
interpolation is disabled, '%' is now handled like any other
unexpected character.

The next commit will improve how such lexical errors are handled.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-39-armbru@redhat.com>
---
 include/qapi/qmp/json-lexer.h |  4 ++--
 qobject/json-lexer.c          | 30 ++++++++++++++++++------------
 qobject/json-parser.c         |  4 ----
 qobject/json-streamer.c       |  2 +-
 tests/qmp-test.c              |  4 ++++
 5 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/include/qapi/qmp/json-lexer.h b/include/qapi/qmp/json-lexer.h
index 8bce6ef676..afa84cb910 100644
--- a/include/qapi/qmp/json-lexer.h
+++ b/include/qapi/qmp/json-lexer.h
@@ -33,12 +33,12 @@ typedef enum json_token_type {
 } JSONTokenType;
 
 typedef struct JSONLexer {
-    int state;
+    int start_state, state;
     GString *token;
     int x, y;
 } JSONLexer;
 
-void json_lexer_init(JSONLexer *lexer);
+void json_lexer_init(JSONLexer *lexer, bool enable_interpolation);
 
 void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size);
 
diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c
index 5436809be6..96fe13621d 100644
--- a/qobject/json-lexer.c
+++ b/qobject/json-lexer.c
@@ -92,7 +92,7 @@
  *   Like double-quoted strings, except they're delimited by %x27
  *   (apostrophe) instead of %x22 (quotation mark), and can't contain
  *   unescaped apostrophe, but can contain unescaped quotation mark.
- * - Interpolation:
+ * - Interpolation, if enabled:
  *   interpolation = %((l|ll|I64)[du]|[ipsf])
  *
  * Note:
@@ -123,9 +123,11 @@ enum json_lexer_state {
     IN_INTERP_I64,
     IN_WHITESPACE,
     IN_START,
+    IN_START_INTERP,            /* must be IN_START + 1 */
 };
 
-QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START);
+QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START_INTERP);
+QEMU_BUILD_BUG_ON(IN_START_INTERP != IN_START + 1);
 
 #define TERMINAL(state) [0 ... 0x7F] = (state)
 
@@ -257,8 +259,12 @@ static const uint8_t json_lexer[][256] =  {
         ['I'] = IN_INTERP_I,
     },
 
-    /* top level rule */
-    [IN_START] = {
+    /*
+     * Two start states:
+     * - IN_START recognizes JSON tokens with our string extensions
+     * - IN_START_INTERP additionally recognizes interpolation.
+     */
+    [IN_START ... IN_START_INTERP] = {
         ['"'] = IN_DQ_STRING,
         ['\''] = IN_SQ_STRING,
         ['0'] = IN_ZERO,
@@ -271,17 +277,18 @@ static const uint8_t json_lexer[][256] =  {
         [','] = JSON_COMMA,
         [':'] = JSON_COLON,
         ['a' ... 'z'] = IN_KEYWORD,
-        ['%'] = IN_INTERP,
         [' '] = IN_WHITESPACE,
         ['\t'] = IN_WHITESPACE,
         ['\r'] = IN_WHITESPACE,
         ['\n'] = IN_WHITESPACE,
     },
+    [IN_START_INTERP]['%'] = IN_INTERP,
 };
 
-void json_lexer_init(JSONLexer *lexer)
+void json_lexer_init(JSONLexer *lexer, bool enable_interpolation)
 {
-    lexer->state = IN_START;
+    lexer->start_state = lexer->state = enable_interpolation
+        ? IN_START_INTERP : IN_START;
     lexer->token = g_string_sized_new(3);
     lexer->x = lexer->y = 0;
 }
@@ -321,7 +328,7 @@ static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
             /* fall through */
         case JSON_SKIP:
             g_string_truncate(lexer->token, 0);
-            new_state = IN_START;
+            new_state = lexer->start_state;
             break;
         case IN_ERROR:
             /* XXX: To avoid having previous bad input leaving the parser in an
@@ -340,8 +347,7 @@ static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
             json_message_process_token(lexer, lexer->token, JSON_ERROR,
                                        lexer->x, lexer->y);
             g_string_truncate(lexer->token, 0);
-            new_state = IN_START;
-            lexer->state = new_state;
+            lexer->state = lexer->start_state;
             return;
         default:
             break;
@@ -356,7 +362,7 @@ static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
         json_message_process_token(lexer, lexer->token, lexer->state,
                                    lexer->x, lexer->y);
         g_string_truncate(lexer->token, 0);
-        lexer->state = IN_START;
+        lexer->state = lexer->start_state;
     }
 }
 
@@ -371,7 +377,7 @@ void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
 
 void json_lexer_flush(JSONLexer *lexer)
 {
-    if (lexer->state != IN_START) {
+    if (lexer->state != lexer->start_state) {
         json_lexer_feed_char(lexer, 0, true);
     }
 }
diff --git a/qobject/json-parser.c b/qobject/json-parser.c
index 864cb578d8..2855eaaeca 100644
--- a/qobject/json-parser.c
+++ b/qobject/json-parser.c
@@ -427,10 +427,6 @@ static QObject *parse_interpolation(JSONParserContext *ctxt, va_list *ap)
 {
     JSONToken *token;
 
-    if (ap == NULL) {
-        return NULL;
-    }
-
     token = parser_context_pop_token(ctxt);
     assert(token && token->type == JSON_INTERP);
 
diff --git a/qobject/json-streamer.c b/qobject/json-streamer.c
index fa595a8761..a373e0114a 100644
--- a/qobject/json-streamer.c
+++ b/qobject/json-streamer.c
@@ -115,7 +115,7 @@ void json_message_parser_init(JSONMessageParser *parser,
     parser->tokens = g_queue_new();
     parser->token_size = 0;
 
-    json_lexer_init(&parser->lexer);
+    json_lexer_init(&parser->lexer, !!ap);
 }
 
 void json_message_parser_feed(JSONMessageParser *parser,
diff --git a/tests/qmp-test.c b/tests/qmp-test.c
index 7b3ba17c4a..4ae2245484 100644
--- a/tests/qmp-test.c
+++ b/tests/qmp-test.c
@@ -94,6 +94,10 @@ static void test_malformed(QTestState *qts)
 
     /* lexical error: interpolation */
     qtest_qmp_send_raw(qts, "%%p\n");
+    /* two errors, one for "%", one for "p" */
+    resp = qtest_qmp_receive(qts);
+    g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
+    qobject_unref(resp);
     resp = qtest_qmp_receive(qts);
     g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
     qobject_unref(resp);
-- 
2.17.1

  parent reply	other threads:[~2018-08-24 19:41 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-24 19:31 [Qemu-devel] [PULL 00/58] QObject patches for 2018-08-24 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 01/58] docs/interop/qmp-spec: How to force known good parser state Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 02/58] check-qjson: Cover multiple JSON objects in same string Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 03/58] check-qjson: Cover blank and lexically erroneous input Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 04/58] check-qjson: Cover whitespace more thoroughly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 05/58] qmp-cmd-test: Split off qmp-test Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 06/58] qmp-test: Cover syntax and lexical errors Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 07/58] test-qga: Clean up how we test QGA synchronization Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 08/58] check-qjson: Cover escaped characters more thoroughly, part 1 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 09/58] check-qjson: Streamline escaped_string()'s test strings Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 10/58] check-qjson: Cover escaped characters more thoroughly, part 2 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 11/58] check-qjson: Consolidate partly redundant string tests Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 12/58] check-qjson: Cover UTF-8 in single quoted strings Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 13/58] check-qjson: Simplify utf8_string() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 14/58] check-qjson: Fix utf8_string() to test all invalid sequences Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 15/58] check-qjson qmp-test: Cover control characters more thoroughly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 16/58] check-qjson: Cover interpolation " Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 17/58] json: Fix lexer to include the bad character in JSON_ERROR token Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 18/58] json: Reject unescaped control characters Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 19/58] json: Revamp lexer documentation Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 20/58] json: Tighten and simplify qstring_from_escaped_str()'s loop Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 21/58] check-qjson: Document we expect invalid UTF-8 to be rejected Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 22/58] json: Reject invalid UTF-8 sequences Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 23/58] json: Report first rather than last parse error Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 24/58] json: Leave rejecting invalid UTF-8 to parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 25/58] json: Accept overlong \xC0\x80 as U+0000 ("modified UTF-8") Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 26/58] json: Leave rejecting invalid escape sequences to parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 27/58] json: Simplify parse_string() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 28/58] json: Reject invalid \uXXXX, fix \u0000 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 29/58] json: Fix \uXXXX for surrogate pairs Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 30/58] check-qjson: Fix and enable utf8_string()'s disabled part Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 31/58] json: remove useless return value from lexer/parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 32/58] json-parser: simplify and avoid JSONParserContext allocation Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 33/58] json: Have lexer call streamer directly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 34/58] json: Redesign the callback to consume JSON values Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 35/58] json: Don't pass null @tokens to json_parser_parse() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 36/58] json: Don't create JSON_ERROR tokens that won't be used Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 37/58] json: Rename token JSON_ESCAPE & friends to JSON_INTERP Markus Armbruster
2018-08-24 19:31 ` Markus Armbruster [this message]
2018-08-24 19:31 ` [Qemu-devel] [PULL 39/58] json: Pass lexical errors and limit violations to callback Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 40/58] json: Leave rejecting invalid interpolation to parser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 41/58] json: Replace %I64d, %I64u by %PRId64, %PRIu64 Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 42/58] json: Improve names of lexer states related to numbers Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 43/58] qjson: Fix qobject_from_json() & friends for multiple values Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 44/58] json: Fix latent parser aborts at end of input Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 45/58] json: Fix streamer not to ignore trailing unterminated structures Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 46/58] json: Assert json_parser_parse() consumes all tokens on success Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 47/58] qjson: Have qobject_from_json() & friends reject empty and blank Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 48/58] json: Enforce token count and size limits more tightly Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 49/58] json: Streamline json_message_process_token() Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 50/58] json: Unbox tokens queue in JSONMessageParser Markus Armbruster
2018-08-24 19:31 ` [Qemu-devel] [PULL 51/58] json: Make JSONToken opaque outside json-parser.c Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 52/58] qobject: Drop superfluous includes of qemu-common.h Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 53/58] json: Clean up headers Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 54/58] tests/drive_del-test: Fix harmless JSON interpolation bug Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 55/58] json: Keep interpolation state in JSONParserContext Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 56/58] json: Improve safety of qobject_from_jsonf_nofail() & friends Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 57/58] json: Support %% in JSON strings when interpolating Markus Armbruster
2018-08-24 19:32 ` [Qemu-devel] [PULL 58/58] json: Update references to RFC 7159 to RFC 8259 Markus Armbruster
2018-08-25  9:58 ` [Qemu-devel] [PULL 00/58] QObject patches for 2018-08-24 Peter Maydell

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=20180824193206.25475-39-armbru@redhat.com \
    --to=armbru@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).