Skip to content

Commit

Permalink
error: Avoid unnecessary error_propagate() after error_setg()
Browse files Browse the repository at this point in the history
Replace

    error_setg(&err, ...);
    error_propagate(errp, err);

by

    error_setg(errp, ...);

Related pattern:

    if (...) {
        error_setg(&err, ...);
        goto out;
    }
    ...
 out:
    error_propagate(errp, err);
    return;

When all paths to label out are that way, replace by

    if (...) {
        error_setg(errp, ...);
        return;
    }

and delete the label along with the error_propagate().

When we have at most one other path that actually needs to propagate,
and maybe one at the end that where propagation is unnecessary, e.g.

    foo(..., &err);
    if (err) {
        goto out;
    }
    ...
    bar(..., &err);
 out:
    error_propagate(errp, err);
    return;

move the error_propagate() to where it's needed, like

    if (...) {
        foo(..., &err);
        error_propagate(errp, err);
        return;
    }
    ...
    bar(..., errp);
    return;

and transform the error_setg() as above.

In some places, the transformation results in obviously unnecessary
error_propagate().  The next few commits will eliminate them.

Bonus: the elimination of gotos will make later patches in this series
easier to review.

Candidates for conversion tracked down with this Coccinelle script:

    @@
    identifier err, errp;
    expression list args;
    @@
    -    error_setg(&err, args);
    +    error_setg(errp, args);
         ... when != err
         error_propagate(errp, err);

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200707160613.848843-34-armbru@redhat.com>
  • Loading branch information
Markus Armbruster committed Jul 10, 2020
1 parent 0c0e618 commit dcfe480
Show file tree
Hide file tree
Showing 24 changed files with 169 additions and 242 deletions.
11 changes: 5 additions & 6 deletions backends/cryptodev.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,15 @@ cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
uint32_t value;

if (!visit_type_uint32(v, name, &value, &local_err)) {
goto out;
error_propagate(errp, local_err);
return;
}
if (!value) {
error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
PRIu32 "'", object_get_typename(obj), name, value);
goto out;
error_setg(errp, "Property '%s.%s' doesn't take value '%" PRIu32 "'",
object_get_typename(obj), name, value);
return;
}
backend->conf.peers.queues = value;
out:
error_propagate(errp, local_err);
}

static void
Expand Down
19 changes: 6 additions & 13 deletions backends/hostmem-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,16 @@ static void file_memory_backend_set_align(Object *o, Visitor *v,
uint64_t val;

if (host_memory_backend_mr_inited(backend)) {
error_setg(&local_err, "cannot change property '%s' of %s",
name, object_get_typename(o));
goto out;
error_setg(errp, "cannot change property '%s' of %s", name,
object_get_typename(o));
return;
}

if (!visit_type_size(v, name, &val, &local_err)) {
goto out;
error_propagate(errp, local_err);
return;
}
fb->align = val;

out:
error_propagate(errp, local_err);
}

static bool file_memory_backend_get_pmem(Object *o, Error **errp)
Expand All @@ -139,21 +137,16 @@ static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);

if (host_memory_backend_mr_inited(backend)) {

error_setg(errp, "cannot change property 'pmem' of %s.",
object_get_typename(o));
return;
}

#ifndef CONFIG_LIBPMEM
if (value) {
Error *local_err = NULL;

error_setg(&local_err,
"Lack of libpmem support while setting the 'pmem=on'"
error_setg(errp, "Lack of libpmem support while setting the 'pmem=on'"
" of %s. We can't ensure data persistence.",
object_get_typename(o));
error_propagate(errp, local_err);
return;
}
#endif
Expand Down
15 changes: 7 additions & 8 deletions backends/hostmem-memfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,20 @@ memfd_backend_set_hugetlbsize(Object *obj, Visitor *v, const char *name,
uint64_t value;

if (host_memory_backend_mr_inited(MEMORY_BACKEND(obj))) {
error_setg(&local_err, "cannot change property value");
goto out;
error_setg(errp, "cannot change property value");
return;
}

if (!visit_type_size(v, name, &value, &local_err)) {
goto out;
error_propagate(errp, local_err);
return;
}
if (!value) {
error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
PRIu64 "'", object_get_typename(obj), name, value);
goto out;
error_setg(errp, "Property '%s.%s' doesn't take value '%" PRIu64 "'",
object_get_typename(obj), name, value);
return;
}
m->hugetlbsize = value;
out:
error_propagate(errp, local_err);
}

