qapi: return a 'missing parameter' error

The 'old' dispatch code returned a QERR_MISSING_PARAMETER for missing
parameters, but the qapi qmp_dispatch() code uses
QERR_INVALID_PARAMETER_TYPE.

Improve qapi code to return QERR_MISSING_PARAMETER where
appropriate.

Fix expected error message in iotests.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Alberto Garcia <berto@igalia.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20160930095948.3154-4-marcandre.lureau@redhat.com>
[Drop incorrect error_setg() from qmp_input_type_any() and
qmp_input_type_null()]
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
Marc-André Lureau 2016-09-30 13:59:48 +04:00 committed by Markus Armbruster
parent eac8e79ff7
commit 1382d4abdf
2 changed files with 49 additions and 20 deletions

View File

@ -56,7 +56,7 @@ static QmpInputVisitor *to_qiv(Visitor *v)
static QObject *qmp_input_get_object(QmpInputVisitor *qiv, static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
const char *name, const char *name,
bool consume) bool consume, Error **errp)
{ {
StackObject *tos; StackObject *tos;
QObject *qobj; QObject *qobj;
@ -80,6 +80,9 @@ static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
bool removed = g_hash_table_remove(tos->h, name); bool removed = g_hash_table_remove(tos->h, name);
assert(removed); assert(removed);
} }
if (!ret) {
error_setg(errp, QERR_MISSING_PARAMETER, name);
}
} else { } else {
assert(qobject_type(qobj) == QTYPE_QLIST); assert(qobject_type(qobj) == QTYPE_QLIST);
assert(!name); assert(!name);
@ -165,13 +168,16 @@ static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
size_t size, Error **errp) size_t size, Error **errp)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true); QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
Error *err = NULL; Error *err = NULL;
if (obj) { if (obj) {
*obj = NULL; *obj = NULL;
} }
if (!qobj || qobject_type(qobj) != QTYPE_QDICT) { if (!qobj) {
return;
}
if (qobject_type(qobj) != QTYPE_QDICT) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"QDict"); "QDict");
return; return;
@ -193,10 +199,13 @@ static void qmp_input_start_list(Visitor *v, const char *name,
GenericList **list, size_t size, Error **errp) GenericList **list, size_t size, Error **errp)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true); QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
const QListEntry *entry; const QListEntry *entry;
if (!qobj || qobject_type(qobj) != QTYPE_QLIST) { if (!qobj) {
return;
}
if (qobject_type(qobj) != QTYPE_QLIST) {
if (list) { if (list) {
*list = NULL; *list = NULL;
} }
@ -234,11 +243,10 @@ static void qmp_input_start_alternate(Visitor *v, const char *name,
bool promote_int, Error **errp) bool promote_int, Error **errp)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, false); QObject *qobj = qmp_input_get_object(qiv, name, false, errp);
if (!qobj) { if (!qobj) {
*obj = NULL; *obj = NULL;
error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
return; return;
} }
*obj = g_malloc0(size); *obj = g_malloc0(size);
@ -252,8 +260,13 @@ static void qmp_input_type_int64(Visitor *v, const char *name, int64_t *obj,
Error **errp) Error **errp)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true)); QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
QInt *qint;
if (!qobj) {
return;
}
qint = qobject_to_qint(qobj);
if (!qint) { if (!qint) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"integer"); "integer");
@ -268,8 +281,13 @@ static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
{ {
/* FIXME: qobject_to_qint mishandles values over INT64_MAX */ /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true)); QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
QInt *qint;
if (!qobj) {
return;
}
qint = qobject_to_qint(qobj);
if (!qint) { if (!qint) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"integer"); "integer");
@ -283,8 +301,13 @@ static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
Error **errp) Error **errp)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QBool *qbool = qobject_to_qbool(qmp_input_get_object(qiv, name, true)); QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
QBool *qbool;
if (!qobj) {
return;
}
qbool = qobject_to_qbool(qobj);
if (!qbool) { if (!qbool) {
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"boolean"); "boolean");
@ -298,10 +321,15 @@ static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
Error **errp) Error **errp)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QString *qstr = qobject_to_qstring(qmp_input_get_object(qiv, name, true)); QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
QString *qstr;
*obj = NULL;
if (!qobj) {
return;
}
qstr = qobject_to_qstring(qobj);
if (!qstr) { if (!qstr) {
*obj = NULL;
error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
"string"); "string");
return; return;
@ -314,10 +342,13 @@ static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
Error **errp) Error **errp)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true); QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
QInt *qint; QInt *qint;
QFloat *qfloat; QFloat *qfloat;
if (!qobj) {
return;
}
qint = qobject_to_qint(qobj); qint = qobject_to_qint(qobj);
if (qint) { if (qint) {
*obj = qint_get_int(qobject_to_qint(qobj)); *obj = qint_get_int(qobject_to_qint(qobj));
@ -338,11 +369,10 @@ static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
Error **errp) Error **errp)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true); QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
*obj = NULL;
if (!qobj) { if (!qobj) {
error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
*obj = NULL;
return; return;
} }
@ -353,10 +383,9 @@ static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
static void qmp_input_type_null(Visitor *v, const char *name, Error **errp) static void qmp_input_type_null(Visitor *v, const char *name, Error **errp)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, true); QObject *qobj = qmp_input_get_object(qiv, name, true, errp);
if (!qobj) { if (!qobj) {
error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
return; return;
} }
@ -369,7 +398,7 @@ static void qmp_input_type_null(Visitor *v, const char *name, Error **errp)
static void qmp_input_optional(Visitor *v, const char *name, bool *present) static void qmp_input_optional(Visitor *v, const char *name, bool *present)
{ {
QmpInputVisitor *qiv = to_qiv(v); QmpInputVisitor *qiv = to_qiv(v);
QObject *qobj = qmp_input_get_object(qiv, name, false); QObject *qobj = qmp_input_get_object(qiv, name, false, NULL);
if (!qobj) { if (!qobj) {
*present = false; *present = false;

View File

@ -56,7 +56,7 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 encryption=on
Testing: -S Testing: -S
QMP_VERSION QMP_VERSION
{"return": {}} {"return": {}}
{"error": {"class": "GenericError", "desc": "Invalid parameter type for 'driver', expected: string"}} {"error": {"class": "GenericError", "desc": "Parameter 'driver' is missing"}}
{"return": {}} {"return": {}}
{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN"} {"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN"}