qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Eduardo Habkost <ehabkost@redhat.com>,
	qemu-devel@nongnu.org, Markus Armbruster <armbru@redhat.com>
Cc: kwolf@redhat.com, cornelia.huck@de.ibm.com, mreitz@redhat.com,
	qemu-block@nongnu.org, borntraeger@de.ibm.com
Subject: Re: [Qemu-devel] [PATCH] error: Remove NULL checks on error_propagate() calls
Date: Thu, 9 Jun 2016 13:37:23 -0600	[thread overview]
Message-ID: <5759C573.1070403@redhat.com> (raw)
In-Reply-To: <1465494036-23928-1-git-send-email-ehabkost@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 6145 bytes --]

On 06/09/2016 11:40 AM, Eduardo Habkost wrote:
> error_propagate() already ignores local_err==NULL, so there's no
> need to check it before calling.
> 
> Done using the following Coccinelle patch:
> 
>   @@
>   identifier L;
>   expression E;
>   @@
>   -if (L) {
>        error_propagate(E, L);
>   -}
> 
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
> ---

> diff --git a/block.c b/block.c
> index f54bc25..ecca55a 100644
> --- a/block.c
> +++ b/block.c
> @@ -301,9 +301,7 @@ static void coroutine_fn bdrv_create_co_entry(void *opaque)
>      assert(cco->drv);
>  
>      ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err);
> -    if (local_err) {
> -        error_propagate(&cco->err, local_err);
> -    }
> +    error_propagate(&cco->err, local_err);
>      cco->ret = ret;
>  }

This is a case where we can go one step further:

assert(cco->drv);
cco->ret = cco->drv->bdrv_create(cco->filename, cco->opts, &cco->err);

and ditch local_err and ret altogether.

>  
> @@ -364,9 +362,7 @@ int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
>      }
>  
>      ret = bdrv_create(drv, filename, opts, &local_err);
> -    if (local_err) {
> -        error_propagate(errp, local_err);
> -    }
> +    error_propagate(errp, local_err);
>      return ret;
>  }

Another place where we could ditch local_err, and directly call

return bdrv_create(drv, filename, opts, errp);

Of course, unless you tweak the Coccinelle script to do that cleanup
automatically, or improve the commit message to mention that you did
further cleanups beyond Coccinelle, then it would belong better as a
followup patch, leaving this conversion to be fine to commit as is.

>  
> @@ -1760,18 +1756,14 @@ fail:
>      QDECREF(options);
>      bs->options = NULL;
>      bdrv_unref(bs);
> -    if (local_err) {
> -        error_propagate(errp, local_err);
> -    }
> +    error_propagate(errp, local_err);
>      return NULL;

This one's fine, along with any others I don't comment on.

> +++ b/block/qcow2.c
> @@ -2394,9 +2394,7 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
>      ret = qcow2_create2(filename, size, backing_file, backing_fmt, flags,
>                          cluster_size, prealloc, opts, version, refcount_order,
>                          &local_err);
> -    if (local_err) {
> -        error_propagate(errp, local_err);
> -    }
> +    error_propagate(errp, local_err);

Another case where we could ditch local_err, OR, we could do:

     prealloc = qapi_enum_parse(..., &local_err);
     if (local_err) {
-        error_propagate(errp, local_err);
         ret = -EINVAL;
         goto finish;
     }
...
     ret = qcow2_create2(..., &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
-    }

 finish:
+    error_propagate(errp, local_err);

> +++ b/block/raw-posix.c
> @@ -587,9 +587,7 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags,
>  
>      s->type = FTYPE_FILE;
>      ret = raw_open_common(bs, options, flags, 0, &local_err);
> -    if (local_err) {
> -        error_propagate(errp, local_err);
> -    }
> +    error_propagate(errp, local_err);
>      return ret;
>  }

Another case where we could ditch local_err.