static void
Expand Down
27 changes: 12 additions & 15 deletions backends/hostmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,22 @@ host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
uint64_t value;

if (host_memory_backend_mr_inited(backend)) {
error_setg(&local_err, "cannot change property %s of %s ",
name, object_get_typename(obj));
goto out;
error_setg(errp, "cannot change property %s of %s ", name,
object_get_typename(obj));
return;
}

if (!visit_type_size(v, name, &value, &local_err)) {
goto out;
error_propagate(errp, local_err);
return;
}
if (!value) {
error_setg(&local_err,
error_setg(errp,
"property '%s' of %s doesn't take value '%" PRIu64 "'",
name, object_get_typename(obj), value);
goto out;
return;
}
backend->size = value;
out:
error_propagate(errp, local_err);
}

static void
Expand Down Expand Up @@ -257,17 +256,15 @@ static void host_memory_backend_set_prealloc_threads(Object *obj, Visitor *v,
uint32_t value;

if (!visit_type_uint32(v, name, &value, &local_err)) {
goto out;
error_propagate(errp, local_err);
return;
}
if (value <= 0) {
error_setg(&local_err,
"property '%s' of %s doesn't take value '%d'",
name, object_get_typename(obj), value);
goto out;
error_setg(errp, "property '%s' of %s doesn't take value '%d'", name,
object_get_typename(obj), value);
return;
}
backend->prealloc_threads = value;
out:
error_propagate(errp, local_err);
}

static void host_memory_backend_init(Object *obj)
Expand Down
16 changes: 7 additions & 9 deletions block/quorum.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,13 +910,12 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
/* count how many different children are present */
s->num_children = qdict_array_entries(options, "children.");
if (s->num_children < 0) {
error_setg(&local_err, "Option children is not a valid array");
error_setg(errp, "Option children is not a valid array");
ret = -EINVAL;
goto exit;
}
if (s->num_children < 1) {
error_setg(&local_err,
"Number of provided children must be 1 or more");
error_setg(errp, "Number of provided children must be 1 or more");
ret = -EINVAL;
goto exit;
}
Expand All @@ -929,7 +928,7 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,

s->threshold = qemu_opt_get_number(opts, QUORUM_OPT_VOTE_THRESHOLD, 0);
/* and validate it against s->num_children */
ret = quorum_valid_threshold(s->threshold, s->num_children, &local_err);
ret = quorum_valid_threshold(s->threshold, s->num_children, errp);
if (ret < 0) {
goto exit;
}
Expand All @@ -942,15 +941,15 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
-EINVAL, NULL);
}
if (ret < 0) {
error_setg(&local_err, "Please set read-pattern as fifo or quorum");
error_setg(errp, "Please set read-pattern as fifo or quorum");
goto exit;
}
s->read_pattern = ret;

