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>
This commit is contained in:
NeilBrown 2014-12-18 16:48:15 +11:00
parent 16afb1a5ef
commit 93d3bd3b28
1 changed files with 4 additions and 4 deletions

8
util.c
View File

@ -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);
}