[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v3 47/58] qjson: Have qobject_from_json() & friends
From: |
Markus Armbruster |
Subject: |
[Qemu-devel] [PATCH v3 47/58] qjson: Have qobject_from_json() & friends reject empty and blank |
Date: |
Thu, 23 Aug 2018 18:40:14 +0200 |
The last case where qobject_from_json() & friends return null without
setting an error is empty or blank input. Callers:
* block.c's parse_json_protocol() reports "Could not parse the JSON
options". It's marked as a work-around, because it also covered
actual bugs, but they got fixed in the previous few commits.
* qobject_input_visitor_new_str() reports "JSON parse error". Also
marked as work-around. The recent fixes have made this unreachable,
because it currently gets called only for input starting with '{'.
* check-qjson.c's empty_input() and blank_input() demonstrate the
behavior.
* The other callers are not affected since they only pass input with
exactly one JSON value or, in the case of negative tests, one error.
Fail with "Expecting a JSON value" instead of returning null, and
simplify callers.
Signed-off-by: Markus Armbruster <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
---
block.c | 5 -----
qapi/qobject-input-visitor.c | 5 -----
qobject/qjson.c | 4 ++++
tests/check-qjson.c | 12 ++++++++++--
4 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/block.c b/block.c
index 6161dbe3eb..0dbb1fcc7b 100644
--- a/block.c
+++ b/block.c
@@ -1478,11 +1478,6 @@ static QDict *parse_json_filename(const char *filename,
Error **errp)
options_obj = qobject_from_json(filename, errp);
if (!options_obj) {
- /* Work around qobject_from_json() lossage TODO fix that */
- if (errp && !*errp) {
- error_setg(errp, "Could not parse the JSON options");
- return NULL;
- }
error_prepend(errp, "Could not parse the JSON options: ");
return NULL;
}
diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c
index da57f4cc24..3e88b27f9e 100644
--- a/qapi/qobject-input-visitor.c
+++ b/qapi/qobject-input-visitor.c
@@ -725,11 +725,6 @@ Visitor *qobject_input_visitor_new_str(const char *str,
if (is_json) {
obj = qobject_from_json(str, errp);
if (!obj) {
- /* Work around qobject_from_json() lossage TODO fix that */
- if (errp && !*errp) {
- error_setg(errp, "JSON parse error");
- return NULL;
- }
return NULL;
}
args = qobject_to(QDict, obj);
diff --git a/qobject/qjson.c b/qobject/qjson.c
index 7f69036487..b9ccae2c2a 100644
--- a/qobject/qjson.c
+++ b/qobject/qjson.c
@@ -70,6 +70,10 @@ static QObject *qobject_from_jsonv(const char *string,
va_list *ap,
json_message_parser_flush(&state.parser);
json_message_parser_destroy(&state.parser);
+ if (!state.result && !state.err) {
+ error_setg(&state.err, "Expecting a JSON value");
+ }
+
error_propagate(errp, state.err);
return state.result;
}
diff --git a/tests/check-qjson.c b/tests/check-qjson.c
index 0ca4b3c823..936258ddd4 100644
--- a/tests/check-qjson.c
+++ b/tests/check-qjson.c
@@ -1291,13 +1291,21 @@ static void simple_interpolation(void)
static void empty_input(void)
{
- QObject *obj = qobject_from_json("", &error_abort);
+ Error *err = NULL;
+ QObject *obj;
+
+ obj = qobject_from_json("", &err);
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
static void blank_input(void)
{
- QObject *obj = qobject_from_json("\n ", &error_abort);
+ Error *err = NULL;
+ QObject *obj;
+
+ obj = qobject_from_json("\n ", &err);
+ error_free_or_abort(&err);
g_assert(obj == NULL);
}
--
2.17.1
- [Qemu-devel] [PATCH v3 23/58] json: Report first rather than last parse error, (continued)
- [Qemu-devel] [PATCH v3 23/58] json: Report first rather than last parse error, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 14/58] check-qjson: Fix utf8_string() to test all invalid sequences, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 11/58] check-qjson: Consolidate partly redundant string tests, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 01/58] docs/interop/qmp-spec: How to force known good parser state, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 20/58] json: Tighten and simplify qstring_from_escaped_str()'s loop, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 09/58] check-qjson: Streamline escaped_string()'s test strings, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 02/58] check-qjson: Cover multiple JSON objects in same string, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 03/58] check-qjson: Cover blank and lexically erroneous input, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 06/58] qmp-test: Cover syntax and lexical errors, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 49/58] json: Streamline json_message_process_token(), Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 47/58] qjson: Have qobject_from_json() & friends reject empty and blank,
Markus Armbruster <=
- [Qemu-devel] [PATCH v3 46/58] json: Assert json_parser_parse() consumes all tokens on success, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 42/58] json: Improve names of lexer states related to numbers, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 51/58] json: Make JSONToken opaque outside json-parser.c, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 43/58] qjson: Fix qobject_from_json() & friends for multiple values, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 48/58] json: Enforce token count and size limits more tightly, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 38/58] json: Treat unwanted interpolation as lexical error, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 57/58] json: Support %% in JSON strings when interpolating, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 53/58] json: Clean up headers, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 54/58] tests/drive_del-test: Fix harmless JSON interpolation bug, Markus Armbruster, 2018/08/23
- [Qemu-devel] [PATCH v3 58/58] json: Update references to RFC 7159 to RFC 8259, Markus Armbruster, 2018/08/23