From: Eduardo Habkost <ehabkost@redhat.com>
To: Igor Mammedov <imammedo@redhat.com>
Cc: Thomas Huth <thuth@redhat.com>,
qemu-devel@nongnu.org, Markus Armbruster <armbru@redhat.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 1/2] qdev_monitor: Simplify error handling in qdev_device_add()
Date: Thu, 5 Oct 2017 14:11:59 -0300 [thread overview]
Message-ID: <20171005171159.GG4015@localhost.localdomain> (raw)
In-Reply-To: <20171005155912.5871c4d7@nial.brq.redhat.com>
On Thu, Oct 05, 2017 at 03:59:12PM +0200, Igor Mammedov wrote:
> On Thu, 5 Oct 2017 14:32:17 +0200
> Thomas Huth <thuth@redhat.com> wrote:
>
> > Instead of doing the clean-ups on errors multiple times, introduce
> > a jump label at the end of the function that can be used by all
> > error paths that need this cleanup.
> >
> > Suggested-by: Igor Mammedov <imammedo@redhat.com>
> > Signed-off-by: Thomas Huth <thuth@redhat.com>
> > ---
> > qdev-monitor.c | 21 ++++++++++-----------
> > 1 file changed, 10 insertions(+), 11 deletions(-)
> >
> > diff --git a/qdev-monitor.c b/qdev-monitor.c
> > index 8fd6df9..cb2b109 100644
> > --- a/qdev-monitor.c
> > +++ b/qdev-monitor.c
> > @@ -620,22 +620,21 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
> >
> > /* set properties */
> > if (qemu_opt_foreach(opts, set_property, dev, &err)) {
> > - error_propagate(errp, err);
> > - object_unparent(OBJECT(dev));
> > - object_unref(OBJECT(dev));
> > - return NULL;
> > + goto err_del_dev;
> > }
> >
> > dev->opts = opts;
> > object_property_set_bool(OBJECT(dev), true, "realized", &err);
> > - if (err != NULL) {
> > - error_propagate(errp, err);
> > - dev->opts = NULL;
> > - object_unparent(OBJECT(dev));
> > - object_unref(OBJECT(dev));
> > - return NULL;
> > + if (!err) {
> > + return dev;
> > }
> typically the same error check pattern is used through out the function
> so I'd not do inversion here. i.e. keep normal flow non-branched and jump on error
>
> if (err != NULL) {
> goto err_del_dev;
> }
> return dev;
>
> err_del_dev:
> dev->opts = NULL;
I prefer this pattern also. It makes the success/error paths
very easy to spot.
--
Eduardo
next prev parent reply other threads:[~2017-10-05 17:12 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-05 12:32 [Qemu-devel] [PATCH v2 0/2] Check for the availability of a hotplug controller before adding a device Thomas Huth
2017-10-05 12:32 ` [Qemu-devel] [PATCH 1/2] qdev_monitor: Simplify error handling in qdev_device_add() Thomas Huth
2017-10-05 13:59 ` Igor Mammedov
2017-10-05 17:11 ` Eduardo Habkost [this message]
2017-10-05 12:32 ` [Qemu-devel] [PATCH v2 2/2] qdev: Check for the availability of a hotplug controller before adding a device Thomas Huth
2017-10-05 14:13 ` Igor Mammedov
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=20171005171159.GG4015@localhost.localdomain \
--to=ehabkost@redhat.com \
--cc=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=imammedo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=thuth@redhat.com \
/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).