From: NeilBrown Date: Thu, 18 Dec 2014 05:48:15 +0000 (+1100) Subject: util: remove rounding error where reporting "human sizes". X-Git-Tag: mdadm-3.3.3~87 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93d3bd3b28486ae3f933b3f94c81d27565d5e89e;p=thirdparty%2Fmdadm.git util: remove rounding error where reporting "human sizes". The division 1<<20 / 200 is not exact, so dividing by this to convert bytes into half-megs is wrong and results in incorrect output. As we are doing "long long" arithmetic, there is no risk of an overflow until we reach 64 petabytes. So change to * 200 / (1<<20). Reported-by: Jan Echternach Resolved-debian-bug: 763917 URL: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=763917 Signed-off-by: NeilBrown --- diff --git a/util.c b/util.c index 37c6e0d3..89f6b0f4 100644 --- a/util.c +++ b/util.c @@ -671,13 +671,13 @@ char *human_size(long long bytes) if (bytes < 5000*1024) buf[0] = 0; else if (bytes < 2*1024LL*1024LL*1024LL) { - long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; + long cMiB = (bytes * 200LL / (1LL<<20) + 1) / 2; long cMB = (bytes / ( 1000000LL / 200LL ) +1) /2; snprintf(buf, sizeof(buf), " (%ld.%02ld MiB %ld.%02ld MB)", cMiB/100 , cMiB % 100, cMB/100, cMB % 100); } else { - long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; + long cGiB = (bytes * 200LL / (1LL<<30) +1) / 2; long cGB = (bytes / (1000000000LL/200LL ) +1) /2; snprintf(buf, sizeof(buf), " (%ld.%02ld GiB %ld.%02ld GB)", cGiB/100 , cGiB % 100, @@ -706,11 +706,11 @@ char *human_size_brief(long long bytes, int prefix) buf[0] = 0; else if (prefix == IEC) { if (bytes < 2*1024LL*1024LL*1024LL) { - long cMiB = (bytes / ( (1LL<<20) / 200LL ) +1) /2; + long cMiB = (bytes * 200LL / (1LL<<20) +1) /2; snprintf(buf, sizeof(buf), "%ld.%02ldMiB", cMiB/100 , cMiB % 100); } else { - long cGiB = (bytes / ( (1LL<<30) / 200LL ) +1) /2; + long cGiB = (bytes * 200LL / (1LL<<30) +1) /2; snprintf(buf, sizeof(buf), "%ld.%02ldGiB", cGiB/100 , cGiB % 100); }