[PATCH 1/3] Avoid 32-bit overflow for rates when calculating bar length
Avoid 32-bit overflow by using double instead of int. uint64_t would be another option but these are only ever used in conjunction with floats. (float was also an option) --- ui.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/ui.c b/ui.c index 57ca6c0..8a3b9d0 100644 --- a/ui.c +++ b/ui.c @@ -71,7 +71,8 @@ int dontshowdisplay = 0;
/* Barchart scales. */ static struct { - int max, interval; + double max; + int interval; } scale[] = { { 64000, 10 }, /* 64 kbit/s */ { 128000, 10 }, @@ -105,7 +106,7 @@ static float get_max_bandwidth() { }
/* rate in bits */ -static int get_bar_length(const int rate) { +static int get_bar_length(const double rate) { float l; if (rate <= 0) return 0;
[PATCH 2/3] scale[] up to tbit
Extend the scale[] array up to terabit. 10gbit is not uncommon, 100gbit 40 and 100 gbit are coming, 400 gbit and terabit are future possibilities. --- ui.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/ui.c b/ui.c index 8a3b9d0..d1500ad 100644 --- a/ui.c +++ b/ui.c @@ -74,13 +74,16 @@ static struct { double max; int interval; } scale[] = { - { 64000, 10 }, /* 64 kbit/s */ - { 128000, 10 }, - { 256000, 10 }, - { 1000000, 10 }, /* 1 Mbit/s */ - { 10000000, 10 }, - { 100000000, 100 }, - { 1000000000, 100 } /* 1 Gbit/s */ + { 64000, 10 }, /* 64 kbit/s */ + { 128000, 10 }, + { 256000, 10 }, + { 1000000, 10 }, /* 1 Mbit/s */ + { 10000000, 10 }, + { 100000000, 100 }, + { 1000000000, 100 }, /* 1 Gbit/s */ + { 10000000000, 100 }, + { 100000000000, 100 }, + { 1000000000000, 100 }, /* 1 Tbit/s */ }; static int rateidx = 0, wantbiggerrate;
[PATCH 3/3] rateidx_init fix
When calculating the first rateidx, we were overshooting to the next scale. Fix that. --- ui.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/ui.c b/ui.c index d1500ad..15624bf 100644 --- a/ui.c +++ b/ui.c @@ -116,7 +116,8 @@ static int get_bar_length(const double rate) { if (rate > scale[rateidx].max) { wantbiggerrate = 1; if(! rateidx_init) { - while(rate > scale[rateidx_init++].max) { + while(rate > scale[rateidx_init].max) { + rateidx_init++; } rateidx = rateidx_init; }