All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-stable@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] qmp: fix object-add assert() without props
Date: Thu, 22 Sep 2016 11:42:12 +0200	[thread overview]
Message-ID: <8760ponudn.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20160921194126.10223-1-marcandre.lureau@redhat.com> ("Marc-André Lureau"'s message of "Wed, 21 Sep 2016 23:41:26 +0400")

Marc-André Lureau <marcandre.lureau@redhat.com> 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é Lureau <marcandre.lureau@redhat.com>
> ---
>  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 = NULL;
> +    QDict *pdict;
>      Visitor *v;
>      Object *obj;
>  
> @@ -675,14 +675,19 @@ void qmp_object_add(const char *type, const char *id,
>              error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
>              return;
>          }
> +    } else {
> +        pdict = qdict_new();
>      }
>  
> -    v = qmp_input_visitor_new(props, true);
> +    v = qmp_input_visitor_new(QOBJECT(pdict), true);
>      obj = user_creatable_add_type(type, id, pdict, v, errp);
>      visit_free(v);
>      if (obj) {
>          object_unref(obj);
>      }
> +    if (!props) {
> +        qobject_decref(QOBJECT(pdict));
> +    }
>  }
>  
>  void qmp_object_del(const char *id, Error **errp)

The reference counting here is needlessly complex.

Case props != NULL:

* The caller holds a reference to props

* pdict = qobject_to_qdict(props) is a weak reference (not counted)

Case props == NULL:

* pdict = 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 = qobject_to_qdict(props);
        if (!pdict) {
            error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
            return;
        }
        QINCREF(pdict);
    } else {
        pdict = qdict_new();
    }

    v = qmp_input_visitor_new(QOBJECT(pdict), true);
    obj = user_creatable_add_type(type, id, pdict, v, errp);
    visit_free(v);
    if (obj) {
        object_unref(obj);
    }
    QDECREF(pdict);

      parent reply	other threads:[~2016-09-22  9:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-21 19:41 [Qemu-devel] [PATCH] qmp: fix object-add assert() without props Marc-André Lureau
2016-09-21 19:53 ` Eric Blake
2016-09-21 20:05 ` Paolo Bonzini
2016-09-22  8:03 ` Daniel P. Berrange
2016-09-22  9:42 ` Markus Armbruster [this message]

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=8760ponudn.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.