From patchwork Wed Mar 9 02:10:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 8541441 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id AC447C0553 for ; Wed, 9 Mar 2016 02:13:32 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D6EF620211 for ; Wed, 9 Mar 2016 02:13:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0574A201BC for ; Wed, 9 Mar 2016 02:13:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751964AbcCICN1 (ORCPT ); Tue, 8 Mar 2016 21:13:27 -0500 Received: from cn.fujitsu.com ([222.73.24.84]:6701 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1751621AbcCICNZ (ORCPT ); Tue, 8 Mar 2016 21:13:25 -0500 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208";a="384089" Received: from unknown (HELO cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 09 Mar 2016 10:13:03 +0800 Received: from localhost.localdomain (unknown [10.167.226.34]) by cn.fujitsu.com (Postfix) with ESMTP id 7E60E4056401 for ; Wed, 9 Mar 2016 10:13:01 +0800 (CST) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH] btrfs-progs: util: Fix a wrong unit of pretty_size Date: Wed, 9 Mar 2016 10:10:55 +0800 Message-Id: <1457489455-9685-1-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.7.2 MIME-Version: 1.0 X-yoursite-MailScanner-ID: 7E60E4056401.AC40E X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@cn.fujitsu.com X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- utils.c | 7 +++++++ 1 file changed, 7 insertions(+) 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)) {