lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Sat,  5 Dec 2020 22:13:32 +0100
From:   Petr Machata <me@...chata.org>
To:     netdev@...r.kernel.org, dsahern@...il.com,
        stephen@...workplumber.org
Cc:     Po.Liu@....com, toke@...e.dk, dave.taht@...il.com,
        edumazet@...gle.com, tahiliani@...k.edu.in, leon@...nel.org,
        Petr Machata <me@...chata.org>
Subject: [PATCH iproute2-next v2 4/7] lib: sprint_size(): Uncrustify the code a bit

Ideally this and the rate printing would both be converted to a common
helper, but unfortunately the two format differently and this would break
tests and scripts out there. So just make the code look less like a wad of
hay.

Signed-off-by: Petr Machata <me@...chata.org>
---

Notes:
    v2:
    - This patch is new. It addresses a request from Stephen Hemminger to
      clean up the sprint_size() function.

 lib/json_print.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/lib/json_print.c b/lib/json_print.c
index c1df637642fd..d28e957c9603 100644
--- a/lib/json_print.c
+++ b/lib/json_print.c
@@ -344,13 +344,15 @@ int print_color_rate(bool use_iec, enum output_type type, enum color_attr color,
 
 char *sprint_size(__u32 sz, char *buf)
 {
+	long kilo = 1024;
+	long mega = kilo * kilo;
 	size_t len = SPRINT_BSIZE - 1;
 	double tmp = sz;
 
-	if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
-		snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
-	else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
-		snprintf(buf, len, "%gKb", rint(tmp/1024));
+	if (sz >= mega && fabs(mega * rint(tmp / mega) - sz) < 1024)
+		snprintf(buf, len, "%gMb", rint(tmp / mega));
+	else if (sz >= kilo && fabs(kilo * rint(tmp / kilo) - sz) < 16)
+		snprintf(buf, len, "%gKb", rint(tmp / kilo));
 	else
 		snprintf(buf, len, "%ub", sz);
 
-- 
2.25.1

Powered by blists - more mailing lists