summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Warner <james.warner@comcast.net>2022-09-18 00:00:00 -0500
committerCraig Small <csmall@dropbear.xyz>2022-09-20 18:50:55 +1000
commit5f9185e087d0bba3d9ee8c59fbf43b6d658cf70e (patch)
tree34a75af4f246f5317e8fcd88a6ae13c5d4cd727d
parent124f26a423854614aae6bb9e4188e39cadb6b77c (diff)
downloadprocps-ng-5f9185e087d0bba3d9ee8c59fbf43b6d658cf70e.tar.gz
top: try avoiding the edge of a 'divide by zero' cliff
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 5c5bff392bfaf78b7bacb9b954c1fd1e9d818544 . true source of my original sin commit 2d5b51d1a2aa19d077a2f3db5be187f982d70e70 Signed-off-by: Jim Warner <james.warner@comcast.net>
-rw-r--r--src/top/top.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/src/top/top.c b/src/top/top.c
index 9fccc3e..9b2720e 100644
--- a/src/top/top.c
+++ b/src/top/top.c
@@ -6198,19 +6198,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';