public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH v2 pbs] fix #4638: proxmox-backup-client: status: guard against div by zero
@ 2023-06-07  8:55 Maximiliano Sandoval
  2023-06-07  9:23 ` [pbs-devel] applied " Dietmar Maurer
  2023-06-07 11:13 ` [pbs-devel] " Thomas Lamprecht
  0 siblings, 2 replies; 3+ messages in thread
From: Maximiliano Sandoval @ 2023-06-07  8:55 UTC (permalink / raw)
  To: pbs-devel

We throw an error if the value for total is zero.

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
Previous patch contained a wrong email.

 proxmox-backup-client/src/main.rs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/proxmox-backup-client/src/main.rs b/proxmox-backup-client/src/main.rs
index 55198108..5a804d95 100644
--- a/proxmox-backup-client/src/main.rs
+++ b/proxmox-backup-client/src/main.rs
@@ -1590,9 +1590,12 @@ async fn status(param: Value) -> Result<Value, Error> {
         let v = v.as_u64().unwrap();
         let total = record["total"].as_u64().unwrap();
         let roundup = total / 200;
-        let per = ((v + roundup) * 100) / total;
-        let info = format!(" ({} %)", per);
-        Ok(format!("{} {:>8}", v, info))
+        if let Some(per) = ((v + roundup) * 100).checked_div(total) {
+            let info = format!(" ({} %)", per);
+            Ok(format!("{} {:>8}", v, info))
+        } else {
+            bail!("Cannot render total percentage: denominator is zero");
+        }
     };
 
     let options = default_table_format_options()
-- 
2.39.2





^ permalink raw reply	[flat|nested] 3+ messages in thread

* [pbs-devel] applied [PATCH v2 pbs] fix #4638: proxmox-backup-client: status: guard against div by zero
  2023-06-07  8:55 [pbs-devel] [PATCH v2 pbs] fix #4638: proxmox-backup-client: status: guard against div by zero Maximiliano Sandoval
@ 2023-06-07  9:23 ` Dietmar Maurer
  2023-06-07 11:13 ` [pbs-devel] " Thomas Lamprecht
  1 sibling, 0 replies; 3+ messages in thread
From: Dietmar Maurer @ 2023-06-07  9:23 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Maximiliano Sandoval

applied, thanks!




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [pbs-devel] [PATCH v2 pbs] fix #4638: proxmox-backup-client: status: guard against div by zero
  2023-06-07  8:55 [pbs-devel] [PATCH v2 pbs] fix #4638: proxmox-backup-client: status: guard against div by zero Maximiliano Sandoval
  2023-06-07  9:23 ` [pbs-devel] applied " Dietmar Maurer
@ 2023-06-07 11:13 ` Thomas Lamprecht
  1 sibling, 0 replies; 3+ messages in thread
From: Thomas Lamprecht @ 2023-06-07 11:13 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Maximiliano Sandoval

Am 07/06/2023 um 10:55 schrieb Maximiliano Sandoval:
> We throw an error if the value for total is zero.
> 
> Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
> ---
> Previous patch contained a wrong email.
> 
>  proxmox-backup-client/src/main.rs | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/proxmox-backup-client/src/main.rs b/proxmox-backup-client/src/main.rs
> index 55198108..5a804d95 100644
> --- a/proxmox-backup-client/src/main.rs
> +++ b/proxmox-backup-client/src/main.rs
> @@ -1590,9 +1590,12 @@ async fn status(param: Value) -> Result<Value, Error> {
>          let v = v.as_u64().unwrap();
>          let total = record["total"].as_u64().unwrap();
>          let roundup = total / 200;
> -        let per = ((v + roundup) * 100) / total;
> -        let info = format!(" ({} %)", per);
> -        Ok(format!("{} {:>8}", v, info))
> +        if let Some(per) = ((v + roundup) * 100).checked_div(total) {
> +            let info = format!(" ({} %)", per);

FYI: When touching existing format lines, please feel encouraged to switch over
to using template variables wherever possible:

let info = format!(" ({per} %)");
Ok(format!("{v} {info:>8}"))

> +            Ok(format!("{} {:>8}", v, info))
> +        } else {
> +            bail!("Cannot render total percentage: denominator is zero");
> +        }
>      };
>  
>      let options = default_table_format_options()





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-07 11:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07  8:55 [pbs-devel] [PATCH v2 pbs] fix #4638: proxmox-backup-client: status: guard against div by zero Maximiliano Sandoval
2023-06-07  9:23 ` [pbs-devel] applied " Dietmar Maurer
2023-06-07 11:13 ` [pbs-devel] " Thomas Lamprecht

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal