All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Richard Henderson <richard.henderson@linaro.org>
Cc: qemu-devel@nongnu.org, fei2.wu@intel.com,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: Re: [PATCH v17 09/16] util/log: Add Error argument to qemu_str_to_log_mask
Date: Tue, 10 Oct 2023 14:55:27 +0200	[thread overview]
Message-ID: <87y1gamybk.fsf@pond.sub.org> (raw)
In-Reply-To: <20231003183058.1639121-10-richard.henderson@linaro.org> (Richard Henderson's message of "Tue, 3 Oct 2023 11:30:51 -0700")

Richard Henderson <richard.henderson@linaro.org> writes:

> Do not rely on return value of 0 to indicate error,
> pass along an Error pointer to be set.

Not wrong, but goes against error.h's recommendation

 * - Whenever practical, also return a value that indicates success /
 *   failure.  This can make the error checking more concise, and can
 *   avoid useless error object creation and destruction.  Note that
 *   we still have many functions returning void.  We recommend
 *   • bool-valued functions return true on success / false on failure,
 *   • pointer-valued functions return non-null / null pointer, and
 *   • integer-valued functions return non-negative / negative.

>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  include/qemu/log.h | 2 +-
>  bsd-user/main.c    | 6 ++++--
>  linux-user/main.c  | 7 +++++--
>  monitor/hmp-cmds.c | 5 +++--
>  softmmu/vl.c       | 7 +++++--
>  util/log.c         | 5 +++--
>  6 files changed, 21 insertions(+), 11 deletions(-)
>
> diff --git a/include/qemu/log.h b/include/qemu/log.h
> index df59bfabcd..9b92d2663e 100644
> --- a/include/qemu/log.h
> +++ b/include/qemu/log.h
> @@ -87,7 +87,7 @@ bool qemu_set_log_filename(const char *filename, Error **errp);
>  bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp);
>  void qemu_set_dfilter_ranges(const char *ranges, Error **errp);
>  bool qemu_log_in_addr_range(uint64_t addr);
> -int qemu_str_to_log_mask(const char *str);
> +int qemu_str_to_log_mask(const char *str, Error **errp);
>  
>  /* Print a usage message listing all the valid logging categories
>   * to the specified FILE*.
> diff --git a/bsd-user/main.c b/bsd-user/main.c
> index 703f3e2c41..a981239a0b 100644
> --- a/bsd-user/main.c
> +++ b/bsd-user/main.c
> @@ -411,8 +411,10 @@ int main(int argc, char **argv)
>      {
>          int mask = 0;
>          if (log_mask) {
> -            mask = qemu_str_to_log_mask(log_mask);
> -            if (!mask) {
> +            Error *err = NULL;
> +            mask = qemu_str_to_log_mask(log_mask, &err);
> +            if (err) {
> +                error_report_err(err);
>                  qemu_print_log_usage(stdout);
>                  exit(1);
>              }
> diff --git a/linux-user/main.c b/linux-user/main.c
> index 0c23584a96..d0464736cc 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -264,8 +264,11 @@ static void handle_arg_help(const char *arg)
>  
>  static void handle_arg_log(const char *arg)
>  {
> -    last_log_mask = qemu_str_to_log_mask(arg);
> -    if (!last_log_mask) {
> +    Error *err = NULL;
> +
> +    last_log_mask = qemu_str_to_log_mask(arg, &err);
> +    if (err) {
> +        error_report_err(err);
>          qemu_print_log_usage(stdout);
>          exit(EXIT_FAILURE);
>      }
> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
> index 6c559b48c8..c4bd97d467 100644
> --- a/monitor/hmp-cmds.c
> +++ b/monitor/hmp-cmds.c
> @@ -291,8 +291,9 @@ void hmp_log(Monitor *mon, const QDict *qdict)
>      if (!strcmp(items, "none")) {
>          mask = 0;
>      } else {
> -        mask = qemu_str_to_log_mask(items);
> -        if (!mask) {
> +        mask = qemu_str_to_log_mask(items, &err);
> +        if (err) {
> +            error_free(err);
>              hmp_help_cmd(mon, "log");
>              return;
>          }
> diff --git a/softmmu/vl.c b/softmmu/vl.c
> index 98e071e63b..02193696b9 100644
> --- a/softmmu/vl.c
> +++ b/softmmu/vl.c
> @@ -2486,8 +2486,11 @@ static void qemu_process_early_options(void)
>      {
>          int mask = 0;
>          if (log_mask) {
> -            mask = qemu_str_to_log_mask(log_mask);
> -            if (!mask) {
> +            Error *err = NULL;
> +
> +            mask = qemu_str_to_log_mask(log_mask, &err);
> +            if (err) {
> +                error_report_err(err);
>                  qemu_print_log_usage(stdout);
>                  exit(1);
>              }
> diff --git a/util/log.c b/util/log.c
> index def88a9402..b5f08db202 100644
> --- a/util/log.c
> +++ b/util/log.c
> @@ -500,8 +500,8 @@ const QEMULogItem qemu_log_items[] = {
>      { 0, NULL, NULL },
>  };
>  
> -/* takes a comma separated list of log masks. Return 0 if error. */
> -int qemu_str_to_log_mask(const char *str)
> +/* Takes a comma separated list of log masks. */
> +int qemu_str_to_log_mask(const char *str, Error **errp)
>  {
>      const QEMULogItem *item;
>      int mask = 0;
> @@ -524,6 +524,7 @@ int qemu_str_to_log_mask(const char *str)
       char **parts = g_strsplit(str, ",", 0);
       char **tmp;

When @parts is null or "", the loop runs zero times, and ...

       for (tmp = parts; tmp && *tmp; tmp++) {
           if (g_str_equal(*tmp, "all")) {
               for (item = qemu_log_items; item->mask != 0; item++) {
                   mask |= item->mask;
               }
   #ifdef CONFIG_TRACE_LOG
           } else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') {
               trace_enable_events((*tmp) + 6);
               mask |= LOG_TRACE;
   #endif
           } else {
               for (item = qemu_log_items; item->mask != 0; item++) {
                   if (g_str_equal(*tmp, item->name)) {
>                      goto found;
>                  }
>              }
> +            error_setg(errp, "Invalid -d option \"%s\"", *tmp);
>              goto error;
>          found:
>              mask |= item->mask;
           }
       }