> @@ -2453,9 +2449,7 @@ static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
>  
>      /* open will not fail even if no CD is inserted, so add O_NONBLOCK */
>      ret = raw_open_common(bs, options, flags, O_NONBLOCK, &local_err);
> -    if (local_err) {
> -        error_propagate(errp, local_err);
> -    }
> +    error_propagate(errp, local_err);
>      return ret;

and another.

>  }
>  
> @@ -2573,9 +2567,7 @@ static int cdrom_open(BlockDriverState *bs, QDict *options, int flags,
>  
>      ret = raw_open_common(bs, options, flags, 0, &local_err);
>      if (ret) {
> -        if (local_err) {
> -            error_propagate(errp, local_err);
> -        }
> +        error_propagate(errp, local_err);
>          return ret;
>      }

and another

>  
> diff --git a/block/raw_bsd.c b/block/raw_bsd.c
> index b1d5237..5af11b6 100644
> --- a/block/raw_bsd.c
> +++ b/block/raw_bsd.c
> @@ -194,9 +194,7 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
>      int ret;
>  
>      ret = bdrv_create_file(filename, opts, &local_err);
> -    if (local_err) {
> -        error_propagate(errp, local_err);
> -    }
> +    error_propagate(errp, local_err);
>      return ret;

and another

>  }
>  
> diff --git a/block/snapshot.c b/block/snapshot.c
> index da89d2b..bf5c2ca 100644
> --- a/block/snapshot.c
> +++ b/block/snapshot.c
> @@ -358,9 +358,7 @@ int bdrv_snapshot_load_tmp_by_id_or_name(BlockDriverState *bs,
>          ret = bdrv_snapshot_load_tmp(bs, NULL, id_or_name, &local_err);
>      }
>  
> -    if (local_err) {
> -        error_propagate(errp, local_err);
> -    }
> +    error_propagate(errp, local_err);
>  
>      return ret;

and another

>  }
> diff --git a/blockdev.c b/blockdev.c
> index 7fd515a..028dba3 100644
> --- a/blockdev.c
> +++ b/blockdev.c
> @@ -3633,9 +3633,7 @@ void qmp_drive_mirror(const char *device, const char *target,
>                             has_unmap, unmap,
>                             &local_err);
>      bdrv_unref(target_bs);
> -    if (local_err) {
> -        error_propagate(errp, local_err);
> -    }
> +    error_propagate(errp, local_err);
>  out:
>      aio_context_release(aio_context);
>  }

and another.

Hmm - it seems like in most of the cases where the ONLY thing done in
the if (local_err) block is to propagate the error, we should instead be
directly assigning to errp instead of wasting a local variable.  At this
point, my review is repetitive enough that I'll stop looking, and leave
it up to you and Markus whether to attempt a more ambitious Coccinelle
script.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

  reply	other threads:[~2016-06-09 19:37 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-09 17:40 [Qemu-devel] [PATCH] error: Remove NULL checks on error_propagate() calls Eduardo Habkost
2016-06-09 19:37 ` Eric Blake [this message]
2016-06-09 19:50   ` Eduardo Habkost
2016-06-09 20:11     ` Eric Blake
2016-06-09 20:21       ` Eduardo Habkost
2016-06-09 20:21   ` [Qemu-devel] [PATCH] error: Avoid redudant error_propagate() usage Eduardo Habkost
2016-06-09 20:47     ` Eduardo Habkost
2016-06-09 20:54       ` Eric Blake
2016-06-09 21:57         ` Eduardo Habkost
2016-06-09 20:57     ` Eric Blake
2016-06-09 21:59       ` Eduardo Habkost
  -- strict thread matches above, loose matches on Subject: below --
2018-12-13 17:31 [Qemu-devel] [PATCH] error: Remove NULL checks on error_propagate() calls Markus Armbruster
2018-12-13 21:23 ` Eric Blake
2018-12-14 10:44 ` Philippe Mathieu-Daudé

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=5759C573.1070403@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=borntraeger@de.ibm.com \
    --cc=cornelia.huck@de.ibm.com \
    --cc=ehabkost@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --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).