diff mbox series

[v4,02/22] monitor: make hmp_handle_error return a boolean

Message ID 20211028155457.967291-3-berrange@redhat.com
State New
Headers show
Series monitor: explicitly permit QMP commands to be added for all use cases | expand

Commit Message

Daniel P. Berrangé Oct. 28, 2021, 3:54 p.m. UTC
This turns the pattern

  if (err) {
     hmp_handle_error(mon, err);
     return;
  }

into

  if (hmp_handle_error(mon, err);
     return;
  }

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 hw/core/machine-hmp-cmds.c |  3 +--
 include/monitor/hmp.h      |  2 +-
 monitor/hmp-cmds.c         | 28 +++++++++++-----------------
 3 files changed, 13 insertions(+), 20 deletions(-)

Comments

Dr. David Alan Gilbert Oct. 28, 2021, 4:04 p.m. UTC | #1
* Daniel P. Berrangé (berrange@redhat.com) wrote:
> This turns the pattern
> 
>   if (err) {
>      hmp_handle_error(mon, err);
>      return;
>   }
> 
> into
> 
>   if (hmp_handle_error(mon, err);
                                  ^^^ {
>      return;
>   }
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Yeh, I guess so, doesn't save much.


Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>

> ---
>  hw/core/machine-hmp-cmds.c |  3 +--
>  include/monitor/hmp.h      |  2 +-
>  monitor/hmp-cmds.c         | 28 +++++++++++-----------------
>  3 files changed, 13 insertions(+), 20 deletions(-)
> 
> diff --git a/hw/core/machine-hmp-cmds.c b/hw/core/machine-hmp-cmds.c
> index 76b22b00d6..c356783ab9 100644
> --- a/hw/core/machine-hmp-cmds.c
> +++ b/hw/core/machine-hmp-cmds.c
> @@ -53,8 +53,7 @@ void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
>      HotpluggableCPUList *saved = l;
>      CpuInstanceProperties *c;
>  
> -    if (err != NULL) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>  
> diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
> index 6bc27639e0..a2cb002a3a 100644
> --- a/include/monitor/hmp.h
> +++ b/include/monitor/hmp.h
> @@ -16,7 +16,7 @@
>  
>  #include "qemu/readline.h"
>  
> -void hmp_handle_error(Monitor *mon, Error *err);
> +bool hmp_handle_error(Monitor *mon, Error *err);
>  
>  void hmp_info_name(Monitor *mon, const QDict *qdict);
>  void hmp_info_version(Monitor *mon, const QDict *qdict);
> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
> index bcaa41350e..9031cea881 100644
> --- a/monitor/hmp-cmds.c
> +++ b/monitor/hmp-cmds.c
> @@ -62,11 +62,13 @@
>  #include <spice/enums.h>
>  #endif
>  
> -void hmp_handle_error(Monitor *mon, Error *err)
> +bool hmp_handle_error(Monitor *mon, Error *err)
>  {
>      if (err) {
>          error_reportf_err(err, "Error: ");
> +        return true;
>      }
> +    return false;
>  }
>  
>  /*
> @@ -577,8 +579,7 @@ void hmp_info_vnc(Monitor *mon, const QDict *qdict)
>  
>      info2l = qmp_query_vnc_servers(&err);
>      info2l_head = info2l;
> -    if (err) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>      if (!info2l) {
> @@ -693,8 +694,7 @@ void hmp_info_balloon(Monitor *mon, const QDict *qdict)
>      Error *err = NULL;
>  
>      info = qmp_query_balloon(&err);
> -    if (err) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>  
> @@ -1065,8 +1065,7 @@ void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
>      int i;
>  
>      data = qmp_ringbuf_read(chardev, size, false, 0, &err);
> -    if (err) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>  
> @@ -1582,8 +1581,7 @@ void hmp_migrate(Monitor *mon, const QDict *qdict)
>  
>      qmp_migrate(uri, !!blk, blk, !!inc, inc,
>                  false, false, true, resume, &err);
> -    if (err) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>  
> @@ -1917,8 +1915,7 @@ void hmp_rocker(Monitor *mon, const QDict *qdict)
>      Error *err = NULL;
>  
>      rocker = qmp_query_rocker(name, &err);
> -    if (err != NULL) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>  
> @@ -1936,8 +1933,7 @@ void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
>      Error *err = NULL;
>  
>      list = qmp_query_rocker_ports(name, &err);
> -    if (err != NULL) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>  
> @@ -1965,8 +1961,7 @@ void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
>      Error *err = NULL;
>  
>      list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
> -    if (err != NULL) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>  
> @@ -2115,8 +2110,7 @@ void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
>      Error *err = NULL;
>  
>      list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
> -    if (err != NULL) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>  
> -- 
> 2.31.1
>
Philippe Mathieu-Daudé Oct. 28, 2021, 4:47 p.m. UTC | #2
On 10/28/21 17:54, Daniel P. Berrangé wrote:
> This turns the pattern
> 
>   if (err) {
>      hmp_handle_error(mon, err);
>      return;
>   }
> 
> into
> 
>   if (hmp_handle_error(mon, err);
>      return;
>   }
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  hw/core/machine-hmp-cmds.c |  3 +--
>  include/monitor/hmp.h      |  2 +-
>  monitor/hmp-cmds.c         | 28 +++++++++++-----------------
>  3 files changed, 13 insertions(+), 20 deletions(-)
> 
> diff --git a/hw/core/machine-hmp-cmds.c b/hw/core/machine-hmp-cmds.c
> index 76b22b00d6..c356783ab9 100644
> --- a/hw/core/machine-hmp-cmds.c
> +++ b/hw/core/machine-hmp-cmds.c
> @@ -53,8 +53,7 @@ void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
>      HotpluggableCPUList *saved = l;
>      CpuInstanceProperties *c;
>  
> -    if (err != NULL) {
> -        hmp_handle_error(mon, err);
> +    if (hmp_handle_error(mon, err)) {
>          return;
>      }
>  
> diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
> index 6bc27639e0..a2cb002a3a 100644
> --- a/include/monitor/hmp.h
> +++ b/include/monitor/hmp.h
> @@ -16,7 +16,7 @@
>  
>  #include "qemu/readline.h"
>  
> -void hmp_handle_error(Monitor *mon, Error *err);
> +bool hmp_handle_error(Monitor *mon, Error *err);
>  
>  void hmp_info_name(Monitor *mon, const QDict *qdict);
>  void hmp_info_version(Monitor *mon, const QDict *qdict);
> diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
> index bcaa41350e..9031cea881 100644
> --- a/monitor/hmp-cmds.c
> +++ b/monitor/hmp-cmds.c
> @@ -62,11 +62,13 @@
>  #include <spice/enums.h>
>  #endif
>  
> -void hmp_handle_error(Monitor *mon, Error *err)
> +bool hmp_handle_error(Monitor *mon, Error *err)
>  {
>      if (err) {
>          error_reportf_err(err, "Error: ");
> +        return true;
>      }
> +    return false;
>  }

The usual pattern is to return false on error. Anyhow,
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Daniel P. Berrangé Nov. 1, 2021, 3:54 p.m. UTC | #3
On Thu, Oct 28, 2021 at 06:47:38PM +0200, Philippe Mathieu-Daudé wrote:
> On 10/28/21 17:54, Daniel P. Berrangé wrote:
> > This turns the pattern
> > 
> >   if (err) {
> >      hmp_handle_error(mon, err);
> >      return;
> >   }
> > 
> > into
> > 
> >   if (hmp_handle_error(mon, err);
> >      return;
> >   }
> > 
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> > ---
> >  hw/core/machine-hmp-cmds.c |  3 +--
> >  include/monitor/hmp.h      |  2 +-
> >  monitor/hmp-cmds.c         | 28 +++++++++++-----------------
> >  3 files changed, 13 insertions(+), 20 deletions(-)
> > 
> > diff --git a/hw/core/machine-hmp-cmds.c b/hw/core/machine-hmp-cmds.c
> > index 76b22b00d6..c356783ab9 100644
> > --- a/hw/core/machine-hmp-cmds.c
> > +++ b/hw/core/machine-hmp-cmds.c
> > @@ -53,8 +53,7 @@ void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
> >      HotpluggableCPUList *saved = l;
> >      CpuInstanceProperties *c;
> >  
> > -    if (err != NULL) {
> > -        hmp_handle_error(mon, err);
> > +    if (hmp_handle_error(mon, err)) {
> >          return;
> >      }
> >  
> > diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
> > index 6bc27639e0..a2cb002a3a 100644
> > --- a/include/monitor/hmp.h
> > +++ b/include/monitor/hmp.h
> > @@ -16,7 +16,7 @@
> >  
> >  #include "qemu/readline.h"
> >  
> > -void hmp_handle_error(Monitor *mon, Error *err);
> > +bool hmp_handle_error(Monitor *mon, Error *err);
> >  
> >  void hmp_info_name(Monitor *mon, const QDict *qdict);
> >  void hmp_info_version(Monitor *mon, const QDict *qdict);
> > diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
> > index bcaa41350e..9031cea881 100644
> > --- a/monitor/hmp-cmds.c
> > +++ b/monitor/hmp-cmds.c
> > @@ -62,11 +62,13 @@
> >  #include <spice/enums.h>
> >  #endif
> >  
> > -void hmp_handle_error(Monitor *mon, Error *err)
> > +bool hmp_handle_error(Monitor *mon, Error *err)
> >  {
> >      if (err) {
> >          error_reportf_err(err, "Error: ");
> > +        return true;
> >      }
> > +    return false;
> >  }
> 
> The usual pattern is to return false on error. Anyhow,

NB That pattern is for functions which are setting errors
via an Error **errp output parameter.

This function is for checking whether an error is set and
reporting message to console, via an Error *err input
parameter.

> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> 

Regards,
Daniel
diff mbox series

Patch

diff --git a/hw/core/machine-hmp-cmds.c b/hw/core/machine-hmp-cmds.c
index 76b22b00d6..c356783ab9 100644
--- a/hw/core/machine-hmp-cmds.c
+++ b/hw/core/machine-hmp-cmds.c
@@ -53,8 +53,7 @@  void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
     HotpluggableCPUList *saved = l;
     CpuInstanceProperties *c;
 
-    if (err != NULL) {
-        hmp_handle_error(mon, err);
+    if (hmp_handle_error(mon, err)) {
         return;
     }
 
diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
index 6bc27639e0..a2cb002a3a 100644
--- a/include/monitor/hmp.h
+++ b/include/monitor/hmp.h
@@ -16,7 +16,7 @@ 
 
 #include "qemu/readline.h"
 
-void hmp_handle_error(Monitor *mon, Error *err);
+bool hmp_handle_error(Monitor *mon, Error *err);
 
 void hmp_info_name(Monitor *mon, const QDict *qdict);
 void hmp_info_version(Monitor *mon, const QDict *qdict);
diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
index bcaa41350e..9031cea881 100644
--- a/monitor/hmp-cmds.c
+++ b/monitor/hmp-cmds.c
@@ -62,11 +62,13 @@ 
 #include <spice/enums.h>
 #endif
 
-void hmp_handle_error(Monitor *mon, Error *err)
+bool hmp_handle_error(Monitor *mon, Error *err)
 {
     if (err) {
         error_reportf_err(err, "Error: ");
+        return true;
     }
+    return false;
 }
 
 /*
@@ -577,8 +579,7 @@  void hmp_info_vnc(Monitor *mon, const QDict *qdict)
 
     info2l = qmp_query_vnc_servers(&err);
     info2l_head = info2l;
-    if (err) {
-        hmp_handle_error(mon, err);
+    if (hmp_handle_error(mon, err)) {
         return;
     }
     if (!info2l) {
@@ -693,8 +694,7 @@  void hmp_info_balloon(Monitor *mon, const QDict *qdict)
     Error *err = NULL;
 
     info = qmp_query_balloon(&err);
-    if (err) {
-        hmp_handle_error(mon, err);
+    if (hmp_handle_error(mon, err)) {
         return;
     }
 
@@ -1065,8 +1065,7 @@  void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
     int i;
 
     data = qmp_ringbuf_read(chardev, size, false, 0, &err);
-    if (err) {
-        hmp_handle_error(mon, err);
+    if (hmp_handle_error(mon, err)) {
         return;
     }
 
@@ -1582,8 +1581,7 @@  void hmp_migrate(Monitor *mon, const QDict *qdict)
 
     qmp_migrate(uri, !!blk, blk, !!inc, inc,
                 false, false, true, resume, &err);
-    if (err) {
-        hmp_handle_error(mon, err);
+    if (hmp_handle_error(mon, err)) {
         return;
     }
 
@@ -1917,8 +1915,7 @@  void hmp_rocker(Monitor *mon, const QDict *qdict)
     Error *err = NULL;
 
     rocker = qmp_query_rocker(name, &err);
-    if (err != NULL) {
-        hmp_handle_error(mon, err);
+    if (hmp_handle_error(mon, err)) {
         return;
     }
 
@@ -1936,8 +1933,7 @@  void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
     Error *err = NULL;
 
     list = qmp_query_rocker_ports(name, &err);
-    if (err != NULL) {
-        hmp_handle_error(mon, err);
+    if (hmp_handle_error(mon, err)) {
         return;
     }
 
@@ -1965,8 +1961,7 @@  void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
     Error *err = NULL;
 
     list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
-    if (err != NULL) {
-        hmp_handle_error(mon, err);
+    if (hmp_handle_error(mon, err)) {
         return;
     }
 
@@ -2115,8 +2110,7 @@  void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
     Error *err = NULL;
 
     list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
-    if (err != NULL) {
-        hmp_handle_error(mon, err);
+    if (hmp_handle_error(mon, err)) {
         return;
     }