qdict: Make conversion from QObject * accept null

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 <armbru@redhat.com>
Message-Id: <1444918537-18107-4-git-send-email-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
This commit is contained in:
Markus Armbruster 2015-10-15 16:15:34 +02:00
parent 14b6160099
commit 89cad9f3ec
2 changed files with 6 additions and 11 deletions

View File

@ -573,7 +573,6 @@ static void process_command(GAState *s, QDict *req)
static void process_event(JSONMessageParser *parser, QList *tokens) static void process_event(JSONMessageParser *parser, QList *tokens)
{ {
GAState *s = container_of(parser, GAState, parser); GAState *s = container_of(parser, GAState, parser);
QObject *obj;
QDict *qdict; QDict *qdict;
Error *err = NULL; Error *err = NULL;
int ret; int ret;
@ -581,9 +580,9 @@ static void process_event(JSONMessageParser *parser, QList *tokens)
g_assert(s && parser); g_assert(s && parser);
g_debug("process_event: called"); g_debug("process_event: called");
obj = json_parser_parse_err(tokens, NULL, &err); qdict = qobject_to_qdict(json_parser_parse_err(tokens, NULL, &err));
if (err || !obj || qobject_type(obj) != QTYPE_QDICT) { if (err || !qdict) {
qobject_decref(obj); QDECREF(qdict);
qdict = qdict_new(); qdict = qdict_new();
if (!err) { if (!err) {
g_warning("failed to parse event: unknown error"); g_warning("failed to parse event: unknown error");
@ -593,12 +592,8 @@ static void process_event(JSONMessageParser *parser, QList *tokens)
} }
qdict_put_obj(qdict, "error", qmp_build_error_object(err)); qdict_put_obj(qdict, "error", qmp_build_error_object(err));
error_free(err); error_free(err);
} else {
qdict = qobject_to_qdict(obj);
} }
g_assert(qdict);
/* handle host->guest commands */ /* handle host->guest commands */
if (qdict_haskey(qdict, "execute")) { if (qdict_haskey(qdict, "execute")) {
process_command(s, qdict); process_command(s, qdict);

View File

@ -46,9 +46,9 @@ QDict *qdict_new(void)
*/ */
QDict *qobject_to_qdict(const QObject *obj) QDict *qobject_to_qdict(const QObject *obj)
{ {
if (qobject_type(obj) != QTYPE_QDICT) if (!obj || qobject_type(obj) != QTYPE_QDICT) {
return NULL; return NULL;
}
return container_of(obj, QDict, base); 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) 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));
} }
/** /**