]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
display_options: print_size: Fix order overflow
authorPali Rohár <pali@kernel.org>
Mon, 12 Sep 2022 19:02:27 +0000 (21:02 +0200)
committerTom Rini <trini@konsulko.com>
Fri, 23 Sep 2022 19:13:18 +0000 (15:13 -0400)
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 <pali@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
lib/display_options.c
test/lib/test_print.c

index 7feb446a559086a891e39c6160a6b674cd6ec9e9..80def5201f96d068013667b66ee208ca1798a470 100644 (file)
@@ -127,6 +127,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];
+                       }
                }
        }
 
index a60a5a51f1269483dbf27d5282254a193a6339dd..79b67c779321c279c48433750c0b4c8b49a751c2 100644 (file)
@@ -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;