diff mbox

btrfs-progs: util: Fix a wrong unit of pretty_size

Message ID 1457489455-9685-1-git-send-email-quwenruo@cn.fujitsu.com (mailing list archive)
State Accepted
Headers show

Commit Message

Qu Wenruo March 9, 2016, 2:10 a.m. UTC
If parameter for pretty_size is smaller than default base(1024),
pretty_size() will output wrong unit.
For example, pretty_size(1008) will output '0.98B' not '1008B' or
'0.98KiB'.

The cause is, for default base and auto-detect unit, base will be 1024
but num_divs is still 0, last result will still be divided by base,
causing the bug.

Fix it by checking num_divs in default case, and if num_divs is 0,
change base to 1.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 utils.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

David Sterba March 9, 2016, 1:03 p.m. UTC | #1
On Wed, Mar 09, 2016 at 10:10:55AM +0800, Qu Wenruo wrote:
> If parameter for pretty_size is smaller than default base(1024),
> pretty_size() will output wrong unit.
> For example, pretty_size(1008) will output '0.98B' not '1008B' or
> '0.98KiB'.
> 
> The cause is, for default base and auto-detect unit, base will be 1024
> but num_divs is still 0, last result will still be divided by base,
> causing the bug.
> 
> Fix it by checking num_divs in default case, and if num_divs is 0,
> change base to 1.
> 
> Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>

Applied, thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/utils.c b/utils.c
index f814017..52237db 100644
--- a/utils.c
+++ b/utils.c
@@ -1695,6 +1695,13 @@  int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mod
 			size /= mult;
 			num_divs++;
 		}
+		/*
+		 * If the value is smaller than base, we didn't do any
+		 * division, in that case, base should be 1, not original
+		 * base, or the unit will be wrong
+		 */
+		if (num_divs == 0)
+			base = 1;
 	}
 
 	if (num_divs >= ARRAY_SIZE(unit_suffix_binary)) {