if (s->read_pattern == QUORUM_READ_PATTERN_QUORUM) {
s->is_blkverify = qemu_opt_get_bool(opts, QUORUM_OPT_BLKVERIFY, false);
if (s->is_blkverify && (s->num_children != 2 || s->threshold != 2)) {
error_setg(&local_err, "blkverify=on can only be set if there are "
error_setg(errp, "blkverify=on can only be set if there are "
"exactly two files and vote-threshold is 2");
ret = -EINVAL;
goto exit;
Expand All @@ -959,7 +958,7 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
s->rewrite_corrupted = qemu_opt_get_bool(opts, QUORUM_OPT_REWRITE,
false);
if (s->rewrite_corrupted && s->is_blkverify) {
error_setg(&local_err,
error_setg(errp,
"rewrite-corrupted=on cannot be used with blkverify=on");
ret = -EINVAL;
goto exit;
Expand All @@ -979,6 +978,7 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
&child_of_bds, BDRV_CHILD_DATA, false,
&local_err);
if (local_err) {
error_propagate(errp, local_err);
ret = -EINVAL;
goto close_exit;
}
Expand All @@ -1004,8 +1004,6 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
g_free(opened);
exit:
qemu_opts_del(opts);
/* propagate error */
error_propagate(errp, local_err);
return ret;
}

Expand Down
11 changes: 5 additions & 6 deletions block/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,28 @@ static int replication_open(BlockDriverState *bs, QDict *options,

mode = qemu_opt_get(opts, REPLICATION_MODE);
if (!mode) {
error_setg(&local_err, "Missing the option mode");
error_setg(errp, "Missing the option mode");
goto fail;
}

if (!strcmp(mode, "primary")) {
s->mode = REPLICATION_MODE_PRIMARY;
top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
if (top_id) {
error_setg(&local_err, "The primary side does not support option top-id");
error_setg(errp,
"The primary side does not support option top-id");
goto fail;
}
} else if (!strcmp(mode, "secondary")) {
s->mode = REPLICATION_MODE_SECONDARY;
top_id = qemu_opt_get(opts, REPLICATION_TOP_ID);
s->top_id = g_strdup(top_id);
if (!s->top_id) {
error_setg(&local_err, "Missing the option top-id");
error_setg(errp, "Missing the option top-id");
goto fail;
}
} else {
error_setg(&local_err,
error_setg(errp,
"The option mode's value should be primary or secondary");
goto fail;
}
Expand All @@ -136,8 +137,6 @@ static int replication_open(BlockDriverState *bs, QDict *options,

fail:
qemu_opts_del(opts);
error_propagate(errp, local_err);

return ret;
}

Expand Down
22 changes: 9 additions & 13 deletions block/throttle-groups.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,16 +819,17 @@ static void throttle_group_set(Object *obj, Visitor *v, const char * name,
* transaction, as certain combinations are invalid.
*/
if (tg->is_initialized) {
error_setg(&local_err, "Property cannot be set after initialization");
goto ret;
error_setg(errp, "Property cannot be set after initialization");
return;
}

if (!visit_type_int64(v, name, &value, &local_err)) {
goto ret;
error_propagate(errp, local_err);
return;
}
if (value < 0) {
error_setg(&local_err, "Property values cannot be negative");
goto ret;
error_setg(errp, "Property values cannot be negative");
return;
}

cfg = &tg->ts.cfg;
Expand All @@ -841,21 +842,16 @@ static void throttle_group_set(Object *obj, Visitor *v, const char * name,
break;
case BURST_LENGTH:
if (value > UINT_MAX) {
error_setg(&local_err, "%s value must be in the"
"range [0, %u]", info->name, UINT_MAX);
goto ret;
error_setg(errp, "%s value must be in the" "range [0, %u]",
info->name, UINT_MAX);
return;
}
cfg->buckets[info->type].burst_length = value;
break;
case IOPS_SIZE:
cfg->op_size = value;
break;
}

ret:
error_propagate(errp, local_err);
return;

}

static void throttle_group_get(Object *obj, Visitor *v, const char *name,
Expand Down
9 changes: 4 additions & 5 deletions block/vxhs.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ static int vxhs_open(BlockDriverState *bs, QDict *options,
/* vdisk-id is the disk UUID */
vdisk_id_opt = qemu_opt_get(opts, VXHS_OPT_VDISK_ID);
if (!vdisk_id_opt) {
error_setg(&local_err, QERR_MISSING_PARAMETER, VXHS_OPT_VDISK_ID);
error_setg(errp, QERR_MISSING_PARAMETER, VXHS_OPT_VDISK_ID);
ret = -EINVAL;
goto out;
}

/* vdisk-id may contain a leading '/' */
if (strlen(vdisk_id_opt) > UUID_FMT_LEN + 1) {
error_setg(&local_err, "vdisk-id cannot be more than %d characters",
error_setg(errp, "vdisk-id cannot be more than %d characters",
UUID_FMT_LEN);
ret = -EINVAL;
goto out;
Expand All @@ -352,14 +352,14 @@ static int vxhs_open(BlockDriverState *bs, QDict *options,

server_host_opt = qemu_opt_get(tcp_opts, VXHS_OPT_HOST);
if (!server_host_opt) {
error_setg(&local_err, QERR_MISSING_PARAMETER,
error_setg(errp, QERR_MISSING_PARAMETER,
VXHS_OPT_SERVER"."VXHS_OPT_HOST);
ret = -EINVAL;
goto out;
}

if (strlen(server_host_opt) > MAXHOSTNAMELEN) {
error_setg(&local_err, "server.host cannot be more than %d characters",
error_setg(errp, "server.host cannot be more than %d characters",
MAXHOSTNAMELEN);
ret = -EINVAL;
goto out;
Expand Down Expand Up @@ -412,7 +412,6 @@ static int vxhs_open(BlockDriverState *bs, QDict *options,

if (ret < 0) {
vxhs_unref();
error_propagate(errp, local_err);
g_free(s->vdisk_hostinfo.host);
g_free(s->vdisk_guid);
g_free(s->tlscredsid);
Expand Down
Loading

0 comments on commit dcfe480

Please sign in to comment.