Skip to contentSkip to conversation

display_options: print_size: Fix order overflow

PATCH Superseded Pali Rohár 3 participants 6 messages Sep 11, 2022 – Sep 23, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 display_options: print_size: Fix order overflow

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".

---
 lib/display_options.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/display_options.c +6 -0
index 360b01bcf5ff..c281c1d2c10d 100644
@@ -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];
+			}
 		}
 	}
 
Signature · 1 line
2.20.1
#1 lore raw

Hi Pali,

Quoted 33 lines · Pali Rohár

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".

---
 lib/display_options.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/display_options.c +6 -1
index 360b01bcf5ff..c281c1d2c10d 100644
@@ -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];
+                       }
                }
        }
 
--
Quoted 2 lines

2.20.1

Please add a test case for this to test/lib/test_print.c

Regards,
Simon

#2 lore raw
Quoted 35 lines · On Monday 12 September 2022 07:34:43 Simon Glass wrote:

Hi Pali,

Quoted 32 lines · Pali Rohár

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".

---
 lib/display_options.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/display_options.c +6 -0
index 360b01bcf5ff..c281c1d2c10d 100644
@@ -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];
+                       }
                }
        }
 
Quoted 8 lines
Quoted 3 lines

--
2.20.1

Please add a test case for this to test/lib/test_print.c

Regards,
Simon

Ok, I will look at it.

#3 lore raw

PATCH v2 1/1 display_options: print_size: Fix order overflow

In reply to Pali Rohár

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".

---
Changes in v2:
* Add unit test case

---
 lib/display_options.c | 6 ++++++
 test/lib/test_print.c | 3 +++
 2 files changed, 9 insertions(+)
lib/display_options.c +6 -0
index 360b01bcf5ff..59ed4f61b741 100644
@@ -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];
+			}
 		}
 	}
 
test/lib/test_print.c +3 -0
index a60a5a51f126..79b67c779321 100644
@@ -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;
Signature · 1 line
2.20.1
Reviewed-by on v2 1/1
#4 lore raw
Quoted 12 lines · Pali Rohár

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".

Quoted 7 lines

---
Changes in v2:
* Add unit test case

---
 lib/display_options.c | 6 ++++++
 test/lib/test_print.c | 3 +++
 2 files changed, 9 insertions(+)
#5 lore raw
In reply to Pali Rohár
Quoted 11 lines · Pali Rohár

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".

Applied to u-boot/next, thanks!

Signature · 1 line
Tom