提交 dd98e848 编写于 作者: M Markus Armbruster

qjson: Have qobject_from_json() & friends reject empty and blank

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: NMarkus Armbruster <armbru@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-48-armbru@redhat.com>
上级 5d50113c
...@@ -1478,11 +1478,6 @@ static QDict *parse_json_filename(const char *filename, Error **errp) ...@@ -1478,11 +1478,6 @@ static QDict *parse_json_filename(const char *filename, Error **errp)
options_obj = qobject_from_json(filename, errp); options_obj = qobject_from_json(filename, errp);
if (!options_obj) { 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: "); error_prepend(errp, "Could not parse the JSON options: ");
return NULL; return NULL;
} }
......
...@@ -725,11 +725,6 @@ Visitor *qobject_input_visitor_new_str(const char *str, ...@@ -725,11 +725,6 @@ Visitor *qobject_input_visitor_new_str(const char *str,
if (is_json) { if (is_json) {
obj = qobject_from_json(str, errp); obj = qobject_from_json(str, errp);
if (!obj) { if (!obj) {
/* Work around qobject_from_json() lossage TODO fix that */
if (errp && !*errp) {
error_setg(errp, "JSON parse error");
return NULL;
}
return NULL; return NULL;
} }
args = qobject_to(QDict, obj); args = qobject_to(QDict, obj);
......
...@@ -70,6 +70,10 @@ static QObject *qobject_from_jsonv(const char *string, va_list *ap, ...@@ -70,6 +70,10 @@ static QObject *qobject_from_jsonv(const char *string, va_list *ap,
json_message_parser_flush(&state.parser); json_message_parser_flush(&state.parser);
json_message_parser_destroy(&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); error_propagate(errp, state.err);
return state.result; return state.result;
} }
......
...@@ -1291,13 +1291,21 @@ static void simple_interpolation(void) ...@@ -1291,13 +1291,21 @@ static void simple_interpolation(void)
static void empty_input(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); g_assert(obj == NULL);
} }
static void blank_input(void) 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); g_assert(obj == NULL);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册