]> granicus.if.org Git - procps-ng/commitdiff
top: try avoiding the edge of a 'divide by zero' cliff <=== port of newlib 5f9185e0
authorJim Warner <james.warner@comcast.net>
Mon, 19 Sep 2022 05:00:00 +0000 (00:00 -0500)
committerCraig Small <csmall@dropbear.xyz>
Tue, 20 Sep 2022 09:34:40 +0000 (19:34 +1000)
______________________________ original newlib message
----------------------------------- ( minus git hash )

Darn, after testing on some older, out of date distros
I was embarrassed to find some awful code I created in
the commit shown below. I was rewarded with some 'nan'
floating point values and 'inf' computational results.

Reference(s);
. a missed opportunity to repent
commit ........................................
. true source of my original sin
commit ........................................

Signed-off-by: Jim Warner <james.warner@comcast.net>
top/top.c

index f6950191e6373ac7a91374c8043324e8d86a1bf6..9c046e58967152edd2f8dd288684e16a0d80dd4c 100644 (file)
--- a/top/top.c
+++ b/top/top.c
@@ -6331,19 +6331,22 @@ static struct rx_st *sum_rx (struct graph_parms *these) {
    static __thread struct rx_st rx;
    char buf1[SMLBUFSIZ], buf2[SMLBUFSIZ], buf3[MEDBUFSIZ];
    int ix, num1, num2, width;
-   float scale;
+   float scale = 0.0;
 
-   scale = 100.0 / these->total;
+   if (these->total > 0)
+      scale = 100.0 / these->total;
    rx.pcnt_one = scale * these->part1;
    rx.pcnt_two = scale * these->part2;
    if (rx.pcnt_one + rx.pcnt_two > 100.0 || rx.pcnt_two < 0)
       rx.pcnt_two = 0;
    rx.pcnt_tot = rx.pcnt_one + rx.pcnt_two;
 
-   num1 = (int)((rx.pcnt_one * these->adjust) + .5),
+   num1 = (int)((rx.pcnt_one * these->adjust) + .5);
    num2 = (int)((rx.pcnt_two * these->adjust) + .5);
-   if (num1 + num2 > these->length)
+   if (num1 + num2 > these->length) {
+      if (num1 > these->length) num1 = these->length;
       num2 = these->length - num1;
+   }
 
    width = these->length;
    buf1[0] = buf2[0] = buf3[0] = '\0';