From 924e9b0da90ca1fa0de7c5076768a8a9f3795f8d Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Sun, 8 Nov 2020 10:21:21 -0500 Subject: qemu-option: simplify search for end of key Use strcspn to find an equal or comma value, and pass the result directly to get_opt_name to avoid another strchr. Reviewed-by: Markus Armbruster Signed-off-by: Paolo Bonzini --- util/qemu-option.c | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) (limited to 'util/qemu-option.c') diff --git a/util/qemu-option.c b/util/qemu-option.c index 25792159ba..6bd654a473 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -38,27 +38,19 @@ #include "qemu/help_option.h" /* - * Extracts the name of an option from the parameter string (p points at the + * Extracts the name of an option from the parameter string (@p points at the * first byte of the option name) * - * The option name is delimited by delim (usually , or =) or the string end - * and is copied into option. The caller is responsible for free'ing option - * when no longer required. + * The option name is @len characters long and is copied into @option. The + * caller is responsible for free'ing @option when no longer required. * * The return value is the position of the delimiter/zero byte after the option - * name in p. + * name in @p. */ -static const char *get_opt_name(const char *p, char **option, char delim) +static const char *get_opt_name(const char *p, char **option, size_t len) { - char *offset = strchr(p, delim); - - if (offset) { - *option = g_strndup(p, offset - p); - return offset; - } else { - *option = g_strdup(p); - return p + strlen(p); - } + *option = g_strndup(p, len); + return p + len; } /* @@ -766,12 +758,11 @@ static const char *get_opt_name_value(const char *params, const char *firstname, char **name, char **value) { - const char *p, *pe, *pc; - - pe = strchr(params, '='); - pc = strchr(params, ','); + const char *p; + size_t len; - if (!pe || (pc && pc < pe)) { + len = strcspn(params, "=,"); + if (params[len] != '=') { /* found "foo,more" */ if (firstname) { /* implicitly named first option */ @@ -779,7 +770,7 @@ static const char *get_opt_name_value(const char *params, p = get_opt_value(params, value); } else { /* option without value, must be a flag */ - p = get_opt_name(params, name, ','); + p = get_opt_name(params, name, len); if (strncmp(*name, "no", 2) == 0) { memmove(*name, *name + 2, strlen(*name + 2) + 1); *value = g_strdup("off"); @@ -789,7 +780,7 @@ static const char *get_opt_name_value(const char *params, } } else { /* found "foo=bar,more" */ - p = get_opt_name(params, name, '='); + p = get_opt_name(params, name, len); assert(*p == '='); p++; p = get_opt_value(p, value); -- cgit 1.4.1 From 45c53fe64c28239b7504d00d76fe37ef31c0eefa Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 9 Nov 2020 03:50:46 -0500 Subject: qemu-option: pass QemuOptsList to opts_accepts_any A QemuOptsList can be of one of two kinds: either it is pre-validated, or it accepts any key and validation happens somewhere else (typically in a Visitor or against a list of QOM properties). opts_accepts_any returns true if a QemuOpts instance was created from a QemuOptsList of the latter kind, but there is no function to do the check on a QemuOptsList. Since this property comes from the QemuOptsList and almost all callers of opts_accepts_any use opts->list anyway, modify the function to accept QemuOptsList. Reviewed-by: Markus Armbruster Signed-off-by: Paolo Bonzini --- util/qemu-option.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'util/qemu-option.c') diff --git a/util/qemu-option.c b/util/qemu-option.c index 6bd654a473..c88e159f18 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -460,16 +460,16 @@ static bool qemu_opt_parse(QemuOpt *opt, Error **errp) } } -static bool opts_accepts_any(const QemuOpts *opts) +static bool opts_accepts_any(const QemuOptsList *list) { - return opts->list->desc[0].name == NULL; + return list->desc[0].name == NULL; } int qemu_opt_unset(QemuOpts *opts, const char *name) { QemuOpt *opt = qemu_opt_find(opts, name); - assert(opts_accepts_any(opts)); + assert(opts_accepts_any(opts->list)); if (opt == NULL) { return -1; @@ -500,9 +500,10 @@ static bool opt_validate(QemuOpt *opt, bool *help_wanted, Error **errp) { const QemuOptDesc *desc; + const QemuOptsList *list = opt->opts->list; - desc = find_desc_by_name(opt->opts->list->desc, opt->name); - if (!desc && !opts_accepts_any(opt->opts)) { + desc = find_desc_by_name(list->desc, opt->name); + if (!desc && !opts_accepts_any(list)) { error_setg(errp, QERR_INVALID_PARAMETER, opt->name); if (help_wanted && is_help_option(opt->name)) { *help_wanted = true; @@ -535,9 +536,10 @@ bool qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val, { QemuOpt *opt; const QemuOptDesc *desc; + const QemuOptsList *list = opts->list; - desc = find_desc_by_name(opts->list->desc, name); - if (!desc && !opts_accepts_any(opts)) { + desc = find_desc_by_name(list->desc, name); + if (!desc && !opts_accepts_any(list)) { error_setg(errp, QERR_INVALID_PARAMETER, name); return false; } @@ -557,9 +559,10 @@ bool qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val, { QemuOpt *opt; const QemuOptDesc *desc; + const QemuOptsList *list = opts->list; - desc = find_desc_by_name(opts->list->desc, name); - if (!desc && !opts_accepts_any(opts)) { + desc = find_desc_by_name(list->desc, name); + if (!desc && !opts_accepts_any(list)) { error_setg(errp, QERR_INVALID_PARAMETER, name); return false; } @@ -1107,7 +1110,7 @@ bool qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp) { QemuOpt *opt; - assert(opts_accepts_any(opts)); + assert(opts_accepts_any(opts->list)); QTAILQ_FOREACH(opt, &opts->head, next) { opt->desc = find_desc_by_name(desc, opt->name); -- cgit 1.4.1