All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] display_options: print_size: Fix order overflow
@ 2022-09-11  9:37 Pali Rohár
  2022-09-12 13:34 ` Simon Glass
  2022-09-12 19:02 ` [PATCH v2] " Pali Rohár
  0 siblings, 2 replies; 6+ messages in thread
From: Pali Rohár @ 2022-09-11  9:37 UTC (permalink / raw)
  To: u-boot

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>
---
 lib/display_options.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lib/display_options.c b/lib/display_options.c
index 360b01bcf5ff..c281c1d2c10d 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];
+			}
 		}
 	}
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] display_options: print_size: Fix order overflow
  2022-09-11  9:37 [PATCH] display_options: print_size: Fix order overflow Pali Rohár
@ 2022-09-12 13:34 ` Simon Glass
  2022-09-12 18:56   ` Pali Rohár
  2022-09-12 19:02 ` [PATCH v2] " Pali Rohár
  1 sibling, 1 reply; 6+ messages in thread
From: Simon Glass @ 2022-09-12 13:34 UTC (permalink / raw)
  To: Pali Rohár; +Cc: U-Boot Mailing List

Hi Pali,

On Sun, 11 Sept 2022 at 03:38, Pali Rohár <pali@kernel.org> wrote:
>
> 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>
> ---
>  lib/display_options.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/lib/display_options.c b/lib/display_options.c
> index 360b01bcf5ff..c281c1d2c10d 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];
> +                       }
>                 }
>         }
>
> --
> 2.20.1
>

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

Regards,
Simon

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] display_options: print_size: Fix order overflow
  2022-09-12 13:34 ` Simon Glass
@ 2022-09-12 18:56   ` Pali Rohár
  0 siblings, 0 replies; 6+ messages in thread
From: Pali Rohár @ 2022-09-12 18:56 UTC (permalink / raw)
  To: Simon Glass; +Cc: U-Boot Mailing List

On Monday 12 September 2022 07:34:43 Simon Glass wrote:
> Hi Pali,
> 
> On Sun, 11 Sept 2022 at 03:38, Pali Rohár <pali@kernel.org> wrote:
> >
> > 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>
> > ---
> >  lib/display_options.c | 6 ++++++
> >  1 file changed, 6 insertions(+)
> >
> > diff --git a/lib/display_options.c b/lib/display_options.c
> > index 360b01bcf5ff..c281c1d2c10d 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];
> > +                       }
> >                 }
> >         }
> >
> > --
> > 2.20.1
> >
> 
> Please add a test case for this to test/lib/test_print.c
> 
> Regards,
> Simon

Ok, I will look at it.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] display_options: print_size: Fix order overflow
  2022-09-11  9:37 [PATCH] display_options: print_size: Fix order overflow Pali Rohár
  2022-09-12 13:34 ` Simon Glass
@ 2022-09-12 19:02 ` Pali Rohár
  2022-09-14 17:10   ` Simon Glass
  2022-09-23 22:48   ` Tom Rini
  1 sibling, 2 replies; 6+ messages in thread
From: Pali Rohár @ 2022-09-12 19:02 UTC (permalink / raw)
  To: Simon Glass; +Cc: u-boot

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>

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] display_options: print_size: Fix order overflow
  2022-09-12 19:02 ` [PATCH v2] " Pali Rohár
@ 2022-09-14 17:10   ` Simon Glass
  2022-09-23 22:48   ` Tom Rini
  1 sibling, 0 replies; 6+ messages in thread
From: Simon Glass @ 2022-09-14 17:10 UTC (permalink / raw)
  To: Pali Rohár; +Cc: U-Boot Mailing List

On Mon, 12 Sept 2022 at 13:03, Pali Rohár <pali@kernel.org> wrote:
>
> 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>
>
> ---
> Changes in v2:
> * Add unit test case
> ---
>  lib/display_options.c | 6 ++++++
>  test/lib/test_print.c | 3 +++
>  2 files changed, 9 insertions(+)

Reviewed-by: Simon Glass <sjg@chromium.org>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] display_options: print_size: Fix order overflow
  2022-09-12 19:02 ` [PATCH v2] " Pali Rohár
  2022-09-14 17:10   ` Simon Glass
@ 2022-09-23 22:48   ` Tom Rini
  1 sibling, 0 replies; 6+ messages in thread
From: Tom Rini @ 2022-09-23 22:48 UTC (permalink / raw)
  To: Pali Rohár; +Cc: Simon Glass, u-boot

[-- Attachment #1: Type: text/plain, Size: 591 bytes --]

On Mon, Sep 12, 2022 at 09:02:27PM +0200, Pali Rohár wrote:

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

Applied to u-boot/next, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-09-23 22:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-11  9:37 [PATCH] display_options: print_size: Fix order overflow Pali Rohár
2022-09-12 13:34 ` Simon Glass
2022-09-12 18:56   ` Pali Rohár
2022-09-12 19:02 ` [PATCH v2] " Pali Rohár
2022-09-14 17:10   ` Simon Glass
2022-09-23 22:48   ` Tom Rini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.