Commit ad1fe0d8 authored by Daniel Borkmann's avatar Daniel Borkmann Committed by Stephen Hemminger

tc: util: fix print_rate for ludicrous speeds

The for loop should only probe up to G[i]bit rates, so that we
end up with T[i]bit as the last max units[] slot for snprintf(3),
and not possibly an invalid pointer in case rate is multiple of
kilo.

Fixes: 8cecdc28 ("tc: more user friendly rates")
Reported-by: default avatarJose R. Guzman Mosqueda <jose.r.guzman.mosqueda@intel.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 518af1e0
......@@ -250,18 +250,19 @@ void print_rate(char *buf, int len, __u64 rate)
extern int use_iec;
unsigned long kilo = use_iec ? 1024 : 1000;
const char *str = use_iec ? "i" : "";
int i = 0;
static char *units[5] = {"", "K", "M", "G", "T"};
int i;
rate <<= 3; /* bytes/sec -> bits/sec */
for (i = 0; i < ARRAY_SIZE(units); i++) {
for (i = 0; i < ARRAY_SIZE(units) - 1; i++) {
if (rate < kilo)
break;
if (((rate % kilo) != 0) && rate < 1000*kilo)
break;
rate /= kilo;
}
snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment