qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: Chunyan Liu <cyliu@suse.com>
Cc: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>,
	qemu-devel@nongnu.org, stefanha@redhat.com
Subject: Re: [Qemu-devel] [v19 03/25] improve some functions in qemu-option.c
Date: Wed, 22 Jan 2014 14:33:41 +0100	[thread overview]
Message-ID: <20140122133341.GH10065@dhcp-200-207.str.redhat.com> (raw)
In-Reply-To: <1390227608-7225-4-git-send-email-cyliu@suse.com>

Am 20.01.2014 um 15:19 hat Chunyan Liu geschrieben:
> Improve opt_get and opt_set group of functions. For opt_get, check and handle
> NUlL input; for opt_set, when set to an existing option, rewrite the option
> with new value.
> 
> Signed-off-by: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
> Signed-off-by: Chunyan Liu <cyliu@suse.com>

Why do we want to allow NULL opts? Silently ignoring NULL instead of
crashing leads to more subtle failure. Is there a legitimate user
passing NULL?

>  util/qemu-option.c |   80 ++++++++++++++++++++++++++++++++++++++++++---------
>  1 files changed, 66 insertions(+), 14 deletions(-)
> 
> diff --git a/util/qemu-option.c b/util/qemu-option.c
> index fd84f95..8944b62 100644
> --- a/util/qemu-option.c
> +++ b/util/qemu-option.c
> @@ -499,6 +499,9 @@ static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
>  {
>      QemuOpt *opt;
>  
> +    if (!opts)
> +        return NULL;

The qemu coding style requires braces here (and in the following
instances).

> +
>      QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
>          if (strcmp(opt->name, name) != 0)
>              continue;
> @@ -509,9 +512,13 @@ static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
>  
>  const char *qemu_opt_get(QemuOpts *opts, const char *name)
>  {
> -    QemuOpt *opt = qemu_opt_find(opts, name);
> +    QemuOpt *opt;
>      const QemuOptDesc *desc;
>  
> +    if (!opts)
> +        return NULL;
> +
> +    opt = qemu_opt_find(opts, name);
>      if (!opt) {
>          desc = find_desc_by_name(opts->list->desc, name);
>          if (desc && desc->def_value_str) {
> @@ -535,10 +542,15 @@ bool qemu_opt_has_help_opt(QemuOpts *opts)
>  
>  bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
>  {
> -    QemuOpt *opt = qemu_opt_find(opts, name);
> +    QemuOpt *opt;
>      const QemuOptDesc *desc;
>      Error *local_err = NULL;
>  
> +    if (!opts)
> +        return defval;
> +
> +    opt = qemu_opt_find(opts, name);
> +
>      if (opt == NULL) {
>          desc = find_desc_by_name(opts->list->desc, name);
>          if (desc && desc->def_value_str) {
> @@ -553,10 +565,15 @@ bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
>  
>  uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
>  {
> -    QemuOpt *opt = qemu_opt_find(opts, name);
> +    QemuOpt *opt;
>      const QemuOptDesc *desc;
>      Error *local_err = NULL;
>  
> +    if (!opts)
> +        return defval;
> +
> +    opt = qemu_opt_find(opts, name);
> +
>      if (opt == NULL) {
>          desc = find_desc_by_name(opts->list->desc, name);
>          if (desc && desc->def_value_str) {
> @@ -571,10 +588,14 @@ uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
>  
>  uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
>  {
> -    QemuOpt *opt = qemu_opt_find(opts, name);
> +    QemuOpt *opt;
>      const QemuOptDesc *desc;
>      Error *local_err = NULL;
>  
> +    if (!opts)
> +        return defval;
> +
> +    opt = qemu_opt_find(opts, name);
>      if (opt == NULL) {
>          desc = find_desc_by_name(opts->list->desc, name);
>          if (desc && desc->def_value_str) {
> @@ -612,6 +633,10 @@ static void qemu_opt_parse(QemuOpt *opt, Error **errp)
>  
>  static void qemu_opt_del(QemuOpt *opt)
>  {
> +    if (!opt) {
> +        return;
> +    }
> +
>      QTAILQ_REMOVE(&opt->opts->head, opt, next);
>      g_free((/* !const */ char*)opt->name);
>      g_free((/* !const */ char*)opt->str);
> @@ -664,6 +689,13 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,
>          return;
>      }
>  
> +    opt = qemu_opt_find(opts, name);
> +    if (opt) {
> +        g_free((char*)opt->str);
> +        opt->str = g_strdup(value);

Why is qemu_opt_parse() not needed here?

> +        return;
> +    }
> +
>      opt = g_malloc0(sizeof(*opt));
>      opt->name = g_strdup(name);
>      opt->opts = opts;
> @@ -704,16 +736,24 @@ void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
>  int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
>  {
>      QemuOpt *opt;
> -    const QemuOptDesc *desc = opts->list->desc;
> +    const QemuOptDesc *desc;
>  
> -    opt = g_malloc0(sizeof(*opt));
> -    opt->desc = find_desc_by_name(desc, name);
> -    if (!opt->desc && !opts_accepts_any(opts)) {
> +    desc = find_desc_by_name(opts->list->desc, name);
> +    if (!desc && !opts_accepts_any(opts)) {
>          qerror_report(QERR_INVALID_PARAMETER, name);
> -        g_free(opt);
>          return -1;
>      }
>  
> +    opt = qemu_opt_find(opts, name);
> +    if (opt) {
> +        g_free((char*)opt->str);
> +        opt->value.boolean =val;

Missing space after =

> +        opt->str = g_strdup(val ? "on" : "off");
> +        return 0;
> +    }
> +
> +    opt = g_malloc0(sizeof(*opt));
> +    opt->desc = desc;
>      opt->name = g_strdup(name);
>      opt->opts = opts;
>      opt->value.boolean = !!val;

Kevin

  reply	other threads:[~2014-01-22 14:24 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-20 14:19 [Qemu-devel] [v19 00/25] replace QEMUOptionParameter with QemuOpts Chunyan Liu
2014-01-20 14:19 ` [Qemu-devel] [v19 01/25] add def_value_str to QemuOptDesc Chunyan Liu
2014-01-22 13:25   ` Kevin Wolf
2014-01-20 14:19 ` [Qemu-devel] [v19 02/25] qapi: output def_value_str when query command line options Chunyan Liu
2014-01-20 15:53   ` Eric Blake
2014-01-20 14:19 ` [Qemu-devel] [v19 03/25] improve some functions in qemu-option.c Chunyan Liu
2014-01-22 13:33   ` Kevin Wolf [this message]
2014-01-20 14:19 ` [Qemu-devel] [v19 04/25] add some QemuOpts functions for replace work Chunyan Liu
2014-01-22 13:57   ` Kevin Wolf
2014-01-20 14:19 ` [Qemu-devel] [v19 05/25] change block layer to support both QemuOpts and QEMUOptionParameter Chunyan Liu
2014-01-22 14:19   ` Kevin Wolf
2014-01-20 14:19 ` [Qemu-devel] [v19 06/25] cow.c: replace QEMUOptionParameter with QemuOpts Chunyan Liu
2014-01-22 14:20   ` Kevin Wolf
2014-01-20 14:19 ` [Qemu-devel] [v19 07/25] gluster.c: " Chunyan Liu
2014-01-22 13:10   ` Kevin Wolf
2014-01-20 14:19 ` [Qemu-devel] [v19 08/25] iscsi.c: replace QEMUOptionParamter " Chunyan Liu
2014-01-20 14:19 ` [Qemu-devel] [v19 09/25] qcow.c: " Chunyan Liu
2014-01-20 14:19 ` [Qemu-devel] [v19 10/25] qcow2.c: replace QEMUOptionParameter with QemuOpts in create Chunyan Liu
2014-01-20 14:19 ` [Qemu-devel] [v19 11/25] qcow2.c: replace QEMUOptionParameter with QemuOpts in amend options Chunyan Liu
2014-01-20 14:19 ` [Qemu-devel] [v19 12/25] qed.c: replace QEMUOptionParameter with QemuOpts Chunyan Liu
2014-01-22 14:24   ` Kevin Wolf
2014-01-20 14:19 ` [Qemu-devel] [v19 13/25] raw-posix.c: " Chunyan Liu
2014-01-20 14:19 ` [Qemu-devel] [v19 14/25] raw-win32.c: " Chunyan Liu
2014-01-20 14:19 ` [Qemu-devel] [v19 15/25] rbd.c: " Chunyan Liu
2014-01-20 14:19 ` [Qemu-devel] [v19 16/25] sheepdog.c: " Chunyan Liu
2014-01-20 14:20 ` [Qemu-devel] [v19 17/25] ssh.c: " Chunyan Liu
2014-01-20 14:20 ` [Qemu-devel] [v19 18/25] vdi.c: " Chunyan Liu
2014-01-20 14:20 ` [Qemu-devel] [v19 19/25] vmdk.c: " Chunyan Liu
2014-01-20 14:20 ` [Qemu-devel] [v19 20/25] vpc.c: " Chunyan Liu
2014-01-20 14:20 ` [Qemu-devel] [v19 21/25] raw_bsd.c: " Chunyan Liu
2014-01-20 14:20 ` [Qemu-devel] [v19 22/25] vhdx.c: " Chunyan Liu
2014-01-22 14:26   ` Kevin Wolf
2014-01-20 14:20 ` [Qemu-devel] [v19 23/25] vvfat.c: " Chunyan Liu
2014-01-20 14:20 ` [Qemu-devel] [v19 24/25] cleanup QEMUOptionParameter Chunyan Liu
2014-01-22 14:33   ` Kevin Wolf
2014-01-20 14:20 ` [Qemu-devel] [v19 25/25] change back to original name from bdrv_create2 to bdrv_create Chunyan Liu
2014-01-22 14:35 ` [Qemu-devel] [v19 00/25] replace QEMUOptionParameter with QemuOpts Kevin Wolf
2014-01-22 16:11   ` Stefan Hajnoczi

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=20140122133341.GH10065@dhcp-200-207.str.redhat.com \
    --to=kwolf@redhat.com \
    --cc=cyliu@suse.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=wdongxu@linux.vnet.ibm.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).