qobject: Make conversion from QObject * accept null
cover qobject: Make conversion from QObject * accept null
The qobject_to_FOO() crash on null, which is a trap for the unwary. Return null instead, and simplify a few callers.
Throw in a patch to drop QObject_HEAD.
Luiz, I'm happy to take this through my tree, since got a QMP series based on it (to be posted shortly).
Markus Armbruster (6):
qobject: Drop QObject_HEAD
qbool: Make conversion from QObject * accept null
qdict: Make conversion from QObject * accept null qfloat qint: Make conversion from QObject * accept null
qlist: Make conversion from QObject * accept null
qstring: Make conversion from QObject * accept null
include/qapi/qmp/qbool.h | 2 +- include/qapi/qmp/qdict.h | 2 +- include/qapi/qmp/qfloat.h | 2 +- include/qapi/qmp/qint.h | 2 +- include/qapi/qmp/qlist.h | 2 +- include/qapi/qmp/qobject.h | 4 ---- include/qapi/qmp/qstring.h | 2 +- qapi/qmp-input-visitor.c | 40 ++++++++++++++++++++++------------------ qga/main.c | 11 +++-------- qobject/qbool.c | 4 ++-- qobject/qdict.c | 39 ++++++++++++--------------------------- qobject/qfloat.c | 4 ++-- qobject/qint.c | 4 ++-- qobject/qlist.c | 3 +-- qobject/qstring.c | 4 ++-- 15 files changed, 52 insertions(+), 73 deletions(-)
Signature · 1 line
2.4.3
PATCH 1/6 qobject: Drop QObject_HEAD
QObject_HEAD is a macro expanding into the common part of structs that are sub-types of QObject. It's always been just QObject base, and unlikely to change. Drop the macro, because the code is clearer with out it.
--- include/qapi/qmp/qbool.h | 2 +- include/qapi/qmp/qdict.h | 2 +- include/qapi/qmp/qfloat.h | 2 +- include/qapi/qmp/qint.h | 2 +- include/qapi/qmp/qlist.h | 2 +- include/qapi/qmp/qobject.h | 4 ---- include/qapi/qmp/qstring.h | 2 +- 7 files changed, 6 insertions(+), 10 deletions(-)
include/qapi/qmp/qbool.h +1 -1
@@ -18,7 +18,7 @@#include "qapi/qmp/qobject.h" typedef struct QBool { -QObject_HEAD ; +QObject base;bool value; }QBool ;
include/qapi/qmp/qdict.h +1 -1
@@ -28,7 +28,7 @@ typedef struct QDictEntry { } QDictEntry;typedef struct QDict { -QObject_HEAD ; +QObject base;size_t size; QLIST_HEAD(,QDictEntry) table[QDICT_BUCKET_MAX]; }QDict ;
include/qapi/qmp/qfloat.h +1 -1
@@ -18,7 +18,7 @@#include "qapi/qmp/qobject.h" typedef struct QFloat { -QObject_HEAD ; +QObject base;double value; }QFloat ;
include/qapi/qmp/qint.h +1 -1
@@ -17,7 +17,7 @@#include "qapi/qmp/qobject.h" typedef struct QInt { -QObject_HEAD ; +QObject base;int64_t value; }QInt ;
include/qapi/qmp/qlist.h +1 -1
@@ -22,7 +22,7 @@ typedef struct QListEntry { } QListEntry;typedef struct QList { -QObject_HEAD ; +QObject base; QTAILQ_HEAD(,QListEntry) head; }QList ;
include/qapi/qmp/qobject.h +0 -4
@@ -59,10 +59,6 @@ typedef struct QObject {size_t refcnt; } QObject; -/* Objects definitions must include this */ -#define QObject_HEAD \ - QObject base -/* Get the 'base' part of an object */ #define QOBJECT (obj) (&(obj)->base)
include/qapi/qmp/qstring.h +1 -1
@@ -17,7 +17,7 @@#include "qapi/qmp/qobject.h" typedef struct QString { - QObject_HEAD; + QObject base; char *string; size_t length; size_t capacity;
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 1/6] qobject: Drop QObject_HEAD
Quoted 6 lines · Markus Armbruster
QObject_HEADis a macro expanding into the common part of structs that are sub-types of QObject. It's always been just QObject base, and unlikely to change. Drop the macro, because the code is clearer with out it.Signed-off-by Markus Armbruster <[email protected]>
Quoted 10 lines
--- include/qapi/qmp/qbool.h | 2 +- include/qapi/qmp/qdict.h | 2 +- include/qapi/qmp/qfloat.h | 2 +- include/qapi/qmp/qint.h | 2 +- include/qapi/qmp/qlist.h | 2 +- include/qapi/qmp/qobject.h | 4 ---- include/qapi/qmp/qstring.h | 2 +- 7 files changed, 6 insertions(+), 10 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 2/6 qbool: Make conversion from QObject * accept null
In reply to Markus Armbrusterqobject_to_qbool() crashes on null, which is a trap for the unwary. Return null instead, and simplify a few callers.
--- qapi/qmp-input-visitor.c | 6 +++--- qobject/qbool.c | 4 ++-- qobject/qdict.c | 11 +++-------- 3 files changed, 8 insertions(+), 13 deletions(-)
qapi/qmp-input-visitor.c +3 -3
@@ -240,15 +240,15 @@ static void qmp_input_type_bool(Visitor *v, bool *obj, const char *name, Error **errp) {QmpInputVisitor *qiv =to_qiv (v); -QObject *qobj =qmp_input_get_object (qiv, name, true); +QBool *qbool =qobject_to_qbool (qmp_input_get_object (qiv, name, true)); -if (!qobj ||qobject_type (qobj) !=QTYPE_QBOOL ) { +if (!qbool) {error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" ,"boolean" );return ; } - *obj =qbool_get_bool (qobject_to_qbool (qobj)); + *obj =qbool_get_bool (qbool); } static void qmp_input_type_str(Visitor *v, char **obj, const char *name,
qobject/qbool.c +2 -2
@@ -51,9 +51,9 @@ bool qbool_get_bool(const QBool *qb)*/ QBool *qobject_to_qbool (const QObject *obj) { -if (qobject_type (obj) !=QTYPE_QBOOL ) +if (!obj ||qobject_type (obj) !=QTYPE_QBOOL ) {return NULL ; - + }return container_of (obj, QBool, base); }
qobject/qdict.c +3 -8
@@ -243,8 +243,7 @@ int64_t qdict_get_int(const QDict *qdict, const char *key)*/ bool qdict_get_bool (const QDict *qdict,const char *key) { -QObject *obj =qdict_get_obj (qdict, key,QTYPE_QBOOL ); -return qbool_get_bool (qobject_to_qbool (obj)); +return qbool_get_bool (qobject_to_qbool (qdict_get (qdict, key))); }/**
@@ -316,13 +315,9 @@ int64_t qdict_get_try_int(const QDict *qdict, const char *key,*/ bool qdict_get_try_bool (const QDict *qdict,const char *key,bool def_value) { -QObject *obj; +QBool *qbool =qobject_to_qbool (qdict_get (qdict, key)); - obj =qdict_get (qdict, key); -if (!obj ||qobject_type (obj) !=QTYPE_QBOOL ) -return def_value; - -return qbool_get_bool (qobject_to_qbool (obj)); +return qbool ?qbool_get_bool (qbool) : def_value; }/**
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 2/6] qbool: Make conversion from QObject * accept null
Quoted 10 lines · Markus Armbruster
qobject_to_qbool()crashes on null, which is a trap for the unwary. Return null instead, and simplify a few callers.Signed-off-by Markus Armbruster <[email protected]>--- qapi/qmp-input-visitor.c | 6 +++--- qobject/qbool.c | 4 ++-- qobject/qdict.c | 11 +++-------- 3 files changed, 8 insertions(+), 13 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 3/6 qdict: Make conversion from QObject * accept null
In reply to Markus Armbrusterqobject_to_qdict() crashes on null, which is a trap for the unwary. Return null instead, and simplify a few callers.
--- qga/main.c | 11 +++-------- qobject/qdict.c | 6 +++--- 2 files changed, 6 insertions(+), 11 deletions(-)
qga/main.c +3 -8
@@ -567,7 +567,6 @@ static void process_command(GAState *s, QDict *req)static void process_event (JSONMessageParser *parser,QList *tokens) {GAState *s =container_of (parser, GAState, parser); -QObject *obj;QDict *qdict;Error *err =NULL ;int ret;
@@ -575,9 +574,9 @@ static void process_event(JSONMessageParser *parser, QList *tokens)g_assert (s && parser);g_debug ("process_event: called" ); - obj =json_parser_parse_err (tokens,NULL , &err); -if (err || !obj ||qobject_type (obj) !=QTYPE_QDICT ) { -qobject_decref (obj); + qdict =qobject_to_qdict (json_parser_parse_err (tokens,NULL , &err)); +if (err || !qdict) { +QDECREF (qdict); qdict =qdict_new ();if (!err) {g_warning ("failed to parse event: unknown error" );
@@ -587,12 +586,8 @@ static void process_event(JSONMessageParser *parser, QList *tokens) }qdict_put_obj (qdict,"error" ,qmp_build_error_object (err));error_free (err); - }else { - qdict =qobject_to_qdict (obj); } -g_assert (qdict); -/* handle host->guest commands */ if (qdict_haskey (qdict,"execute" )) {process_command (s, qdict);
qobject/qdict.c +3 -3
@@ -46,9 +46,9 @@ QDict *qdict_new(void)*/ QDict *qobject_to_qdict (const QObject *obj) { -if (qobject_type (obj) !=QTYPE_QDICT ) +if (!obj ||qobject_type (obj) !=QTYPE_QDICT ) {return NULL ; - + }return container_of (obj, QDict, base); }
@@ -269,7 +269,7 @@ QList *qdict_get_qlist(const QDict *qdict, const char *key)*/ QDict *qdict_get_qdict (const QDict *qdict,const char *key) { -return qobject_to_qdict (qdict_get_obj (qdict, key,QTYPE_QDICT )); +return qobject_to_qdict (qdict_get (qdict, key)); }/**
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 3/6] qdict: Make conversion from QObject * accept null
Quoted 9 lines · Markus Armbruster
qobject_to_qdict()crashes on null, which is a trap for the unwary. Return null instead, and simplify a few callers.Signed-off-by Markus Armbruster <[email protected]>--- qga/main.c | 11 +++-------- qobject/qdict.c | 6 +++--- 2 files changed, 6 insertions(+), 11 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 4/6 qfloat qint: Make conversion from QObject * accept null
In reply to Markus Armbrusterqobject_to_qfloat() and qobject_to_qint() crash on null, which is a trap for the unwary. Return null instead, and simplify a few callers.
--- qapi/qmp-input-visitor.c | 28 ++++++++++++++++------------ qobject/qdict.c | 11 +++-------- qobject/qfloat.c | 4 ++-- qobject/qint.c | 4 ++-- 4 files changed, 23 insertions(+), 24 deletions(-)
qapi/qmp-input-visitor.c +16 -12
@@ -225,15 +225,15 @@ static void qmp_input_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp) {QmpInputVisitor *qiv =to_qiv (v); -QObject *qobj =qmp_input_get_object (qiv, name, true); +QInt *qint =qobject_to_qint (qmp_input_get_object (qiv, name, true)); -if (!qobj ||qobject_type (qobj) !=QTYPE_QINT ) { +if (!qint) {error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" ,"integer" );return ; } - *obj =qint_get_int (qobject_to_qint (qobj)); + *obj =qint_get_int (qint); } static void qmp_input_type_bool(Visitor *v, bool *obj, const char *name,
@@ -271,19 +271,23 @@ static void qmp_input_type_number(Visitor *v, double *obj, const char *name, {QmpInputVisitor *qiv =to_qiv (v);QObject *qobj =qmp_input_get_object (qiv, name, true); +QInt *qint; +QFloat *qfloat; -if (!qobj || (qobject_type (qobj) !=QTYPE_QFLOAT && -qobject_type (qobj) !=QTYPE_QINT )) { -error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" , -"number" ); -return ; - } - -if (qobject_type (qobj) ==QTYPE_QINT ) { + qint =qobject_to_qint (qobj); +if (qint) { *obj =qint_get_int (qobject_to_qint (qobj)); - }else { +return ; + } + + qfloat =qobject_to_qfloat (qobj); +if (qfloat) { *obj =qfloat_get_double (qobject_to_qfloat (qobj)); +return ; } + +error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" , +"number" ); } static void qmp_input_type_any(Visitor *v, QObject **obj, const char *name,
qobject/qdict.c +3 -8
@@ -229,8 +229,7 @@ double qdict_get_double(const QDict *qdict, const char *key)*/ int64_t qdict_get_int (const QDict *qdict,const char *key) { -QObject *obj =qdict_get_obj (qdict, key,QTYPE_QINT ); -return qint_get_int (qobject_to_qint (obj)); +return qint_get_int (qobject_to_qint (qdict_get (qdict, key))); }/**
@@ -297,13 +296,9 @@ const char *qdict_get_str(const QDict *qdict, const char *key)int64_t qdict_get_try_int (const QDict *qdict,const char *key,int64_t def_value) { -QObject *obj; +QInt *qint =qobject_to_qint (qdict_get (qdict, key)); - obj =qdict_get (qdict, key); -if (!obj ||qobject_type (obj) !=QTYPE_QINT ) -return def_value; - -return qint_get_int (qobject_to_qint (obj)); +return qint ?qint_get_int (qint) : def_value; }/**
qobject/qfloat.c +2 -2
@@ -51,9 +51,9 @@ double qfloat_get_double(const QFloat *qf)*/ QFloat *qobject_to_qfloat (const QObject *obj) { -if (qobject_type (obj) !=QTYPE_QFLOAT ) +if (!obj ||qobject_type (obj) !=QTYPE_QFLOAT ) {return NULL ; - + }return container_of (obj, QFloat, base); }
qobject/qint.c +2 -2
@@ -50,9 +50,9 @@ int64_t qint_get_int(const QInt *qi)*/ QInt *qobject_to_qint (const QObject *obj) { -if (qobject_type (obj) !=QTYPE_QINT ) +if (!obj ||qobject_type (obj) !=QTYPE_QINT ) {return NULL ; - + }return container_of (obj, QInt, base); }
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 4/6] qfloat qint: Make conversion from QObject * accept null
Quoted 11 lines · Markus Armbruster
qobject_to_qfloat()andqobject_to_qint()crash on null, which is a trap for the unwary. Return null instead, and simplify a few callers.Signed-off-by Markus Armbruster <[email protected]>--- qapi/qmp-input-visitor.c | 28 ++++++++++++++++------------ qobject/qdict.c | 11 +++-------- qobject/qfloat.c | 4 ++-- qobject/qint.c | 4 ++-- 4 files changed, 23 insertions(+), 24 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 5/6 qlist: Make conversion from QObject * accept null
In reply to Markus Armbrusterqobject_to_qlist() crashes on null, which is a trap for the unwary. Return null instead.
--- qobject/qlist.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
qobject/qlist.c +1 -2
@@ -142,10 +142,9 @@ size_t qlist_size(const QList *qlist)*/ QList *qobject_to_qlist (const QObject *obj) { -if (qobject_type (obj) !=QTYPE_QLIST ) { +if (!obj ||qobject_type (obj) !=QTYPE_QLIST ) {return NULL ; } -return container_of (obj, QList, base); }
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 5/6] qlist: Make conversion from QObject * accept null
Quoted 8 lines · Markus Armbruster
qobject_to_qlist()crashes on null, which is a trap for the unwary. Return null instead.Signed-off-by Markus Armbruster <[email protected]>--- qobject/qlist.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 6/6 qstring: Make conversion from QObject * accept null
In reply to Markus Armbrusterqobject_to_qstring() crashes on null, which is a trap for the unwary. Return null instead, and simplify a few callers.
--- qapi/qmp-input-visitor.c | 6 +++--- qobject/qdict.c | 11 +++-------- qobject/qstring.c | 4 ++-- 3 files changed, 8 insertions(+), 13 deletions(-)
qapi/qmp-input-visitor.c +3 -3
@@ -255,15 +255,15 @@ static void qmp_input_type_str(Visitor *v, char **obj, const char *name, Error **errp) {QmpInputVisitor *qiv =to_qiv (v); -QObject *qobj =qmp_input_get_object (qiv, name, true); +QString *qstr =qobject_to_qstring (qmp_input_get_object (qiv, name, true)); -if (!qobj ||qobject_type (qobj) !=QTYPE_QSTRING ) { +if (!qstr) {error_setg (errp,QERR_INVALID_PARAMETER_TYPE , name ? name :"null" ,"string" );return ; } - *obj =g_strdup (qstring_get_str (qobject_to_qstring (qobj))); + *obj =g_strdup (qstring_get_str (qstr)); } static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
qobject/qdict.c +3 -8
@@ -282,8 +282,7 @@ QDict *qdict_get_qdict(const QDict *qdict, const char *key)*/ const char *qdict_get_str (const QDict *qdict,const char *key) { -QObject *obj =qdict_get_obj (qdict, key,QTYPE_QSTRING ); -return qstring_get_str (qobject_to_qstring (obj)); +return qstring_get_str (qobject_to_qstring (qdict_get (qdict, key))); }/**
@@ -325,13 +324,9 @@ bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value)*/ const char *qdict_get_try_str (const QDict *qdict,const char *key) { -QObject *obj; +QString *qstr =qobject_to_qstring (qdict_get (qdict, key)); - obj =qdict_get (qdict, key); -if (!obj ||qobject_type (obj) !=QTYPE_QSTRING ) -return NULL ; - -return qstring_get_str (qobject_to_qstring (obj)); +return qstr ?qstring_get_str (qstr) :NULL ; }/**
qobject/qstring.c +2 -2
@@ -117,9 +117,9 @@ void qstring_append_chr(QString *qstring, int c)*/ QString *qobject_to_qstring (const QObject *obj) { -if (qobject_type (obj) !=QTYPE_QSTRING ) +if (!obj ||qobject_type (obj) !=QTYPE_QSTRING ) {return NULL ; - + }return container_of (obj, QString, base); }
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH 6/6] qstring: Make conversion from QObject * accept null
qapi/qmp-input-visitor.c
Quoted 11 lines · Markus Armbruster
qobject_to_qstring()crashes on null, which is a trap for the unwary. Return null instead, and simplify a few callers.Signed-off-by Markus Armbruster <[email protected]>--- qapi/qmp-input-visitor.c | 6 +++--- qobject/qdict.c | 11 +++-------- qobject/qstring.c | 4 ++-- 3 files changed, 8 insertions(+), 13 deletions(-)qapi/qmp-input-visitor.c +0
-0
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 7 lines · On Thu, 15 Oct 2015 16:15:31 +0200 Markus Armbruster <[email protected]> wrote:
The
qobject_to_FOO()crash on null, which is a trap for the unwary. Return null instead, and simplify a few callers.Throw in a patch to drop
QObject_HEAD.Luiz, I'm happy to take this through my tree, since got a QMP series based on it (to be posted shortly).
Please do.
Btw, what do you think about this patch? :)
MAINTAINERS +2 -2
@@ -1010,7 +1010,7 @@ F: qemu-timer.c F: vl.c Human Monitor (HMP) -M: Luiz Capitulino <[email protected]> +M: Markus Armbruster <[email protected]> S: Maintained F: monitor.c F: hmp.c
@@ -1073,7 +1073,7 @@ F: qapi/*.json T: git git://repo.or.cz/qemu/armbru.git qapi-next QObject -M: Luiz Capitulino <[email protected]> +M: Markus Armbruster <[email protected]> S: Maintained F: qobject/ F: include/qapi/qmp/
Quoted 6 lines · Luiz Capitulino
Quoted 3 lines · On Thu, 15 Oct 2015 16:15:31 +0200 Markus Armbruster <[email protected]> wrote:
The
qobject_to_FOO()crash on null, which is a trap for the unwary. Return null instead, and simplify a few callers.
Quoted 8 lines
Quoted 4 lines
Throw in a patch to drop
QObject_HEAD.Luiz, I'm happy to take this through my tree, since got a QMP series based on it (to be posted shortly).
Please do.
Reviewed-by Luiz Capitulino <[email protected]>
Thanks!
Quoted 1 line
Btw, what do you think about this patch? :)
I knew I was playing with fire %-}
Right now I'm too swamped with QAPI patches to take on more. Once we flushed that queue, and got QAPI to quiet down a bit, I can take HMP and QObject off you.