linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Goffredo Baroncelli <kreijack@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: Chris Mason <clmason@fusionio.com>,
	Goffredo Baroncelli <kreijack@inwind.it>
Subject: [BTRFS-PROGS][PATCH] pretty_sizes() returns incorrect values
Date: Mon,  1 Oct 2012 22:11:27 +0200	[thread overview]
Message-ID: <1349122287-6078-1-git-send-email-kreijack@gmail.com> (raw)

From: Goffredo Baroncelli <kreijack@inwind.it>

pretty_sizes() returns incorrect values if the argument is < 1024.

pretty_sizes(0) -> 0.00		OK
pretty_sizes(102) -> 0.10	WRONG
pretty_sizes(1023) -> 1.00	WRONG
pretty_sizes(1024) -> 1.00KB	OK

Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>

---
 utils.c |   30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/utils.c b/utils.c
index aade9e2..04c3e82 100644
--- a/utils.c
+++ b/utils.c
@@ -1097,25 +1097,27 @@ char *pretty_sizes(u64 size)
 {
 	int num_divs = 0;
         int pretty_len = 16;
-	u64 last_size = size;
-	u64 fract_size = size;
 	float fraction;
 	char *pretty;
 
-	while(size > 0) {
-		fract_size = last_size;
-		last_size = size;
-		size /= 1024;
-		num_divs++;
-	}
-	if (num_divs == 0)
-		num_divs = 1;
-	if (num_divs > ARRAY_SIZE(size_strs))
-		return NULL;
+	if( size < 1024 ){
+		fraction = size;
+		num_divs = 0;
+	} else {
+		u64 last_size = size;
+		num_divs = 0;
+		while(size >= 1024){
+			last_size = size;
+			size /= 1024;
+			num_divs ++;
+		}
 
-	fraction = (float)fract_size / 1024;
+		if (num_divs > ARRAY_SIZE(size_strs))
+			return NULL;
+		fraction = (float)last_size / 1024;
+	}
 	pretty = malloc(pretty_len);
-	snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs-1]);
+	snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]);
 	return pretty;
 }
 
-- 
1.7.10.4


                 reply	other threads:[~2012-10-01 20:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1349122287-6078-1-git-send-email-kreijack@gmail.com \
    --to=kreijack@gmail.com \
    --cc=clmason@fusionio.com \
    --cc=kreijack@inwind.it \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).