[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 12/53] qemu-option: Replace opt_set() by cleaner opt_validate()
From: |
Markus Armbruster |
Subject: |
[PULL 12/53] qemu-option: Replace opt_set() by cleaner opt_validate() |
Date: |
Tue, 7 Jul 2020 23:24:22 +0200 |
opt_set() frees its argument @value on failure. Slightly unclean;
functions ideally do nothing on failure.
To tidy this up, move opt_create() from opt_set() into its callers,
along with the cleanup. Rename opt_set() to opt_validate(), noting
its similarity to qemu_opts_validate(). Drop redundant parameter
@opts; use opt->opts instead.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-13-armbru@redhat.com>
---
util/qemu-option.c | 35 +++++++++++++++++++----------------
1 file changed, 19 insertions(+), 16 deletions(-)
diff --git a/util/qemu-option.c b/util/qemu-option.c
index 1023fe7527..d8233b3b35 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -516,36 +516,39 @@ static QemuOpt *opt_create(QemuOpts *opts, const char
*name, char *value,
return opt;
}
-static void opt_set(QemuOpts *opts, const char *name, char *value,
- bool prepend, bool *help_wanted, Error **errp)
+static bool opt_validate(QemuOpt *opt, bool *help_wanted,
+ Error **errp)
{
- QemuOpt *opt;
const QemuOptDesc *desc;
Error *local_err = NULL;
- desc = find_desc_by_name(opts->list->desc, name);
- if (!desc && !opts_accepts_any(opts)) {
- g_free(value);
- error_setg(errp, QERR_INVALID_PARAMETER, name);
- if (help_wanted && is_help_option(name)) {
+ desc = find_desc_by_name(opt->opts->list->desc, opt->name);
+ if (!desc && !opts_accepts_any(opt->opts)) {
+ error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
+ if (help_wanted && is_help_option(opt->name)) {
*help_wanted = true;
}
- return;
+ return false;
}
- opt = opt_create(opts, name, value, prepend);
opt->desc = desc;
qemu_opt_parse(opt, &local_err);
if (local_err) {
error_propagate(errp, local_err);
- qemu_opt_del(opt);
+ return false;
}
+
+ return true;
}
void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
Error **errp)
{
- opt_set(opts, name, g_strdup(value), false, NULL, errp);
+ QemuOpt *opt = opt_create(opts, name, g_strdup(value), false);
+
+ if (!opt_validate(opt, NULL, errp)) {
+ qemu_opt_del(opt);
+ }
}
void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
@@ -817,9 +820,9 @@ static void opts_do_parse(QemuOpts *opts, const char
*params,
const char *firstname, bool prepend,
bool *help_wanted, Error **errp)
{
- Error *local_err = NULL;
char *option, *value;
const char *p;
+ QemuOpt *opt;
for (p = params; *p;) {
p = get_opt_name_value(p, firstname, &option, &value);
@@ -831,10 +834,10 @@ static void opts_do_parse(QemuOpts *opts, const char
*params,
continue;
}
- opt_set(opts, option, value, prepend, help_wanted, &local_err);
+ opt = opt_create(opts, option, value, prepend);
g_free(option);
- if (local_err) {
- error_propagate(errp, local_err);
+ if (!opt_validate(opt, help_wanted, errp)) {
+ qemu_opt_del(opt);
return;
}
}
--
2.26.2
- [PULL 36/53] error: Eliminate error_propagate() manually, (continued)
- [PULL 36/53] error: Eliminate error_propagate() manually, Markus Armbruster, 2020/07/08
- [PULL 33/53] error: Avoid unnecessary error_propagate() after error_setg(), Markus Armbruster, 2020/07/08
- [PULL 43/53] qemu-img: Ignore Error objects where the return value suffices, Markus Armbruster, 2020/07/08
- [PULL 31/53] qdev: Make functions taking Error ** return bool, not void, Markus Armbruster, 2020/07/08
- [PULL 23/53] qom: Crash more nicely on object_property_get_link() failure, Markus Armbruster, 2020/07/08
- [PULL 53/53] xen: Use ERRP_GUARD(), Markus Armbruster, 2020/07/08
- [PULL 26/53] qom: Put name parameter before value / visitor parameter, Markus Armbruster, 2020/07/08
- [PULL 40/53] qapi: Smooth visitor error checking in generated code, Markus Armbruster, 2020/07/08
- [PULL 39/53] qapi: Smooth another visitor error checking pattern, Markus Armbruster, 2020/07/08
- [PULL 18/53] qapi: Use returned bool to check for failure, Coccinelle part, Markus Armbruster, 2020/07/08
- [PULL 12/53] qemu-option: Replace opt_set() by cleaner opt_validate(),
Markus Armbruster <=
- [PULL 01/53] error: Fix examples in error.h's big comment, Markus Armbruster, 2020/07/08
- [PULL 10/53] qemu-option: Simplify around find_default_by_name(), Markus Armbruster, 2020/07/08
- [PULL 17/53] qapi: Make visitor functions taking Error ** return bool, not void, Markus Armbruster, 2020/07/08
- [PULL 37/53] error: Reduce unnecessary error propagation, Markus Armbruster, 2020/07/08
- [PULL 15/53] block: Avoid error accumulation in bdrv_img_create(), Markus Armbruster, 2020/07/08
- [PULL 48/53] sd: Use ERRP_GUARD(), Markus Armbruster, 2020/07/08
- [PULL 14/53] qemu-option: Use returned bool to check for failure, Markus Armbruster, 2020/07/08
- [PULL 21/53] qom: Use error_reportf_err() instead of g_printerr() in examples, Markus Armbruster, 2020/07/08
- [PULL 20/53] s390x/pci: Fix harmless mistake in zpci's property fid's setter, Markus Armbruster, 2020/07/08
- [PULL 29/53] qom: Use returned bool to check for failure, manual part, Markus Armbruster, 2020/07/08