From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55263) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmetL-0005lv-GU for qemu-devel@nongnu.org; Wed, 21 Sep 2016 06:36:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bmetJ-0005Cu-2L for qemu-devel@nongnu.org; Wed, 21 Sep 2016 06:36:38 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45076) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmetI-0005Ca-Po for qemu-devel@nongnu.org; Wed, 21 Sep 2016 06:36:37 -0400 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Wed, 21 Sep 2016 14:36:27 +0400 Message-Id: <20160921103629.6410-2-marcandre.lureau@redhat.com> In-Reply-To: <20160921103629.6410-1-marcandre.lureau@redhat.com> References: <20160921103629.6410-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 1/3] qapi: return a 'missing parameter' error List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: berto@igalia.com, eblake@redhat.com, armbru@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= 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_INVALID_PARAMETER_TYPE where appropriate. Signed-off-by: Marc-Andr=C3=A9 Lureau --- qapi/qmp-input-visitor.c | 109 +++++++++++++++++++++++++++++++----------= ------ 1 file changed, 73 insertions(+), 36 deletions(-) diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c index 64dd392..ea9972d 100644 --- a/qapi/qmp-input-visitor.c +++ b/qapi/qmp-input-visitor.c @@ -56,7 +56,7 @@ static QmpInputVisitor *to_qiv(Visitor *v) =20 static QObject *qmp_input_get_object(QmpInputVisitor *qiv, const char *name, - bool consume) + bool consume, Error **errp) { StackObject *tos; QObject *qobj; @@ -64,30 +64,34 @@ static QObject *qmp_input_get_object(QmpInputVisitor = *qiv, =20 if (QSLIST_EMPTY(&qiv->stack)) { /* Starting at root, name is ignored. */ - return qiv->root; - } - - /* We are in a container; find the next element. */ - tos =3D QSLIST_FIRST(&qiv->stack); - qobj =3D tos->obj; - assert(qobj); - - if (qobject_type(qobj) =3D=3D QTYPE_QDICT) { - assert(name); - ret =3D qdict_get(qobject_to_qdict(qobj), name); - if (tos->h && consume && ret) { - bool removed =3D g_hash_table_remove(tos->h, name); - assert(removed); - } + ret =3D qiv->root; } else { - assert(qobject_type(qobj) =3D=3D QTYPE_QLIST); - assert(!name); - ret =3D qlist_entry_obj(tos->entry); - if (consume) { - tos->entry =3D qlist_next(tos->entry); + /* We are in a container; find the next element. */ + tos =3D QSLIST_FIRST(&qiv->stack); + qobj =3D tos->obj; + assert(qobj); + + if (qobject_type(qobj) =3D=3D QTYPE_QDICT) { + assert(name); + ret =3D qdict_get(qobject_to_qdict(qobj), name); + if (tos->h && consume && ret) { + bool removed =3D g_hash_table_remove(tos->h, name); + assert(removed); + } + } else { + assert(qobject_type(qobj) =3D=3D QTYPE_QLIST); + assert(!name); + ret =3D qlist_entry_obj(tos->entry); + if (consume) { + tos->entry =3D qlist_next(tos->entry); + } } } =20 + if (!ret) { + error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null"); + } + return ret; } =20 @@ -163,13 +167,16 @@ static void qmp_input_start_struct(Visitor *v, cons= t char *name, void **obj, size_t size, Error **errp) { QmpInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qmp_input_get_object(qiv, name, true); + QObject *qobj =3D qmp_input_get_object(qiv, name, true, errp); Error *err =3D NULL; =20 if (obj) { *obj =3D NULL; } - if (!qobj || qobject_type(qobj) !=3D QTYPE_QDICT) { + if (!qobj) { + return; + } + if (qobject_type(qobj) !=3D QTYPE_QDICT) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "nul= l", "QDict"); return; @@ -191,10 +198,13 @@ static void qmp_input_start_list(Visitor *v, const = char *name, GenericList **list, size_t size, Error = **errp) { QmpInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qmp_input_get_object(qiv, name, true); + QObject *qobj =3D qmp_input_get_object(qiv, name, true, errp); const QListEntry *entry; =20 - if (!qobj || qobject_type(qobj) !=3D QTYPE_QLIST) { + if (!qobj) { + return; + } + if (qobject_type(qobj) !=3D QTYPE_QLIST) { if (list) { *list =3D NULL; } @@ -232,11 +242,12 @@ static void qmp_input_start_alternate(Visitor *v, c= onst char *name, bool promote_int, Error **errp) { QmpInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qmp_input_get_object(qiv, name, false); + QObject *qobj =3D qmp_input_get_object(qiv, name, false, errp); =20 - if (!qobj) { + if (obj) { *obj =3D NULL; - error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null"); + } + if (!qobj) { return; } *obj =3D g_malloc0(size); @@ -250,8 +261,12 @@ static void qmp_input_type_int64(Visitor *v, const c= har *name, int64_t *obj, Error **errp) { QmpInputVisitor *qiv =3D to_qiv(v); - QInt *qint =3D qobject_to_qint(qmp_input_get_object(qiv, name, true)= ); + QObject *qobj =3D qmp_input_get_object(qiv, name, true, errp); + QInt *qint =3D qobject_to_qint(qobj); =20 + if (!qobj) { + return; + } if (!qint) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "nul= l", "integer"); @@ -266,8 +281,12 @@ static void qmp_input_type_uint64(Visitor *v, const = char *name, uint64_t *obj, { /* FIXME: qobject_to_qint mishandles values over INT64_MAX */ QmpInputVisitor *qiv =3D to_qiv(v); - QInt *qint =3D qobject_to_qint(qmp_input_get_object(qiv, name, true)= ); + QObject *qobj =3D qmp_input_get_object(qiv, name, true, errp); + QInt *qint =3D qobject_to_qint(qobj); =20 + if (!qobj) { + return; + } if (!qint) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "nul= l", "integer"); @@ -281,8 +300,12 @@ static void qmp_input_type_bool(Visitor *v, const ch= ar *name, bool *obj, Error **errp) { QmpInputVisitor *qiv =3D to_qiv(v); - QBool *qbool =3D qobject_to_qbool(qmp_input_get_object(qiv, name, tr= ue)); + QObject *qobj =3D qmp_input_get_object(qiv, name, true, errp); + QBool *qbool =3D qobject_to_qbool(qobj); =20 + if (!qobj) { + return; + } if (!qbool) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "nul= l", "boolean"); @@ -296,8 +319,12 @@ static void qmp_input_type_str(Visitor *v, const cha= r *name, char **obj, Error **errp) { QmpInputVisitor *qiv =3D to_qiv(v); - QString *qstr =3D qobject_to_qstring(qmp_input_get_object(qiv, name,= true)); + QObject *qobj =3D qmp_input_get_object(qiv, name, true, errp); + QString *qstr =3D qobject_to_qstring(qobj); =20 + if (!qobj) { + return; + } if (!qstr) { *obj =3D NULL; error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "nul= l", @@ -312,10 +339,13 @@ static void qmp_input_type_number(Visitor *v, const= char *name, double *obj, Error **errp) { QmpInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qmp_input_get_object(qiv, name, true); + QObject *qobj =3D qmp_input_get_object(qiv, name, true, errp); QInt *qint; QFloat *qfloat; =20 + if (!qobj) { + return; + } qint =3D qobject_to_qint(qobj); if (qint) { *obj =3D qint_get_int(qobject_to_qint(qobj)); @@ -336,7 +366,11 @@ static void qmp_input_type_any(Visitor *v, const cha= r *name, QObject **obj, Error **errp) { QmpInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qmp_input_get_object(qiv, name, true); + QObject *qobj =3D qmp_input_get_object(qiv, name, true, errp); + + if (!qobj) { + return; + } =20 qobject_incref(qobj); *obj =3D qobj; @@ -345,8 +379,11 @@ static void qmp_input_type_any(Visitor *v, const cha= r *name, QObject **obj, static void qmp_input_type_null(Visitor *v, const char *name, Error **er= rp) { QmpInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qmp_input_get_object(qiv, name, true); + QObject *qobj =3D qmp_input_get_object(qiv, name, true, errp); =20 + if (!qobj) { + return; + } if (qobject_type(qobj) !=3D QTYPE_QNULL) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "nul= l", "null"); @@ -356,7 +393,7 @@ static void qmp_input_type_null(Visitor *v, const cha= r *name, Error **errp) static void qmp_input_optional(Visitor *v, const char *name, bool *prese= nt) { QmpInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qmp_input_get_object(qiv, name, false); + QObject *qobj =3D qmp_input_get_object(qiv, name, false, NULL); =20 if (!qobj) { *present =3D false; --=20 2.10.0