... we end up here with mask zero and no error set.

       g_strfreev(parts);
       return mask;

Before the patch, this counted as error.  Afterwards, it doesn't.
Impact?  Worth mentioning in the commit message?

    error:
       g_strfreev(parts);
       return 0;

Returning -1 here would stick to error.h's recommendation.

   }



  reply	other threads:[~2023-10-10 12:55 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-03 18:30 [PATCH v17 00/16] TCG code quality tracking Richard Henderson
2023-10-03 18:30 ` [PATCH v17 01/16] accel/tcg: Move HMP info jit and info opcount code Richard Henderson
2023-10-10 12:09   ` Philippe Mathieu-Daudé
2023-10-15 12:58   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 02/16] tcg: Record orig_nb_ops TCGContext Richard Henderson
2023-10-15 12:57   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 03/16] tcg: Record nb_deleted_ops in TCGContext Richard Henderson
2023-10-15 12:58   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 04/16] tcg: Record nb_spills " Richard Henderson
2023-10-15 12:59   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 05/16] accel/tcg: Add TBStatistics structure Richard Henderson
2023-10-16 14:38   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 06/16] accel/tcg: Collect TB execution statistics Richard Henderson
2023-10-03 18:30 ` [PATCH v17 07/16] accel/tcg: Collect TB jit statistics Richard Henderson
2023-10-10 12:13   ` Philippe Mathieu-Daudé
2023-10-03 18:30 ` [PATCH v17 08/16] accel/tcg: Add tb_stats hmp command Richard Henderson
2023-10-03 18:30 ` [PATCH v17 09/16] util/log: Add Error argument to qemu_str_to_log_mask Richard Henderson
2023-10-10 12:55   ` Markus Armbruster [this message]
2023-10-15 18:55     ` Richard Henderson
2023-10-03 18:30 ` [PATCH v17 10/16] util/log: Add -d tb_stats Richard Henderson
2023-10-10 12:34   ` Philippe Mathieu-Daudé
2023-10-15 19:53     ` Richard Henderson
2023-10-03 18:30 ` [PATCH v17 11/16] accel/tcg: Add tb_stats_collect and tb_stats_dump Richard Henderson
2023-10-16 14:48   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 12/16] softmmu: Export qemu_ram_ptr_length Richard Henderson
2023-10-10 12:31   ` Philippe Mathieu-Daudé
2023-10-03 18:30 ` [PATCH v17 13/16] disas: Allow monitor_disas to read from ram_addr_t Richard Henderson
2023-10-10 12:46   ` Philippe Mathieu-Daudé
2023-10-15 19:21     ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 14/16] monitor: Change MonitorDec.get_value return type to int64_t Richard Henderson
2023-10-16 14:59   ` Alex Bennée
2023-10-16 15:43   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 15/16] accel/tcg: Add info [tb-list|tb] commands to HMP Richard Henderson
2023-10-16 15:02   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 16/16] accel/tcg: Dump hot TBs at the end of the execution Richard Henderson
2023-10-10 12:36   ` Philippe Mathieu-Daudé
2023-10-10 13:23     ` Alex Bennée
2024-11-14  9:28 ` [PATCH v17 00/16] TCG code quality tracking Nikita Shubin
2025-01-21 10:22   ` Chinmay Rath

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=87y1gamybk.fsf@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=fei2.wu@intel.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.