[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 40/58] json: Leave rejecting invalid interpolation to
From: |
Markus Armbruster |
Subject: |
[Qemu-devel] [PULL 40/58] json: Leave rejecting invalid interpolation to parser |
Date: |
Fri, 24 Aug 2018 21:31:48 +0200 |
Both lexer and parser reject invalid interpolation specifications.
The parser's check is useless.
The lexer ends the token right after the first bad character. This
tends to lead to suboptimal error reporting. For instance, input
[ %04d ]
produces the tokens
JSON_LSQUARE [
JSON_ERROR %0
JSON_INTEGER 4
JSON_KEYWORD d
JSON_RSQUARE ]
The parser then yields an error, an object and two more errors:
error: Invalid JSON syntax
object: 4
error: JSON parse error, invalid keyword
error: JSON parse error, expecting value
Dumb down the lexer to accept [A-Za-z0-9]*. The parser's check is now
used. Emit a proper error there.
The lexer now produces
JSON_LSQUARE [
JSON_INTERP %04d
JSON_RSQUARE ]
and the parser reports just
JSON parse error, invalid interpolation '%04d'
Signed-off-by: Markus Armbruster <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
Message-Id: <address@hidden>
---
qobject/json-lexer.c | 44 ++++++-------------------------------------
qobject/json-parser.c | 1 +
tests/check-qjson.c | 3 ++-
3 files changed, 9 insertions(+), 39 deletions(-)
diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c
index 7c31c2c8ff..f1a4b5a430 100644
--- a/qobject/json-lexer.c
+++ b/qobject/json-lexer.c
@@ -93,7 +93,8 @@
* (apostrophe) instead of %x22 (quotation mark), and can't contain
* unescaped apostrophe, but can contain unescaped quotation mark.
* - Interpolation, if enabled:
- * interpolation = %((l|ll|I64)[du]|[ipsf])
+ * The lexer accepts %[A-Za-z0-9]*, and leaves rejecting invalid
+ * ones to the parser.
*
* Note:
* - Input must be encoded in modified UTF-8.
@@ -116,11 +117,6 @@ enum json_lexer_state {
IN_NEG_NONZERO_NUMBER,
IN_KEYWORD,
IN_INTERP,
- IN_INTERP_L,
- IN_INTERP_LL,
- IN_INTERP_I,
- IN_INTERP_I6,
- IN_INTERP_I64,
IN_WHITESPACE,
IN_START,
IN_START_INTERP, /* must be IN_START + 1 */
@@ -224,39 +220,11 @@ static const uint8_t json_lexer[][256] = {
},
/* interpolation */
- [IN_INTERP_LL] = {
- ['d'] = JSON_INTERP,
- ['u'] = JSON_INTERP,
- },
-
- [IN_INTERP_L] = {
- ['d'] = JSON_INTERP,
- ['l'] = IN_INTERP_LL,
- ['u'] = JSON_INTERP,
- },
-
- [IN_INTERP_I64] = {
- ['d'] = JSON_INTERP,
- ['u'] = JSON_INTERP,
- },
-
- [IN_INTERP_I6] = {
- ['4'] = IN_INTERP_I64,
- },
-
- [IN_INTERP_I] = {
- ['6'] = IN_INTERP_I6,
- },
-
[IN_INTERP] = {
- ['d'] = JSON_INTERP,
- ['i'] = JSON_INTERP,
- ['p'] = JSON_INTERP,
- ['s'] = JSON_INTERP,
- ['u'] = JSON_INTERP,
- ['f'] = JSON_INTERP,
- ['l'] = IN_INTERP_L,
- ['I'] = IN_INTERP_I,
+ TERMINAL(JSON_INTERP),
+ ['A' ... 'Z'] = IN_INTERP,
+ ['a' ... 'z'] = IN_INTERP,
+ ['0' ... '9'] = IN_INTERP,
},
/*
diff --git a/qobject/json-parser.c b/qobject/json-parser.c
index 2855eaaeca..e61cee9e8a 100644
--- a/qobject/json-parser.c
+++ b/qobject/json-parser.c
@@ -453,6 +453,7 @@ static QObject *parse_interpolation(JSONParserContext
*ctxt, va_list *ap)
} else if (!strcmp(token->str, "%f")) {
return QOBJECT(qnum_from_double(va_arg(*ap, double)));
}
+ parse_error(ctxt, token, "invalid interpolation '%s'", token->str);
return NULL;
}
diff --git a/tests/check-qjson.c b/tests/check-qjson.c
index d6fda0786f..83f8a0e6e3 100644
--- a/tests/check-qjson.c
+++ b/tests/check-qjson.c
@@ -1021,7 +1021,8 @@ static void interpolation_unknown(void)
}
g_test_trap_subprocess(NULL, 0, 0);
g_test_trap_assert_failed();
- g_test_trap_assert_stderr("*Unexpected error*stray '%x'*");
+ g_test_trap_assert_stderr("*Unexpected error*"
+ "invalid interpolation '%x'*");
}
static void interpolation_string(void)
--
2.17.1
- [Qemu-devel] [PULL 35/58] json: Don't pass null @tokens to json_parser_parse(), (continued)
- [Qemu-devel] [PULL 35/58] json: Don't pass null @tokens to json_parser_parse(), Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 37/58] json: Rename token JSON_ESCAPE & friends to JSON_INTERP, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 34/58] json: Redesign the callback to consume JSON values, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 08/58] check-qjson: Cover escaped characters more thoroughly, part 1, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 21/58] check-qjson: Document we expect invalid UTF-8 to be rejected, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 31/58] json: remove useless return value from lexer/parser, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 14/58] check-qjson: Fix utf8_string() to test all invalid sequences, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 29/58] json: Fix \uXXXX for surrogate pairs, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 26/58] json: Leave rejecting invalid escape sequences to parser, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 18/58] json: Reject unescaped control characters, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 40/58] json: Leave rejecting invalid interpolation to parser,
Markus Armbruster <=
- [Qemu-devel] [PULL 36/58] json: Don't create JSON_ERROR tokens that won't be used, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 32/58] json-parser: simplify and avoid JSONParserContext allocation, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 58/58] json: Update references to RFC 7159 to RFC 8259, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 42/58] json: Improve names of lexer states related to numbers, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 57/58] json: Support %% in JSON strings when interpolating, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 45/58] json: Fix streamer not to ignore trailing unterminated structures, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 55/58] json: Keep interpolation state in JSONParserContext, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 44/58] json: Fix latent parser aborts at end of input, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 51/58] json: Make JSONToken opaque outside json-parser.c, Markus Armbruster, 2018/08/24
- [Qemu-devel] [PULL 41/58] json: Replace %I64d, %I64u by %PRId64, %PRIu64, Markus Armbruster, 2018/08/24