Skip to contentSkip to conversation

fix print_size printing fractional gigabyte numbers on 32-bit platforms

PATCH Superseded Timur Tabi 2 participants 3 messages Mar 30, 2010

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

PATCH v2 1/1 fix print_size printing fractional gigabyte numbers on 32-bit platforms

In print_size(), the math that calculates the fractional remainder of a number used the same integer size as a physical address. However, the "10 *" factor of the algorithm means that a large number (e.g. 1.5GB) can overflow the integer if we're running on a 32-bit system. Therefore, we need to disassociate this function from the size of a physical address.

---
 include/common.h              |    2 +-
 lib_generic/display_options.c |    6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
include/common.h +1 -1
index a133e34..4e77727 100644
@@ -218,7 +218,7 @@ void	hang		(void) __attribute__ ((noreturn));
 /* */
 phys_size_t initdram (int);
 int	display_options (void);
-void	print_size (phys_size_t, const char *);
+void	print_size(unsigned long long, const char *);
 int	print_buffer (ulong addr, void* data, uint width, uint count, uint linelen);
 
 /* common/main.c */
lib_generic/display_options.c +3 -3
index 2dc2567..da17a62 100644
@@ -43,10 +43,10 @@ int display_options (void)
  * xxx GB, or xxx.y GB as needed; allow for optional trailing string
  * (like "\n")
  */
-void print_size (phys_size_t size, const char *s)
+void print_size(unsigned long long size, const char *s)
 {
-	ulong m = 0, n;
-	phys_size_t d = 1 << 30;		/* 1 GB */
+	unsigned long m = 0, n;
+	unsigned long long d = 1 << 30; 	/* 1 GB */
 	char  c = 'G';
 
 	if (size < d) {			/* try MB */
Signature · 1 line
1.6.5
#1 lore raw

PATCH v2 1/1 fix print_size printing fractional gigabyte numbers on 32-bit platforms

Quoted 17 lines · Timur Tabi

In print_size(), the math that calculates the fractional remainder of a number used the same integer size as a physical address. However, the "10 *" factor of the algorithm means that a large number (e.g. 1.5GB) can overflow the integer if we're running on a 32-bit system. Therefore, we need to disassociate this function from the size of a physical address.

---
 include/common.h              |    2 +-
 lib_generic/display_options.c |    6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
include/common.h +0 -0
index a133e34..4e77727 100644

include/common.h void hang (void) __attribute__ ((noreturn)); · line 221

Quoted 5 lines
include/common.h +1 -1
@@ -218,7 +218,7 @@ void	hang		(void) __attribute__ ((noreturn));
 /* */
 phys_size_t initdram (int);
 int	display_options (void);
-void	print_size (phys_size_t, const char *);
+void	print_size(unsigned long long, const char *);

As stated before, the overflow is an implementation detail. There's no reason to change the interface unless there's a desire to support printing sizes larger than phys_size_t, which is a distinct issue from the bugfix.

-Scott

author
#2 lore raw

PATCH v2 1/1 fix print_size printing fractional gigabyte numbers on 32-bit platforms

Quoted 4 lines · Scott Wood

As stated before, the overflow is an implementation detail. There's no reason to change the interface unless there's a desire to support printing sizes larger than phys_size_t, which is a distinct issue from the bugfix.

You are correct. I'll post a new patch. However, there's a lot of value in being able to print >4GB numbers on a 32-bit system, and it seems silly to make two patches. I'll post a new patch, and Wolfgang can decide what he wants.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale