Skip to contentSkip to conversation

malloc_simple: Remove usage of unsupported %zx format string

PATCH In review Pali Rohár 4 participants 7 messages Feb 3, 2022 – Feb 11, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 malloc_simple: Remove usage of unsupported %zx format string

Replace %zx by %lx and cast size_t to ulong.

U-Boot currently prints garbage debug output:
size=x, ptr=18, limit=18: 4002a000

With this change it prints correct debug data:
size=18, ptr=18, limit=2000: 4002a000

---
 common/malloc_simple.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/malloc_simple.c +1 -1
index 0267fb6bec87..67ee623850e0 100644
@@ -23,7 +23,7 @@ static void *alloc_simple(size_t bytes, int align)
 
 	addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
 	new_ptr = addr + bytes - gd->malloc_base;
-	log_debug("size=%zx, ptr=%lx, limit=%lx: ", bytes, new_ptr,
+	log_debug("size=%lx, ptr=%lx, limit=%lx: ", (ulong)bytes, new_ptr,
 		  gd->malloc_limit);
 	if (new_ptr > gd->malloc_limit) {
 		log_err("alloc space exhausted\n");
Signature · 1 line
2.20.1
#1 lore raw

Hi Pali,

Quoted 9 lines · Pali Rohár

Replace %zx by %lx and cast size_t to ulong.

U-Boot currently prints garbage debug output:
size=x, ptr=18, limit=18: 4002a000

With this change it prints correct debug data:
size=18, ptr=18, limit=2000: 4002a000

This qualifier is implemented in vsprintf, but not tiny-printf, and is widely used throughout the codebase. So perhaps a better fix might be

lib/tiny-printf.c +2 -1
index f661fc6505..ad25bb7383 100644
@@ -229,7 +229,8 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
                                        ch = *fmt++;
                                }
                        }
-                       if (ch == 'l') {
+                       if (ch == 'l' ||
+                           (sizeof(size_t) >= sizeof(int) && ch == 'z')) {
                                ch = *(fmt++);
                                islong = true;
                        }

--

which is not completely correct (since tiny-printf doesn't support long longs), but will address the core issue.

--Sean

#2 lore raw
Quoted 33 lines · Sean Anderson

Hi Pali,

Quoted 9 lines · Pali Rohár

Replace %zx by %lx and cast size_t to ulong.

U-Boot currently prints garbage debug output:
size=x, ptr=18, limit=18: 4002a000

With this change it prints correct debug data:
size=18, ptr=18, limit=2000: 4002a000

This qualifier is implemented in vsprintf, but not tiny-printf, and is widely used throughout the codebase. So perhaps a better fix might be

lib/tiny-printf.c +2 -2
index f661fc6505..ad25bb7383 100644
@@ -229,7 +229,8 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
                                        ch = *fmt++;
                                }
                        }
-                       if (ch == 'l') {
+                       if (ch == 'l' ||
+                           (sizeof(size_t) >= sizeof(int) && ch == 'z')) {
                                ch = *(fmt++);
                                islong = true;
                        }
--
Quoted 2 lines

which is not completely correct (since tiny-printf doesn't support long longs), but will address the core issue.

Actually, we probably need something more like

if (ch == 'z') {
	ch = *(fmt++);
	islong = sizeof(size_t) >= sizeof(int);
}

so that 32-bit arches still print the integer.

--Sean

#3 lore raw
Quoted 41 lines · Sean Anderson
Quoted 33 lines · Sean Anderson

Hi Pali,

Quoted 9 lines · Pali Rohár

Replace %zx by %lx and cast size_t to ulong.

U-Boot currently prints garbage debug output:
size=x, ptr=18, limit=18: 4002a000

With this change it prints correct debug data:
size=18, ptr=18, limit=2000: 4002a000

This qualifier is implemented in vsprintf, but not tiny-printf, and is widely used throughout the codebase. So perhaps a better fix might be

lib/tiny-printf.c +2 -2
index f661fc6505..ad25bb7383 100644
@@ -229,7 +229,8 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
                                        ch = *fmt++;
                                }
                        }
-                       if (ch == 'l') {
+                       if (ch == 'l' ||
+                           (sizeof(size_t) >= sizeof(int) && ch == 'z')) {
                                ch = *(fmt++);
                                islong = true;
                        }
--
Quoted 2 lines

which is not completely correct (since tiny-printf doesn't support long longs), but will address the core issue.

Actually, we probably need something more like

Quoted 6 lines
if (ch == 'z') {
	ch = *(fmt++);
	islong = sizeof(size_t) >= sizeof(int);
}

so that 32-bit arches still print the integer.

Right, but then we grow tiny-printf on the boards that really need to be super concerned about space. We have typically done what Pali proposes here before of make the subset of code that runs under tiny-printf use more restrictive and possibly slightly less optimal format characters.

Signature · 1 line
Tom
Reviewed-by on v1 1/1
#4 lore raw
In reply to Pali Rohár

Hi Pali,

Quoted 5 lines · Pali Rohár

Replace %zx by %lx and cast size_t to ulong.

U-Boot currently prints garbage debug output:
size=x, ptr=18, limit=18: 4002a000

Do you mean in SPL?

Quoted 9 lines

With this change it prints correct debug data:
size=18, ptr=18, limit=2000: 4002a000

---
 common/malloc_simple.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
common/malloc_simple.c +1 -2
index 0267fb6bec87..67ee623850e0 100644
@@ -23,7 +23,7 @@ static void *alloc_simple(size_t bytes, int align)
 
        addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
        new_ptr = addr + bytes - gd->malloc_base;
-       log_debug("size=%zx, ptr=%lx, limit=%lx: ", bytes, new_ptr,
+       log_debug("size=%lx, ptr=%lx, limit=%lx: ", (ulong)bytes, new_ptr,
                  gd->malloc_limit);
        if (new_ptr > gd->malloc_limit) {
                log_err("alloc space exhausted\n");
--
Quoted 2 lines

2.20.1

Regards,
Simon

#5 lore raw
Quoted 10 lines · On Thursday 03 February 2022 14:19:41 Simon Glass wrote:

Hi Pali,

Quoted 5 lines · Pali Rohár

Replace %zx by %lx and cast size_t to ulong.

U-Boot currently prints garbage debug output:
size=x, ptr=18, limit=18: 4002a000

Do you mean in SPL?

Yes, this is printed in 32-bit mvebu SPL.

Quoted 31 lines
Quoted 9 lines

With this change it prints correct debug data:
size=18, ptr=18, limit=2000: 4002a000

---
 common/malloc_simple.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines
common/malloc_simple.c +1 -2
index 0267fb6bec87..67ee623850e0 100644
@@ -23,7 +23,7 @@ static void *alloc_simple(size_t bytes, int align)
 
        addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
        new_ptr = addr + bytes - gd->malloc_base;
-       log_debug("size=%zx, ptr=%lx, limit=%lx: ", bytes, new_ptr,
+       log_debug("size=%lx, ptr=%lx, limit=%lx: ", (ulong)bytes, new_ptr,
                  gd->malloc_limit);
        if (new_ptr > gd->malloc_limit) {
                log_err("alloc space exhausted\n");
--
Quoted 2 lines

2.20.1

Regards,
Simon