From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 54EF6C6FA82 for ; Mon, 12 Sep 2022 19:03:15 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 34EE484A71; Mon, 12 Sep 2022 21:03:13 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="PHFKeouy"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D33D784A73; Mon, 12 Sep 2022 21:03:10 +0200 (CEST) Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 56FFE8494A for ; Mon, 12 Sep 2022 21:03:08 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=pali@kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4A23560B4D; Mon, 12 Sep 2022 19:03:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82194C433D6; Mon, 12 Sep 2022 19:03:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1663009386; bh=n4bRDOOGUj6AReN3FSYwejhkh4o7heWreb1vdwHLxs8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PHFKeouy4pifxnUIwTqySswjBOVKmMaZkGlnWRj3gshNbh2X7oEBu0gUuaHG2QB0y iBPF7nPfKR8X+GKaCmcAZFfaHagmewZx2a9qL1w9jVgELl6YjeZf3CWAYlIhEVOYtD M1YC83Fe8JgmTiSR+kBhWXnvmfkmq+wNcfa3CCAT2TlKatTg+8upV+WexPVH5dxpIN GYQOCHb6pE1tUOUt4ATKhS286jKeTnYBhkVpf9U1zCFNIY7WD9q4TfCeDbCPSOSyJ4 vXAhau5scoPgcuhv5ly2La60+QTya3bC8Mpcc7z9rGSTkjmyIroizLrxI52a8uDFQP 3sUTA52d3yXvg== Received: by pali.im (Postfix) id EC12111D3; Mon, 12 Sep 2022 21:03:03 +0200 (CEST) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: Simon Glass Cc: u-boot@lists.denx.de Subject: [PATCH v2] display_options: print_size: Fix order overflow Date: Mon, 12 Sep 2022 21:02:27 +0200 Message-Id: <20220912190227.20015-1-pali@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20220911093707.9434-1-pali@kernel.org> References: <20220911093707.9434-1-pali@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Function print_size() round size to the nearst value with one decimal fraction number. But in special cases also unit order may overflow. For example value 1073689396 is printed as "1024 MiB" and value 1073741824 as "1 GiB". Fix this issue by detecting order overflow and increasing unit order. With this change also value 1073689396 is printed as "1 GiB". Signed-off-by: Pali Rohár --- Changes in v2: * Add unit test case --- lib/display_options.c | 6 ++++++ test/lib/test_print.c | 3 +++ 2 files changed, 9 insertions(+) diff --git a/lib/display_options.c b/lib/display_options.c index 360b01bcf5ff..59ed4f61b741 100644 --- a/lib/display_options.c +++ b/lib/display_options.c @@ -126,6 +126,12 @@ void print_size(uint64_t size, const char *s) if (m >= 10) { m -= 10; n += 1; + + if (n == 1024 && i > 0) { + n = 1; + m = 0; + c = names[i - 1]; + } } } diff --git a/test/lib/test_print.c b/test/lib/test_print.c index a60a5a51f126..79b67c779321 100644 --- a/test/lib/test_print.c +++ b/test/lib/test_print.c @@ -68,6 +68,9 @@ static int lib_test_print_size(struct unit_test_state *uts) ut_assertok(test_print_size(uts, 7654321, "7.3 MiB;")); ut_assertok(test_print_size(uts, 87654321, "83.6 MiB;")); ut_assertok(test_print_size(uts, 987654321, "941.9 MiB;")); + ut_assertok(test_print_size(uts, 1073689395, "1023.9 MiB;")); + ut_assertok(test_print_size(uts, 1073689396, "1 GiB;")); + ut_assertok(test_print_size(uts, 1073741824, "1 GiB;")); ut_assertok(test_print_size(uts, 1987654321, "1.9 GiB;")); ut_assertok(test_print_size(uts, 54321987654321, "49.4 TiB;")); return 0; -- 2.20.1