From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: mdroth@linux.vnet.ibm.com, lcapitulino@redhat.com
Subject: [Qemu-devel] [PATCH 6/6] qstring: Make conversion from QObject * accept null
Date: Thu, 15 Oct 2015 16:15:37 +0200 [thread overview]
Message-ID: <1444918537-18107-7-git-send-email-armbru@redhat.com> (raw)
In-Reply-To: <1444918537-18107-1-git-send-email-armbru@redhat.com>
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 <armbru@redhat.com>
---
qapi/qmp-input-visitor.c | 6 +++---
qobject/qdict.c | 11 +++--------
qobject/qstring.c | 4 ++--
3 files changed, 8 insertions(+), 13 deletions(-)
diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c
index 267783c..eb6e110 100644
--- a/qapi/qmp-input-visitor.c
+++ b/qapi/qmp-input-visitor.c
@@ -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,
diff --git a/qobject/qdict.c b/qobject/qdict.c
index 97e881b..2d67bf1 100644
--- a/qobject/qdict.c
+++ b/qobject/qdict.c
@@ -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;
}
/**
diff --git a/qobject/qstring.c b/qobject/qstring.c
index 607b7a1..cb72dfb 100644
--- a/qobject/qstring.c
+++ b/qobject/qstring.c
@@ -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);
}
--
2.4.3
next prev parent reply other threads:[~2015-10-15 14:15 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-15 14:15 [Qemu-devel] [PATCH 0/6] qobject: Make conversion from QObject * accept null Markus Armbruster
2015-10-15 14:15 ` [Qemu-devel] [PATCH 1/6] qobject: Drop QObject_HEAD Markus Armbruster
2015-10-15 15:24 ` Eric Blake
2015-10-15 14:15 ` [Qemu-devel] [PATCH 2/6] qbool: Make conversion from QObject * accept null Markus Armbruster
2015-10-15 15:27 ` Eric Blake
2015-10-15 14:15 ` [Qemu-devel] [PATCH 3/6] qdict: " Markus Armbruster
2015-10-15 15:30 ` Eric Blake
2015-10-15 14:15 ` [Qemu-devel] [PATCH 4/6] qfloat qint: " Markus Armbruster
2015-10-15 16:09 ` Eric Blake
2015-10-15 14:15 ` [Qemu-devel] [PATCH 5/6] qlist: " Markus Armbruster
2015-10-15 16:10 ` Eric Blake
2015-10-15 14:15 ` Markus Armbruster [this message]
2015-10-15 16:11 ` [Qemu-devel] [PATCH 6/6] qstring: " Eric Blake
2015-10-15 16:06 ` [Qemu-devel] [PATCH 0/6] qobject: " Luiz Capitulino
2015-10-22 12:20 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1444918537-18107-7-git-send-email-armbru@redhat.com \
--to=armbru@redhat.com \
--cc=lcapitulino@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).