From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42596) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bn0WK-0003yC-PK for qemu-devel@nongnu.org; Thu, 22 Sep 2016 05:42:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bn0WG-0001HK-DX for qemu-devel@nongnu.org; Thu, 22 Sep 2016 05:42:19 -0400 From: Markus Armbruster References: <20160921194126.10223-1-marcandre.lureau@redhat.com> Date: Thu, 22 Sep 2016 11:42:12 +0200 In-Reply-To: <20160921194126.10223-1-marcandre.lureau@redhat.com> (=?utf-8?Q?=22Marc-Andr=C3=A9?= Lureau"'s message of "Wed, 21 Sep 2016 23:41:26 +0400") Message-ID: <8760ponudn.fsf@dusky.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH] qmp: fix object-add assert() without props List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?utf-8?Q?Marc-Andr=C3=A9?= Lureau Cc: qemu-devel@nongnu.org, qemu-stable@nongnu.org Marc-Andr=C3=A9 Lureau writes: > Since commit ad739706bbadee49, user_creatable_add_type() expects to be > given a qdict. However, if object-add is called without props, you reach > the assert: "qemu/qom/object_interfaces.c:115: user_creatable_add_type: > Assertion `qdict' failed.", because the qdict isn't created in this > case (it's optional). > > Furthermore, qmp_input_visitor_new() is not meant to be called without a > dict, and a further commit will assert in this situation. > > If none given, create an empty qdict in qmp to avoid the > user_creatable_add_type() assert(qdict). > > Signed-off-by: Marc-Andr=C3=A9 Lureau > --- > qmp.c | 9 +++++++-- > 1 file changed, 7 insertions(+), 2 deletions(-) > > diff --git a/qmp.c b/qmp.c > index 6733463..8078038 100644 > --- a/qmp.c > +++ b/qmp.c > @@ -665,7 +665,7 @@ void qmp_add_client(const char *protocol, const char = *fdname, > void qmp_object_add(const char *type, const char *id, > bool has_props, QObject *props, Error **errp) > { > - const QDict *pdict =3D NULL; > + QDict *pdict; > Visitor *v; > Object *obj; >=20=20 > @@ -675,14 +675,19 @@ void qmp_object_add(const char *type, const char *i= d, > error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict= "); > return; > } > + } else { > + pdict =3D qdict_new(); > } >=20=20 > - v =3D qmp_input_visitor_new(props, true); > + v =3D qmp_input_visitor_new(QOBJECT(pdict), true); > obj =3D user_creatable_add_type(type, id, pdict, v, errp); > visit_free(v); > if (obj) { > object_unref(obj); > } > + if (!props) { > + qobject_decref(QOBJECT(pdict)); > + } > } >=20=20 > void qmp_object_del(const char *id, Error **errp) The reference counting here is needlessly complex. Case props !=3D NULL: * The caller holds a reference to props * pdict =3D qobject_to_qdict(props) is a weak reference (not counted) Case props =3D=3D NULL: * pdict =3D qdict_new() is a counted reference * When it goes out of scope, we need to decrement the reference count. I recommend to avoid the dual nature of pdict, by converting the weak reference into a strong one, then decrement the reference count unconditionally: if (props) { pdict =3D qobject_to_qdict(props); if (!pdict) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict"); return; } QINCREF(pdict); } else { pdict =3D qdict_new(); } v =3D qmp_input_visitor_new(QOBJECT(pdict), true); obj =3D user_creatable_add_type(type, id, pdict, v, errp); visit_free(v); if (obj) { object_unref(obj); } QDECREF(pdict);