[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v3 18/58] json: Reject unescaped control characters
From: |
Markus Armbruster |
Subject: |
[Qemu-devel] [PATCH v3 18/58] json: Reject unescaped control characters |
Date: |
Thu, 23 Aug 2018 18:39:45 +0200 |
Fix the lexer to reject unescaped control characters in JSON strings,
in accordance with RFC 8259 "The JavaScript Object Notation (JSON)
Data Interchange Format".
Bonus: we now recover more nicely from unclosed strings. E.g.
{"one: 1}\n{"two": 2}
now recovers cleanly after the newline, where before the lexer
remained confused until the next unpaired double quote or lexical
error.
Signed-off-by: Markus Armbruster <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
---
qobject/json-lexer.c | 4 ++--
tests/check-qjson.c | 6 +-----
tests/qmp-test.c | 4 ++--
3 files changed, 5 insertions(+), 9 deletions(-)
diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c
index 7c0875d225..e85e9a78ff 100644
--- a/qobject/json-lexer.c
+++ b/qobject/json-lexer.c
@@ -115,7 +115,7 @@ static const uint8_t json_lexer[][256] = {
['u'] = IN_DQ_UCODE0,
},
[IN_DQ_STRING] = {
- [1 ... 0xBF] = IN_DQ_STRING,
+ [0x20 ... 0xBF] = IN_DQ_STRING,
[0xC2 ... 0xF4] = IN_DQ_STRING,
['\\'] = IN_DQ_STRING_ESCAPE,
['"'] = JSON_STRING,
@@ -155,7 +155,7 @@ static const uint8_t json_lexer[][256] = {
['u'] = IN_SQ_UCODE0,
},
[IN_SQ_STRING] = {
- [1 ... 0xBF] = IN_SQ_STRING,
+ [0x20 ... 0xBF] = IN_SQ_STRING,
[0xC2 ... 0xF4] = IN_SQ_STRING,
['\\'] = IN_SQ_STRING_ESCAPE,
['\''] = JSON_STRING,
diff --git a/tests/check-qjson.c b/tests/check-qjson.c
index 1688b2f5c1..f1405ad47a 100644
--- a/tests/check-qjson.c
+++ b/tests/check-qjson.c
@@ -202,11 +202,7 @@ static void utf8_string(void)
"\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
"\x10\x11\x12\x13\x14\x15\x16\x17"
"\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
- /* bug: not corrected (valid UTF-8, but invalid JSON) */
- "\x01\x02\x03\x04\x05\x06\x07"
- "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
- "\x10\x11\x12\x13\x14\x15\x16\x17"
- "\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
+ NULL,
"\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007"
"\\b\\t\\n\\u000B\\f\\r\\u000E\\u000F"
"\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017"
diff --git a/tests/qmp-test.c b/tests/qmp-test.c
index 5edc97f63f..7b3ba17c4a 100644
--- a/tests/qmp-test.c
+++ b/tests/qmp-test.c
@@ -86,9 +86,9 @@ static void test_malformed(QTestState *qts)
g_assert(recovered(qts));
/* lexical error: control character in string */
- qtest_qmp_send_raw(qts, "{'execute': 'nonexistent', 'id':'\n'}");
+ qtest_qmp_send_raw(qts, "{'execute': 'nonexistent', 'id':'\n");
resp = qtest_qmp_receive(qts);
- g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound"); /* BUG */
+ g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
qobject_unref(resp);
g_assert(recovered(qts));
--
2.17.1
- [Qemu-devel] [PATCH v3 39/58] json: Pass lexical errors and limit violations to callback, (continued)
- [Qemu-devel] [PATCH v3 39/58] json: Pass lexical errors and limit violations to callback, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 26/58] json: Leave rejecting invalid escape sequences to parser, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 31/58] json: remove useless return value from lexer/parser, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 22/58] json: Reject invalid UTF-8 sequences, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 29/58] json: Fix \uXXXX for surrogate pairs, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 37/58] json: Rename token JSON_ESCAPE & friends to JSON_INTERP, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 44/58] json: Fix latent parser aborts at end of input, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 28/58] json: Reject invalid \uXXXX, fix \u0000, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 19/58] json: Revamp lexer documentation, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 17/58] json: Fix lexer to include the bad character in JSON_ERROR token, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 18/58] json: Reject unescaped control characters,
Markus Armbruster <=
- [Qemu-devel] [PATCH v3 35/58] json: Don't pass null @tokens to json_parser_parse(), Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 36/58] json: Don't create JSON_ERROR tokens that won't be used, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 27/58] json: Simplify parse_string(), Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 10/58] check-qjson: Cover escaped characters more thoroughly, part 2, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 16/58] check-qjson: Cover interpolation more thoroughly, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 25/58] json: Accept overlong \xC0\x80 as U+0000 ("modified UTF-8"), Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 04/58] check-qjson: Cover whitespace more thoroughly, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 13/58] check-qjson: Simplify utf8_string(), Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 32/58] json-parser: simplify and avoid JSONParserContext allocation, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 30/58] check-qjson: Fix and enable utf8_string()'s disabled part, Markus Armbruster, 2018/08/23