summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNeilBrown <neilb@suse.de>2014-12-18 16:48:15 +1100
committerNeilBrown <neilb@suse.de>2014-12-18 16:58:44 +1100
commit93d3bd3b28486ae3f933b3f94c81d27565d5e89e (patch)
tree8931e48a9de4858fb80a0489a43bc1dd5c8ab289
parent16afb1a5efb59b45db9ee46dad7cc410aabbe90e (diff)
downloadmdadm-93d3bd3b28486ae3f933b3f94c81d27565d5e89e.tar.gz
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 <jan@goneko.de> Resolved-debian-bug: 763917 URL: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=763917 Signed-off-by: NeilBrown <neilb@suse.de>
-rw-r--r--util.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/util.c b/util.c
index 37c6e0d..89f6b0f 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);
}