[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <063D6719AE5E284EB5DD2968C1650D6D1C9E72A7@AcuExch.aculab.com>
Date: Tue, 4 Nov 2014 11:06:50 +0000
From: David Laight <David.Laight@...LAB.COM>
To: 'Christian Hesse' <mail@...rm.de>,
Stephen Hemminger <stephen@...workplumber.org>
CC: "netdev@...r.kernel.org" <netdev@...r.kernel.org>
Subject: RE: [PATCH v2 1/1] ip-link: in human readable output use dynamic
precision length
From: Christian Hesse
...
> diff --git a/ip/ipaddress.c b/ip/ipaddress.c
> index e240bb5..55cbc77 100644
> --- a/ip/ipaddress.c
> +++ b/ip/ipaddress.c
> @@ -324,6 +324,7 @@ static void print_num(FILE *fp, unsigned width, uint64_t count)
> const char *prefix = "kMGTPE";
> const unsigned int base = use_iec ? 1024 : 1000;
> uint64_t powi = 1;
> + int precision;
> char buf[64];
>
> if (!human_readable || count < base) {
> @@ -343,8 +344,11 @@ static void print_num(FILE *fp, unsigned width, uint64_t count)
> ++prefix;
> }
>
> - snprintf(buf, sizeof(buf), "%.1f%c%s", (double) count / powi,
> - *prefix, use_iec ? "i" : "");
> + if ((precision = 3 - snprintf(NULL, 0, "%"PRIu64, count / powi)) < 0)
Don't put assignments in conditionals.
> + precision = 0;
> +
> + snprintf(buf, sizeof(buf), "%.*f%c%s", precision,
> + (double) count / powi, *prefix, use_iec ? "i" : "");
>
> fprintf(fp, "%-*s ", width, buf);
> }
> --
> 2.1.3
The above will go wrong in all sorts of horrid ways....
For instance you are doing a truncating integer divide, but the FP
value will get rounded for display.
It would be safer to use integers throughout.
Oh, and a 2Mbit E1 link is actually 2048000 :-)
David
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Powered by blists - more mailing lists