tools/nolibc: add support zero pad (0) in printf
Subsystems (2)
PATCH 1/2 tools/nolibc: add support zero pad (0) in printf
From: Cheng Li <[email protected]>
This patch correctly implements the '0' flag in __nolibc_printf() to allow zero-padding for numeric and pointer outputs.
Thanks to David for pointing out the errors in the previous implementation.
The logic ensures that the sign ('-') for negative numbers or the prefix ('0x') for pointers is printed before the padding zeros, adhering to standard printf behavior (e.g., producing "-0005" instead of "000-5").
Examples of the corrected padding logic:
- ("%05d", -5) -> "-0005"
- ("%05p", ptr) -> "0x00..."
--- tools/include/nolibc/stdio.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
tools/include/nolibc/stdio.h +14 -2
@@ -267,7 +267,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char/* we're in an escape sequence, ofs == 1 */ escape =0 ; -if (c =='-' ) { +if (c =='-' || c =='0' ) { padc = c; c = fmt[ofs++]; }
@@ -364,9 +364,21 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const charif (n) { w = len < n ? len : n; n -= w; +if (padc =='0' ) { +if (outstr[0 ] =='-' ) { +if (cb (state, outstr,1 ) !=0 ) +return -1 ; + outstr++; + } +if (outstr[0 ] =='0' && outstr[1 ] =='x' ) { +if (cb (state, outstr,2 ) !=0 ) +return -1 ; + outstr +=2 ; + } + }while (width > w && padc !='-' ) { written +=1 ; -if (cb (state," " ,1 ) !=0 ) +if (cb (state, &padc,1 ) !=0 )return -1 ; width--; }
Signature · 1 line
2.52.0
PATCH 2/2 selftests/nolibc: add tests for printf zero padding (0)
From: Cheng Li <[email protected]>
This patch adds validation for the '0' flag in printf() to ensure correct behavior for pointers, positive integers, and negative integers.
The tests specifically verify that:
- Pointers include the '0x' prefix before padding.
- Negative numbers include the '-' sign before padding.
- Width and padding are correctly calculated.
--- tools/testing/selftests/nolibc/nolibc-test.c | 3 +++ 1 file changed, 3 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +3 -0
@@ -1723,6 +1723,9 @@ static int run_printf(int min, int max)CASE_TEST (truncation);EXPECT_VFPRINTF (25 ,"01234567890123456789" ,"%s" ,"0123456789012345678901234" );break ;CASE_TEST (string_width);EXPECT_VFPRINTF (10 ," 1" ,"%10s" ,"1" );break ;CASE_TEST (number_width);EXPECT_VFPRINTF (10 ," 1" ,"%10d" ,1 );break ; +CASE_TEST (zero_pad_ptr);EXPECT_VFPRINTF (5 ,"0x005" ,"%05p" , (void *)5 );break ; +CASE_TEST (zero_pad_int);EXPECT_VFPRINTF (5 ,"00005" ,"%05d" ,5 );break ; +CASE_TEST (zero_pad_neg);EXPECT_VFPRINTF (5 ,"-0005" ,"%05d" ,-5 );break ;CASE_TEST (number_left);EXPECT_VFPRINTF (10 ,"|-5 |" ,"|%-8d|" ,-5 );break ;CASE_TEST (string_align);EXPECT_VFPRINTF (10 ,"|foo |" ,"|%-8s|" ,"foo" );break ;CASE_TEST (width_trunc);EXPECT_VFPRINTF (25 ," " ,"%25d" ,1 );break ;
Signature · 1 line
2.52.0
Quoted 10 lines · On Fri, 30 Jan 2026 16:37:35 +0800 "licheng.li" <[email protected]> wrote:
From: Cheng Li <[email protected]>
This patch correctly implements the '0' flag in
__nolibc_printf()to allow zero-padding for numeric and pointer outputs.Thanks to David for pointing out the errors in the previous implementation.
The logic ensures that the sign ('-') for negative numbers or the prefix ('0x') for pointers is printed before the padding zeros, adhering to standard printf behavior (e.g., producing "-0005" instead of "000-5").
I think it would be much better to change the contents of tmpbuf[] where it is filled with the converted number.
You'd need to limit the number of zeros added (or the precision) to (say) 32 and then set 'outbuf = tmpbuf + 32' so that the zeros and sign/0x can be added at the front.
There is the (annoying) subtle difference between %#05x and %#.5x just waiting to catch the unwary.
David
Quoted 46 lines
Examples of the corrected padding logic:
- ("%05d", -5) -> "-0005"
- ("%05p", ptr) -> "0x00..."Signed-off-by Cheng Li <[email protected]>--- tools/include/nolibc/stdio.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)tools/include/nolibc/stdio.h +14
-2@@ -267,7 +267,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char/* we're in an escape sequence, ofs == 1 */ escape =0 ; -if (c =='-' ) { +if (c =='-' || c =='0' ) { padc = c; c = fmt[ofs++]; }@@ -364,9 +364,21 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const charif (n) { w = len < n ? len : n; n -= w; +if (padc =='0' ) { +if (outstr[0 ] =='-' ) { +if (cb (state, outstr,1 ) !=0 ) +return -1 ; + outstr++; + } +if (outstr[0 ] =='0' && outstr[1 ] =='x' ) { +if (cb (state, outstr,2 ) !=0 ) +return -1 ; + outstr +=2 ; + } + }while (width > w && padc !='-' ) { written +=1 ; -if (cb (state," " ,1 ) !=0 ) +if (cb (state, &padc,1 ) !=0 )return -1 ; width--; }
Quoted 15 lines · David Laight <[email protected]> 于2026年1月30日周五 18:02写道:
Quoted 10 lines · On Fri, 30 Jan 2026 16:37:35 +0800 "licheng.li" <[email protected]> wrote:
From: Cheng Li <[email protected]>
This patch correctly implements the '0' flag in
__nolibc_printf()to allow zero-padding for numeric and pointer outputs.Thanks to David for pointing out the errors in the previous implementation.
The logic ensures that the sign ('-') for negative numbers or the prefix ('0x') for pointers is printed before the padding zeros, adhering to standard printf behavior (e.g., producing "-0005" instead of "000-5").
Quoted 5 lines
I think it would be much better to change the contents of tmpbuf[] where it is filled with the converted number.
You'd need to limit the number of zeros added (or the precision) to (say) 32 and then set 'outbuf = tmpbuf + 32' so that the zeros and sign/0x can be added at the front.
Hi David,
You are absolutely right. Modifying the buffer generation logic (tmpbuf) is indeed
the robust way to handle sign/prefix insertion combined with padding/precision. My current approach of trying to handle it during the output phase is too fragile
and misses edge cases (as I also realized regarding the negative sign issue).
Given the complexity you mentioned (like the subtle differences in standard compliance)
and your plan to implement full field precision, I will drop this "Zero Padding" patch entirely.
It is better implemented as part of a proper precision support overhaul.
I will focus on the "Left Alignment" patch. I am sending out v4 of that series later,
which incorporates the code-swapping optimization you suggested earlier.
Thanks for the detailed feedback.
Best regards,
Cheng
Quoted 52 lines
There is the (annoying) subtle difference between %#05x and %#.5x just waiting to catch the unwary.
David
Quoted 46 lines
Examples of the corrected padding logic:
- ("%05d", -5) -> "-0005"
- ("%05p", ptr) -> "0x00..."Signed-off-by Cheng Li <[email protected]>--- tools/include/nolibc/stdio.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)tools/include/nolibc/stdio.h +14
-2@@ -267,7 +267,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char/* we're in an escape sequence, ofs == 1 */ escape =0 ; -if (c =='-' ) { +if (c =='-' || c =='0' ) { padc = c; c = fmt[ofs++]; }@@ -364,9 +364,21 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const charif (n) { w = len < n ? len : n; n -= w; +if (padc =='0' ) { +if (outstr[0 ] =='-' ) { +if (cb (state, outstr,1 ) !=0 ) +return -1 ; + outstr++; + } +if (outstr[0 ] =='0' && outstr[1 ] =='x' ) { +if (cb (state, outstr,2 ) !=0 ) +return -1 ; + outstr +=2 ; + } + }while (width > w && padc !='-' ) { written +=1 ; -if (cb (state," " ,1 ) !=0 ) +if (cb (state, &padc,1 ) !=0 )return -1 ; width--; }
Quoted 37 lines · On Fri, 30 Jan 2026 18:12:48 +0800 Cheng Li <[email protected]> wrote:
Quoted 15 lines · David Laight <[email protected]> 于2026年1月30日周五 18:02写道:
Quoted 10 lines · On Fri, 30 Jan 2026 16:37:35 +0800 "licheng.li" <[email protected]> wrote:
From: Cheng Li <[email protected]>
This patch correctly implements the '0' flag in
__nolibc_printf()to allow zero-padding for numeric and pointer outputs.Thanks to David for pointing out the errors in the previous implementation.
The logic ensures that the sign ('-') for negative numbers or the prefix ('0x') for pointers is printed before the padding zeros, adhering to standard printf behavior (e.g., producing "-0005" instead of "000-5").
Quoted 5 lines
I think it would be much better to change the contents of tmpbuf[] where it is filled with the converted number.
You'd need to limit the number of zeros added (or the precision) to (say) 32 and then set 'outbuf = tmpbuf + 32' so that the zeros and sign/0x can be added at the front.Hi David,
You are absolutely right. Modifying the buffer generation logic (tmpbuf) is indeed
the robust way to handle sign/prefix insertion combined with padding/precision. My current approach of trying to handle it during the output phase is too fragile
and misses edge cases (as I also realized regarding the negative sign issue).Given the complexity you mentioned (like the subtle differences in standard compliance)
and your plan to implement full field precision, I will drop this "Zero Padding" patch entirely.
It is better implemented as part of a proper precision support overhaul.
Quoted 5 lines
I will focus on the "Left Alignment" patch. I am sending out v4 of that series later,
which incorporates the code-swapping optimization you suggested earlier.Thanks for the detailed feedback.
A good plan :-)
I've written support for "%[#+- 0]*[.*][duxp]" but not tested the new bits yet.
That is at +175 bytes after I used OPTIMZER_HIDE_VAR() to stop the compiler making a 'pigs breakfast' of the code (added nearly 100 bytes for a simple if() to avoid everything when the value and precision are both zero).
David
Quoted 47 lines · David Laight <[email protected]> 于2026年1月30日周五 22:58写道:
Quoted 37 lines · On Fri, 30 Jan 2026 18:12:48 +0800 Cheng Li <[email protected]> wrote:
Quoted 15 lines · David Laight <[email protected]> 于2026年1月30日周五 18:02写道:
Quoted 10 lines · On Fri, 30 Jan 2026 16:37:35 +0800 "licheng.li" <[email protected]> wrote:
From: Cheng Li <[email protected]>
This patch correctly implements the '0' flag in
__nolibc_printf()to allow zero-padding for numeric and pointer outputs.Thanks to David for pointing out the errors in the previous implementation.
The logic ensures that the sign ('-') for negative numbers or the prefix ('0x') for pointers is printed before the padding zeros, adhering to standard printf behavior (e.g., producing "-0005" instead of "000-5").
Quoted 5 lines
I think it would be much better to change the contents of tmpbuf[] where it is filled with the converted number.
You'd need to limit the number of zeros added (or the precision) to (say) 32 and then set 'outbuf = tmpbuf + 32' so that the zeros and sign/0x can be added at the front.Hi David,
You are absolutely right. Modifying the buffer generation logic (tmpbuf) is indeed
the robust way to handle sign/prefix insertion combined with padding/precision. My current approach of trying to handle it during the output phase is too fragile
and misses edge cases (as I also realized regarding the negative sign issue).Given the complexity you mentioned (like the subtle differences in standard compliance)
and your plan to implement full field precision, I will drop this "Zero Padding" patch entirely.
It is better implemented as part of a proper precision support overhaul.Quoted 5 lines
I will focus on the "Left Alignment" patch. I am sending out v4 of that series later,
which incorporates the code-swapping optimization you suggested earlier.Thanks for the detailed feedback.
Quoted 7 lines
A good plan :-)
I've written support for "%[#+- 0]*[.*][duxp]" but not tested the new bits yet.
That is at +175 bytes after I used
OPTIMZER_HIDE_VAR()to stop the compiler making a 'pigs breakfast' of the code (added nearly 100 bytes for a simpleif()to avoid everything when the value and precision are both zero).
Hi David,
Glad to hear that! Implementing full spec support with such a small footprint (+175 bytes)
sounds impressive. I look forward to seeing your patch series.
Thanks again for the review and suggestions on the left-alignment patch.
Best regards,
Cheng
Quoted 1 line
David
Thomas Weißschuh
maintainerHey Cheng,
Jan 30, 2026 09:37:51 licheng.li <[email protected]>:
Quoted 4 lines
From: Cheng Li <[email protected]>
This patch correctly implements the '0' flag in
__nolibc_printf()to allow zero-padding for numeric and pointer outputs.
Thanks for (all of) your patches.
I am not sure when exactly I can take a proper look at them.
As we are currently fairly late in the 6.20/7.0 development cycle I would like move your patches into the next one. We can still discuss the patches and you can send new revisions and patches,
but they won't be picked up until in a few weeks.
Quoted 53 lines
Thanks to David for pointing out the errors in the previous implementation.
The logic ensures that the sign ('-') for negative numbers or the prefix ('0x') for pointers is printed before the padding zeros, adhering to standard printf behavior (e.g., producing "-0005" instead of "000-5").
Examples of the corrected padding logic:
- ("%05d", -5) -> "-0005"
- ("%05p", ptr) -> "0x00..."Signed-off-by Cheng Li <[email protected]>---
tools/include/nolibc/stdio.h| 16 ++++++++++++++--1 file changed, 14 insertions(+), 2 deletions(-)tools/include/nolibc/stdio.h +14
-3@@ -267,7 +267,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char /* we're in an escape sequence, ofs == 1 */ escape = 0; - if (c == '-') { + if (c == '-' || c == '0') { padc = c; c = fmt[ofs++]; }@@ -364,9 +364,21 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char if (n) { w = len < n ? len : n; n -= w; + if (padc == '0') { + if (outstr[0] == '-') { + if (cb(state, outstr, 1) != 0) + return -1; + outstr++; + } + if (outstr[0] == '0' && outstr[1] == 'x') { + if (cb(state, outstr, 2) != 0) + return -1; + outstr += 2; + } + } while (width > w && padc != '-') { written += 1; - if (cb(state, " ", 1) != 0) + if (cb(state, &padc, 1) != 0) return -1; width--; } --2.52.0
Quoted 6 lines · On Sat, 31 Jan 2026 11:18:49 +0100 Thomas Weißschuh <[email protected]>…
Hey Cheng,
Jan 30, 2026 09:37:51 licheng.li <[email protected]>:
Quoted 2 lines
From: Cheng Li <[email protected]>
Quoted 8 lines
Quoted 2 lines
This patch correctly implements the '0' flag in
__nolibc_printf()to allow zero-padding for numeric and pointer outputs.Thanks for (all of) your patches.
I am not sure when exactly I can take a proper look at them.
As we are currently fairly late in the 6.20/7.0 development cycle I would like move your patches into the next one. We can still discuss the patches and you can send new revisions and patches,
but they won't be picked up until in a few weeks.
Gives me time to re-write them :-)
There is still a bug in the 'align left' code as well. snprintf(buf, 21, "%-25s", "abcd") outputs 20 spaces not "abcd" followed by 16. Easiest fix is to move the truncation in the cb() function.
David
Quoted 55 lines
Quoted 54 lines
Thanks to David for pointing out the errors in the previous implementation.
The logic ensures that the sign ('-') for negative numbers or the prefix ('0x') for pointers is printed before the padding zeros, adhering to standard printf behavior (e.g., producing "-0005" instead of "000-5").
Examples of the corrected padding logic:
- ("%05d", -5) -> "-0005"
- ("%05p", ptr) -> "0x00..."Signed-off-by Cheng Li <[email protected]>---
tools/include/nolibc/stdio.h| 16 ++++++++++++++--1 file changed, 14 insertions(+), 2 deletions(-)tools/include/nolibc/stdio.h +14
-3@@ -267,7 +267,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char /* we're in an escape sequence, ofs == 1 */ escape = 0; - if (c == '-') { + if (c == '-' || c == '0') { padc = c; c = fmt[ofs++]; }@@ -364,9 +364,21 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char if (n) { w = len < n ? len : n; n -= w; + if (padc == '0') { + if (outstr[0] == '-') { + if (cb(state, outstr, 1) != 0) + return -1; + outstr++; + } + if (outstr[0] == '0' && outstr[1] == 'x') { + if (cb(state, outstr, 2) != 0) + return -1; + outstr += 2; + } + } while (width > w && padc != '-') { written += 1; - if (cb(state, " ", 1) != 0) + if (cb(state, &padc, 1) != 0) return -1; width--; } --2.52.0
Quoted 19 lines · David Laight <[email protected]> 于2026年1月31日周六 20:32写道:
Quoted 6 lines · On Sat, 31 Jan 2026 11:18:49 +0100 Thomas Weißschuh <[email protected]>…
Hey Cheng,
Jan 30, 2026 09:37:51 licheng.li <[email protected]>:
Quoted 2 lines
From: Cheng Li <[email protected]>
Quoted 8 lines
Quoted 2 lines
This patch correctly implements the '0' flag in
__nolibc_printf()to allow zero-padding for numeric and pointer outputs.Thanks for (all of) your patches.
I am not sure when exactly I can take a proper look at them.
As we are currently fairly late in the 6.20/7.0 development cycle I would like move your patches into the next one. We can still discuss the patches and you can send new revisions and patches,
but they won't be picked up until in a few weeks.
Quoted 5 lines
Gives me time to re-write them :-)
There is still a bug in the 'align left' code as well.
snprintf(buf, 21, "%-25s", "abcd")outputs 20 spaces not "abcd" followed by 16. Easiest fix is to move the truncation in thecb()function.
Hi David,
I did a double-check on the snprintf(buf, 21, "%-25s", "abcd") case with the v4 patch.
In my testing, the output content is actually correct ("abcd" followed by 16 spaces),
and the return value is 25, which complies with the standard (representing the length
if the buffer were infinite).
**However**, you are right to be concerned about the logic. Upon closer inspection,
I realized there is a potential **buffer overflow risk**.
The current logic checks len < n for the string part, but the loop handling the padding
does not rigorously check if the width exceeds the remaining buffer size n. If the buffer is full, the padding loop might continue trying to write via the callback,
potentially causing an overflow if the callback relies on the caller to enforce limits.
This confirms that your proposal to move the truncation logic entirely into the callback
is the correct architectural fix. It would eliminate the complexity of manually managingn inside the parser and prevent such overflow issues by design.
Therefore, I am perfectly happy to drop my current left-alignment patch entirely and
wait for your refactor, as it provides a much safer architecture."
Best regards,
Cheng
Quoted 58 lines
David
Quoted 55 lines
Quoted 54 lines
Thanks to David for pointing out the errors in the previous implementation.
The logic ensures that the sign ('-') for negative numbers or the prefix ('0x') for pointers is printed before the padding zeros, adhering to standard printf behavior (e.g., producing "-0005" instead of "000-5").
Examples of the corrected padding logic:
- ("%05d", -5) -> "-0005"
- ("%05p", ptr) -> "0x00..."Signed-off-by Cheng Li <[email protected]>---
tools/include/nolibc/stdio.h| 16 ++++++++++++++--1 file changed, 14 insertions(+), 2 deletions(-)tools/include/nolibc/stdio.h +14
-3@@ -267,7 +267,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char/* we're in an escape sequence, ofs == 1 */ escape =0 ; -if (c =='-' ) { +if (c =='-' || c =='0' ) { padc = c; c = fmt[ofs++]; }@@ -364,9 +364,21 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const charif (n) { w = len < n ? len : n; n -= w; +if (padc =='0' ) { +if (outstr[0 ] =='-' ) { +if (cb (state, outstr,1 ) !=0 ) +return -1 ; + outstr++; + } +if (outstr[0 ] =='0' && outstr[1 ] =='x' ) { +if (cb (state, outstr,2 ) !=0 ) +return -1 ; + outstr +=2 ; + } + }while (width > w && padc !='-' ) { written +=1 ; -if (cb (state," " ,1 ) !=0 ) +if (cb (state, &padc,1 ) !=0 )return -1 ; width--; } --2.52.0
Quoted 28 lines · On Sun, 1 Feb 2026 08:49:37 +0800 Cheng Li <[email protected]> wrote:
Quoted 19 lines · David Laight <[email protected]> 于2026年1月31日周六 20:32写道:
Quoted 6 lines · On Sat, 31 Jan 2026 11:18:49 +0100 Thomas Weißschuh <[email protected]>…
Hey Cheng,
Jan 30, 2026 09:37:51 licheng.li <[email protected]>:
Quoted 2 lines
From: Cheng Li <[email protected]>
Quoted 8 lines
Quoted 2 lines
This patch correctly implements the '0' flag in
__nolibc_printf()to allow zero-padding for numeric and pointer outputs.Thanks for (all of) your patches.
I am not sure when exactly I can take a proper look at them.
As we are currently fairly late in the 6.20/7.0 development cycle I would like move your patches into the next one. We can still discuss the patches and you can send new revisions and patches,
but they won't be picked up until in a few weeks.Quoted 5 lines
Gives me time to re-write them :-)
There is still a bug in the 'align left' code as well.
snprintf(buf, 21, "%-25s", "abcd")outputs 20 spaces not "abcd" followed by 16. Easiest fix is to move the truncation in thecb()function.Hi David,
Quoted 8 lines
I did a double-check on the
snprintf(buf, 21, "%-25s", "abcd")case with the v4 patch.In my testing, the output content is actually correct ("abcd" followed by 16 spaces),
and the return value is 25, which complies with the standard (representing the length
if the buffer were infinite).
I was probably checking a slightly different version where the '-' just caused the truncated "abcd" be output before the pad.
Quoted 5 lines
**However**, you are right to be concerned about the logic. Upon closer inspection,
I realized there is a potential **buffer overflow risk**.
...
Quoted 4 lines
Therefore, I am perfectly happy to drop my current left-alignment patch entirely and
wait for your refactor, as it provides a much safer architecture."
I've got it 'mostly rewritten', I'll remove your patches for "%-2s" from the front before I submit them.
I'll put that change in early.
David