qobject: Propagate parse errors through qobject_from_json()
The next few commits will put the errors to use where appropriate. Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Message-Id: <1488317230-26248-13-git-send-email-armbru@redhat.com>
This commit is contained in:
parent
bff17e84a9
commit
57348c2f18
2
block.c
2
block.c
|
@ -1262,7 +1262,7 @@ static QDict *parse_json_filename(const char *filename, Error **errp)
|
||||||
ret = strstart(filename, "json:", &filename);
|
ret = strstart(filename, "json:", &filename);
|
||||||
assert(ret);
|
assert(ret);
|
||||||
|
|
||||||
options_obj = qobject_from_json(filename);
|
options_obj = qobject_from_json(filename, NULL);
|
||||||
if (!options_obj) {
|
if (!options_obj) {
|
||||||
error_setg(errp, "Could not parse the JSON options");
|
error_setg(errp, "Could not parse the JSON options");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
#include "qapi/qmp/qobject.h"
|
#include "qapi/qmp/qobject.h"
|
||||||
#include "qapi/qmp/qstring.h"
|
#include "qapi/qmp/qstring.h"
|
||||||
|
|
||||||
QObject *qobject_from_json(const char *string);
|
QObject *qobject_from_json(const char *string, Error **errp);
|
||||||
QObject *qobject_from_jsonf(const char *string, ...) GCC_FMT_ATTR(1, 2);
|
QObject *qobject_from_jsonf(const char *string, ...) GCC_FMT_ATTR(1, 2);
|
||||||
QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
|
QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
|
||||||
GCC_FMT_ATTR(1, 0);
|
GCC_FMT_ATTR(1, 0);
|
||||||
|
|
|
@ -953,7 +953,7 @@ EventInfoList *qmp_query_events(Error **errp)
|
||||||
static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
|
static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
*ret_data = qobject_from_json(qmp_schema_json);
|
*ret_data = qobject_from_json(qmp_schema_json, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -50,9 +50,9 @@ QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
|
||||||
return state.result;
|
return state.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QObject *qobject_from_json(const char *string)
|
QObject *qobject_from_json(const char *string, Error **errp)
|
||||||
{
|
{
|
||||||
return qobject_from_jsonv(string, NULL, NULL);
|
return qobject_from_jsonv(string, NULL, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -53,7 +53,7 @@ static void escaped_string(void)
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
QString *str;
|
QString *str;
|
||||||
|
|
||||||
obj = qobject_from_json(test_cases[i].encoded);
|
obj = qobject_from_json(test_cases[i].encoded, NULL);
|
||||||
str = qobject_to_qstring(obj);
|
str = qobject_to_qstring(obj);
|
||||||
g_assert(str);
|
g_assert(str);
|
||||||
g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
|
g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
|
||||||
|
@ -85,7 +85,7 @@ static void simple_string(void)
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
QString *str;
|
QString *str;
|
||||||
|
|
||||||
obj = qobject_from_json(test_cases[i].encoded);
|
obj = qobject_from_json(test_cases[i].encoded, NULL);
|
||||||
str = qobject_to_qstring(obj);
|
str = qobject_to_qstring(obj);
|
||||||
g_assert(str);
|
g_assert(str);
|
||||||
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
|
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
|
||||||
|
@ -116,7 +116,7 @@ static void single_quote_string(void)
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
QString *str;
|
QString *str;
|
||||||
|
|
||||||
obj = qobject_from_json(test_cases[i].encoded);
|
obj = qobject_from_json(test_cases[i].encoded, NULL);
|
||||||
str = qobject_to_qstring(obj);
|
str = qobject_to_qstring(obj);
|
||||||
g_assert(str);
|
g_assert(str);
|
||||||
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
|
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
|
||||||
|
@ -809,7 +809,7 @@ static void utf8_string(void)
|
||||||
utf8_in = test_cases[i].utf8_in ?: test_cases[i].utf8_out;
|
utf8_in = test_cases[i].utf8_in ?: test_cases[i].utf8_out;
|
||||||
json_out = test_cases[i].json_out ?: test_cases[i].json_in;
|
json_out = test_cases[i].json_out ?: test_cases[i].json_in;
|
||||||
|
|
||||||
obj = qobject_from_json(json_in);
|
obj = qobject_from_json(json_in, NULL);
|
||||||
if (utf8_out) {
|
if (utf8_out) {
|
||||||
str = qobject_to_qstring(obj);
|
str = qobject_to_qstring(obj);
|
||||||
g_assert(str);
|
g_assert(str);
|
||||||
|
@ -836,7 +836,7 @@ static void utf8_string(void)
|
||||||
* FIXME Enable once these bugs have been fixed.
|
* FIXME Enable once these bugs have been fixed.
|
||||||
*/
|
*/
|
||||||
if (0 && json_out != json_in) {
|
if (0 && json_out != json_in) {
|
||||||
obj = qobject_from_json(json_out);
|
obj = qobject_from_json(json_out, NULL);
|
||||||
str = qobject_to_qstring(obj);
|
str = qobject_to_qstring(obj);
|
||||||
g_assert(str);
|
g_assert(str);
|
||||||
g_assert_cmpstr(qstring_get_str(str), ==, utf8_out);
|
g_assert_cmpstr(qstring_get_str(str), ==, utf8_out);
|
||||||
|
@ -886,7 +886,7 @@ static void simple_number(void)
|
||||||
for (i = 0; test_cases[i].encoded; i++) {
|
for (i = 0; test_cases[i].encoded; i++) {
|
||||||
QInt *qint;
|
QInt *qint;
|
||||||
|
|
||||||
qint = qobject_to_qint(qobject_from_json(test_cases[i].encoded));
|
qint = qobject_to_qint(qobject_from_json(test_cases[i].encoded, NULL));
|
||||||
g_assert(qint);
|
g_assert(qint);
|
||||||
g_assert(qint_get_int(qint) == test_cases[i].decoded);
|
g_assert(qint_get_int(qint) == test_cases[i].decoded);
|
||||||
if (test_cases[i].skip == 0) {
|
if (test_cases[i].skip == 0) {
|
||||||
|
@ -920,7 +920,7 @@ static void float_number(void)
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
QFloat *qfloat;
|
QFloat *qfloat;
|
||||||
|
|
||||||
obj = qobject_from_json(test_cases[i].encoded);
|
obj = qobject_from_json(test_cases[i].encoded, NULL);
|
||||||
qfloat = qobject_to_qfloat(obj);
|
qfloat = qobject_to_qfloat(obj);
|
||||||
g_assert(qfloat);
|
g_assert(qfloat);
|
||||||
g_assert(qfloat_get_double(qfloat) == test_cases[i].decoded);
|
g_assert(qfloat_get_double(qfloat) == test_cases[i].decoded);
|
||||||
|
@ -965,7 +965,7 @@ static void keyword_literal(void)
|
||||||
QObject *null;
|
QObject *null;
|
||||||
QString *str;
|
QString *str;
|
||||||
|
|
||||||
obj = qobject_from_json("true");
|
obj = qobject_from_json("true", NULL);
|
||||||
qbool = qobject_to_qbool(obj);
|
qbool = qobject_to_qbool(obj);
|
||||||
g_assert(qbool);
|
g_assert(qbool);
|
||||||
g_assert(qbool_get_bool(qbool) == true);
|
g_assert(qbool_get_bool(qbool) == true);
|
||||||
|
@ -976,7 +976,7 @@ static void keyword_literal(void)
|
||||||
|
|
||||||
QDECREF(qbool);
|
QDECREF(qbool);
|
||||||
|
|
||||||
obj = qobject_from_json("false");
|
obj = qobject_from_json("false", NULL);
|
||||||
qbool = qobject_to_qbool(obj);
|
qbool = qobject_to_qbool(obj);
|
||||||
g_assert(qbool);
|
g_assert(qbool);
|
||||||
g_assert(qbool_get_bool(qbool) == false);
|
g_assert(qbool_get_bool(qbool) == false);
|
||||||
|
@ -998,7 +998,7 @@ static void keyword_literal(void)
|
||||||
g_assert(qbool_get_bool(qbool) == true);
|
g_assert(qbool_get_bool(qbool) == true);
|
||||||
QDECREF(qbool);
|
QDECREF(qbool);
|
||||||
|
|
||||||
obj = qobject_from_json("null");
|
obj = qobject_from_json("null", NULL);
|
||||||
g_assert(obj != NULL);
|
g_assert(obj != NULL);
|
||||||
g_assert(qobject_type(obj) == QTYPE_QNULL);
|
g_assert(qobject_type(obj) == QTYPE_QNULL);
|
||||||
|
|
||||||
|
@ -1134,13 +1134,13 @@ static void simple_dict(void)
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
QString *str;
|
QString *str;
|
||||||
|
|
||||||
obj = qobject_from_json(test_cases[i].encoded);
|
obj = qobject_from_json(test_cases[i].encoded, NULL);
|
||||||
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
||||||
|
|
||||||
str = qobject_to_json(obj);
|
str = qobject_to_json(obj);
|
||||||
qobject_decref(obj);
|
qobject_decref(obj);
|
||||||
|
|
||||||
obj = qobject_from_json(qstring_get_str(str));
|
obj = qobject_from_json(qstring_get_str(str), NULL);
|
||||||
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
||||||
qobject_decref(obj);
|
qobject_decref(obj);
|
||||||
QDECREF(str);
|
QDECREF(str);
|
||||||
|
@ -1192,7 +1192,7 @@ static void large_dict(void)
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
|
|
||||||
gen_test_json(gstr, 10, 100);
|
gen_test_json(gstr, 10, 100);
|
||||||
obj = qobject_from_json(gstr->str);
|
obj = qobject_from_json(gstr->str, NULL);
|
||||||
g_assert(obj != NULL);
|
g_assert(obj != NULL);
|
||||||
|
|
||||||
qobject_decref(obj);
|
qobject_decref(obj);
|
||||||
|
@ -1243,13 +1243,13 @@ static void simple_list(void)
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
QString *str;
|
QString *str;
|
||||||
|
|
||||||
obj = qobject_from_json(test_cases[i].encoded);
|
obj = qobject_from_json(test_cases[i].encoded, NULL);
|
||||||
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
||||||
|
|
||||||
str = qobject_to_json(obj);
|
str = qobject_to_json(obj);
|
||||||
qobject_decref(obj);
|
qobject_decref(obj);
|
||||||
|
|
||||||
obj = qobject_from_json(qstring_get_str(str));
|
obj = qobject_from_json(qstring_get_str(str), NULL);
|
||||||
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
||||||
qobject_decref(obj);
|
qobject_decref(obj);
|
||||||
QDECREF(str);
|
QDECREF(str);
|
||||||
|
@ -1305,13 +1305,13 @@ static void simple_whitespace(void)
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
QString *str;
|
QString *str;
|
||||||
|
|
||||||
obj = qobject_from_json(test_cases[i].encoded);
|
obj = qobject_from_json(test_cases[i].encoded, NULL);
|
||||||
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
||||||
|
|
||||||
str = qobject_to_json(obj);
|
str = qobject_to_json(obj);
|
||||||
qobject_decref(obj);
|
qobject_decref(obj);
|
||||||
|
|
||||||
obj = qobject_from_json(qstring_get_str(str));
|
obj = qobject_from_json(qstring_get_str(str), NULL);
|
||||||
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
|
||||||
|
|
||||||
qobject_decref(obj);
|
qobject_decref(obj);
|
||||||
|
@ -1332,7 +1332,7 @@ static void simple_varargs(void)
|
||||||
{}})),
|
{}})),
|
||||||
{}}));
|
{}}));
|
||||||
|
|
||||||
embedded_obj = qobject_from_json("[32, 42]");
|
embedded_obj = qobject_from_json("[32, 42]", NULL);
|
||||||
g_assert(embedded_obj != NULL);
|
g_assert(embedded_obj != NULL);
|
||||||
|
|
||||||
obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
|
obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
|
||||||
|
@ -1345,67 +1345,67 @@ static void empty_input(void)
|
||||||
{
|
{
|
||||||
const char *empty = "";
|
const char *empty = "";
|
||||||
|
|
||||||
QObject *obj = qobject_from_json(empty);
|
QObject *obj = qobject_from_json(empty, NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unterminated_string(void)
|
static void unterminated_string(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("\"abc");
|
QObject *obj = qobject_from_json("\"abc", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unterminated_sq_string(void)
|
static void unterminated_sq_string(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("'abc");
|
QObject *obj = qobject_from_json("'abc", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unterminated_escape(void)
|
static void unterminated_escape(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("\"abc\\\"");
|
QObject *obj = qobject_from_json("\"abc\\\"", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unterminated_array(void)
|
static void unterminated_array(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("[32");
|
QObject *obj = qobject_from_json("[32", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unterminated_array_comma(void)
|
static void unterminated_array_comma(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("[32,");
|
QObject *obj = qobject_from_json("[32,", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void invalid_array_comma(void)
|
static void invalid_array_comma(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("[32,}");
|
QObject *obj = qobject_from_json("[32,}", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unterminated_dict(void)
|
static void unterminated_dict(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("{'abc':32");
|
QObject *obj = qobject_from_json("{'abc':32", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unterminated_dict_comma(void)
|
static void unterminated_dict_comma(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("{'abc':32,");
|
QObject *obj = qobject_from_json("{'abc':32,", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void invalid_dict_comma(void)
|
static void invalid_dict_comma(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("{'abc':32,}");
|
QObject *obj = qobject_from_json("{'abc':32,}", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void unterminated_literal(void)
|
static void unterminated_literal(void)
|
||||||
{
|
{
|
||||||
QObject *obj = qobject_from_json("nul");
|
QObject *obj = qobject_from_json("nul", NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1425,11 +1425,11 @@ static void limits_nesting(void)
|
||||||
char buf[2 * (max_nesting + 1) + 1];
|
char buf[2 * (max_nesting + 1) + 1];
|
||||||
QObject *obj;
|
QObject *obj;
|
||||||
|
|
||||||
obj = qobject_from_json(make_nest(buf, max_nesting));
|
obj = qobject_from_json(make_nest(buf, max_nesting), NULL);
|
||||||
g_assert(obj != NULL);
|
g_assert(obj != NULL);
|
||||||
qobject_decref(obj);
|
qobject_decref(obj);
|
||||||
|
|
||||||
obj = qobject_from_json(make_nest(buf, max_nesting + 1));
|
obj = qobject_from_json(make_nest(buf, max_nesting + 1), NULL);
|
||||||
g_assert(obj == NULL);
|
g_assert(obj == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1037,7 +1037,7 @@ static void qmp_deserialize(void **native_out, void *datap,
|
||||||
visit_complete(d->qov, &d->obj);
|
visit_complete(d->qov, &d->obj);
|
||||||
obj_orig = d->obj;
|
obj_orig = d->obj;
|
||||||
output_json = qobject_to_json(obj_orig);
|
output_json = qobject_to_json(obj_orig);
|
||||||
obj = qobject_from_json(qstring_get_str(output_json));
|
obj = qobject_from_json(qstring_get_str(output_json), NULL);
|
||||||
|
|
||||||
QDECREF(output_json);
|
QDECREF(output_json);
|
||||||
d->qiv = qobject_input_visitor_new(obj);
|
d->qiv = qobject_input_visitor_new(obj);
|
||||||
|
|
Loading…
Reference in New Issue