Enhance printf()
Subsystems (2)
v3 cover Enhance printf()
From: David Laight <[email protected]>
Update printf() so that it handles almost all the non-fp formats. In particular:
- Left alignment.
- Zero padding.
- Alternate form "%#x" and "%#o".
- Field precision.
- Variable field width and precision.
- Width modifiers q, L, t and z.
- Conversion specifiers i, o and X (X generates lower case). About the only things that are missing are wide chanacters and floating point.
The tests are updated to match.
Bloat/savings (in nolibc-test, but excluding the program) to patch 12:
Function old new delta
_nolibc_u64toa_base.isra - 143 +143
strerror - 78 +78
__nolibc_sprintf_cb 58 91 +33
itoa_r.isra 60 75 +15
utoa_r.isra 144 - -144
__nolibc_printf 1081 729 -352
(All these functions include ~40 bytes for the stack protector code.) utoa_r.isra and _nolibc_u64toa_base.isra pretty much cancel each other out. itoa_r.isra grows slightly since it calls _nolibc_u64toa_base(). strerror() used to be inlined, but over half of it is the stack check. While some of the code added to __nolibc_sprintf_cb() has come out of __nolibc_printf() 16-20 bytes is removed from the caller.
So there is a net saving of about 280 bytes (including losing a copy of the number to ascii code).
The later patches add code back in:
patch 13 - conversion flags " +#" +80 bytes
patch 14 - left aligning fields +38 bytes
patch 15 - zero padding and field precision +260 bytes
patch 16 - octal output +34 bytes
So probably about +130 bytes, but it will depend on what the application actually calls and inlining decisions made by the compiler. (All x86-64, other architectures will vary.)
The biggest size change is probably removing the .data from strerror(). This reduced the program binary file by 4k if it is the only initialised data in a small program.
Significant changes for v3:
The patches have been re-ordered, split and joined but the final code is pretty much the same as v2.
- Include the patch to stdlib.h that optimises the 'number to ascii' functions. This is needed for the final patch that adds octal support. This includes a fix to the previous version that could generate negative digits.
- Add octal support in the final patch.
- Update the selftests as new features are added.
- The patch to buffer fprintf() output has been removed.
Changes for v2:
Mostly changes to improve the readability of the code.
- New patch #1 inserted to rename the variable 'c' to 'ch'.
- Use #define 'magic' for the bit-masks that check multiple characters. The check for the conversion flag characters is then based on:
ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, ' ', '#', '+', '-', '0');
- Re-order the changes so that the old patch 10 (Use bit-pattern for integral formats) is done at the same time as bit-masks are used for the flags characters and length modifiers.
This means the restructuring changes are done before new features are added.
- Put all the changes to the selftest together at the end. There is one extra test for ("%#01x", 0x1234) (should be "0x1234") which is problematic because once you've removed the length of the "0x" from the field width there are -1 character postions for the digits.
David Laight (17):
tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h tools/nolibc: Optimise and common up the number to ascii functions selftests/nolibc: Fix build with host headers and libc selftests/nolibc: Improve reporting of vfprintf() errors tools/nolibc: Implement strerror() in terms of strerror_r() tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to
'outbuf[]'
tools/nolibc/printf: Move snprintf length check to callback tools/nolibc/printf: Output pad characters in 16 byte chunks tools/nolibc/printf: Simplify __nolibc_printf() tools/nolibc/printf: Use goto and reduce indentation tools/nolibc/printf: Use bit-masks to hold requested flag, length and
conversion chars
tools/nolibc/printf: Handle "%s" with the numeric formats tools/nolibc/printf: Add support for conversion flags " +#" tools/nolibc/printf: Add support for left aligning fields tools/nolibc/printf: Add support for zero padding and field precision tools/nolibc/printf: Add support for octal output selftests/nolibc: Use printf variable field widths and precisions
tools/include/nolibc/compiler.h | 5 + tools/include/nolibc/stdio.h | 502 ++++++++++++++----- tools/include/nolibc/stdlib.h | 169 +++---- tools/testing/selftests/nolibc/nolibc-test.c | 179 ++++--- 4 files changed, 575 insertions(+), 280 deletions(-)
Signature · 1 line
2.39.5
PATCH v3 1/17 tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h
From: David Laight <[email protected]>
Needed to stop compiler 'optimisations' bloating code. Equivalent to the definition in include/linux/compiler.h
---
New patch for v3.
tools/include/nolibc/compiler.h | 5 +++++ 1 file changed, 5 insertions(+)
tools/include/nolibc/compiler.h +5 -0
@@ -71,4 +71,9 @@# define __nolibc_static_assert (_t)#endif +#ifndef _NOLIBC_OPTIMIZER_HIDE_VAR +/* Make the optimizer believe the variable can be manipulated arbitrarily. */ +#define _NOLIBC_OPTIMIZER_HIDE_VAR (var) __asm__ ("" : "+r" (var)) +#endif +#endif /* _NOLIBC_COMPILER_H */
Signature · 1 line
2.39.5
Thomas Weißschuh
maintainerRe: [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h
Quoted 18 lines · On 2026-02-23 10:17:19+0000, [email protected] wrote:
From: David Laight <[email protected]>
Needed to stop compiler 'optimisations' bloating code. Equivalent to the definition in
include/linux/compiler.hSigned-off-by David Laight <[email protected]>---
New patch for v3.
tools/include/nolibc/compiler.h | 5 +++++ 1 file changed, 5 insertions(+)tools/include/nolibc/compiler.h +0
-0
tools/include/nolibc/compiler.h line 74
Quoted 4 lines
tools/include/nolibc/compiler.h +1
-0@@ -71,4 +71,9 @@# define __nolibc_static_assert (_t)#endif +#ifndef _NOLIBC_OPTIMIZER_HIDE_VAR
The ifndef guard should not be necessary.
Quoted 8 lines
tools/include/nolibc/compiler.h +4
-0+/* Make the optimizer believe the variable can be manipulated arbitrarily. */ +#define _NOLIBC_OPTIMIZER_HIDE_VAR (var) __asm__ ("" : "+r" (var)) +#endif +#endif /* _NOLIBC_COMPILER_H */ Signature · 1 line
2.39.5
Re: [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h
Quoted 22 lines · On Wed, 25 Feb 2026 22:25:12 +0100 Thomas Weißschuh <[email protected]>…
Quoted 21 lines · On 2026-02-23 10:17:19+0000, [email protected] wrote:
From: David Laight <[email protected]>
Needed to stop compiler 'optimisations' bloating code. Equivalent to the definition in
include/linux/compiler.hSigned-off-by David Laight <[email protected]>---
New patch for v3.
tools/include/nolibc/compiler.h | 5 +++++ 1 file changed, 5 insertions(+)tools/include/nolibc/compiler.h +0
-0@@ -71,4 +71,9 @@# define __nolibc_static_assert (_t)#endif
Quoted 3 lines
tools/include/nolibc/compiler.h line 74
Quoted 1 line
tools/include/nolibc/compiler.h +1
-0+#ifndef _NOLIBC_OPTIMIZER_HIDE_VARThe ifndef guard should not be necessary.
I copied the one from include/linux/compiler.h
Nothing seems to need that one either.
Can you take it out?
David
Quoted 9 lines
Quoted 9 lines
tools/include/nolibc/compiler.h +4
-0+/* Make the optimizer believe the variable can be manipulated arbitrarily. */ +#define _NOLIBC_OPTIMIZER_HIDE_VAR (var) __asm__ ("" : "+r" (var)) +#endif +#endif /* _NOLIBC_COMPILER_H */ Signature · 1 line
2.39.5
Thomas Weißschuh
maintainerRe: [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h
Quoted 25 lines · David Laight
Quoted 22 lines · On Wed, 25 Feb 2026 22:25:12 +0100 Thomas Weißschuh <[email protected]>…
Quoted 21 lines · On 2026-02-23 10:17:19+0000, [email protected] wrote:
From: David Laight <[email protected]>
Needed to stop compiler 'optimisations' bloating code. Equivalent to the definition in
include/linux/compiler.hSigned-off-by David Laight <[email protected]>---
New patch for v3.
tools/include/nolibc/compiler.h | 5 +++++ 1 file changed, 5 insertions(+)tools/include/nolibc/compiler.h +0
-0@@ -71,4 +71,9 @@# define __nolibc_static_assert (_t)#endif
Quoted 6 lines
Quoted 3 lines
tools/include/nolibc/compiler.h line 74
Quoted 1 line
tools/include/nolibc/compiler.h +1
-0+#ifndef _NOLIBC_OPTIMIZER_HIDE_VARThe ifndef guard should not be necessary.
I copied the one from
include/linux/compiler.h
Nothing seems to need that one either.
Yeah, someone should probably clean that up, too.
Quoted 1 line
Can you take it out?
Sure, I can do that when applying the patch.
But a new revision will be necessary anyways.
Quoted 10 lines
Quoted 9 lines
Quoted 9 lines
tools/include/nolibc/compiler.h +4
-0+/* Make the optimizer believe the variable can be manipulated arbitrarily. */ +#define _NOLIBC_OPTIMIZER_HIDE_VAR (var) __asm__ ("" : "+r" (var)) +#endif +#endif /* _NOLIBC_COMPILER_H */ Signature · 1 line
2.39.5
PATCH v3 2/17 tools/nolibc: Optimise and common up the number to ascii functions
In reply to [email protected]From: David Laight <[email protected]>
Implement u[64]to[ah]_r() using a common function that uses multiply by reciprocal to generate the least significant digit first and then reverses the string.
On 32bit this is five multiplies (with 64bit product) for each output digit. I think the old utoa_r() always did 36 multiplies and a lot of subtracts - so this is likely faster even for 32bit values. Definitely better for 64bit values (especially small ones).
Clearly shifts are faster for base 16, but reversing the output buffer makes a big difference.
Sharing the code reduces the footprint (unless gcc decides to constant fold the functions).
Definitely helps vfprintf() where the constants get loaded and a single call is done.
Also makes it cheap to add octal support to vfprintf for completeness.
---
This was previously a separate patch.
Moved here since a major user in the printf code and the printf tests test this code.
v3: Revert to the code from v1. But with a simple test not a loop.
tools/include/nolibc/stdlib.h | 169 +++++++++++++++++----------------- 1 file changed, 82 insertions(+), 87 deletions(-)
tools/include/nolibc/stdlib.h +82 -87
@@ -188,36 +188,91 @@ void *realloc(void *old_ptr, size_t new_size)return ret; } -/* Converts the unsigned long integer <in> to its hex representation into +/* Converts the unsigned 64bit integer <in> to base <base> ascii into * buffer <buffer>, which must be long enough to store the number and the -* trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The -* buffer is filled from the first byte, and the number of characters emitted -* (not counting the trailing zero) is returned. The function is constructed -* in a way to optimize the code size and avoid any divide that could add a -* dependency on large external functions. +* trailing zero. The buffer is filled from the first byte, and the number +* of characters emitted (not counting the trailing zero) is returned. +* The function uses 'multiply by reciprocal' for the divisions and +* requires the caller pass the correct reciprocal. +* +* Note that unlike __div64_const32() in asm-generic/div64.h there isn't +* an extra shift done (by ___p), the reciprocal has to be lower resulting +* in a slightly low quotient. +* Keep things simple by correcting for the error. +* This also saves calculating the 'low * low' product (e2 below) which is +* very unlikely to be significant. +* +* Some maths: +* recip = p2 / base - e1; // With e1 < base. +* q = (recip * in - e2) / p2; // With e2 < p2. +* = base / in - (e1 * in + e2) / p2; +* > base / in - (e1 * p2 + p2) / p2; +* = base / in - ((e1 + 1) * p2) / p2; +* > base / in - base; +* So the maximum error is less than 'base'. +* Hence the largest possible digit is '2 * base - 1'. +* For base 10 e1 is 6 and you can get digits of 15 (eg from 2**64-1). +* Error e1 is largest for a base that is a factor of 2**64+1, the smallest is 274177 +* and converting 2**42-1 in base 274177 does generate a digit of 274177+274175. +* This all means only a single correction is needed rather than a loop. +* +* __int128 isn't used for mips because gcc prior to 10.0 will call +* __multi3 for MIPS64r6. */ -static __attribute__((unused)) -int utoh_r (unsigned long in,char *buffer) +#define _NOLIBC_U64TOA_RECIP (base) ((base) & 1 ? ~0ull / (base) : (1ull << 63) / ((base) / 2)) +static __attribute__((unused, noinline)) +int _nolibc_u64toa_base (uint64_t in,char *buffer,unsigned int base,uint64_t recip) { -signed char pos = (~0UL >0xfffffffful ) ?60 :28 ; -int digits =0 ; -int dig; +unsigned int digits =0 ; +unsigned int dig; +uint64_t q; +char *p; +/* Generate least significant digit first */ do { - dig = in >> pos; - in -= (uint64_t )dig << pos; - pos -=4 ; -if (dig || digits || pos <0 ) { -if (dig >9 ) - dig +='a' -'0' -10 ; - buffer[digits++] ='0' + dig; +#if defined(__SIZEOF_INT128__) && !defined(__mips__) + q = ((unsigned __int128 )in * recip) >>64 ; +#else +uint64_t p = (uint32_t )in * (recip >>32 ); + q = (in >>32 ) * (recip >>32 ) + (p >>32 ); + p = (uint32_t )p + (in >>32 ) * (uint32_t )recip; + q += p >>32 ; +#endif + dig = in - q * base; +/* Correct for any rounding errors */ +if (dig >= base) { + dig -= base; + q++; } - }while (pos >=0 ); +if (dig >9 ) + dig +='a' -'0' -10 ; + buffer[digits++] ='0' + dig; + }while ((in = q)); buffer[digits] =0 ; + +/* Order reverse to result */ +for (p = buffer + digits -1 ; p > buffer; buffer++, p--) { + dig = *buffer; + *buffer = *p; + *p = dig; + } +return digits; } +/* Converts the unsigned long integer <in> to its hex representation into +* buffer <buffer>, which must be long enough to store the number and the +* trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The +* buffer is filled from the first byte, and the number of characters emitted +* (not counting the trailing zero) is returned. +*/ +static __inline__ __attribute__((unused)) +int utoh_r (unsigned long in,char *buffer) +{ +return _nolibc_u64toa_base (in, buffer,16 ,_NOLIBC_U64TOA_RECIP (16 )); +} +/* converts unsigned long <in> to an hex string using the static itoa_buffer * and returns the pointer to that string. */
@@ -233,30 +288,11 @@ char *utoh(unsigned long in)* trailing zero (21 bytes for 18446744073709551615 in 64-bit, 11 for * 4294967295 in 32-bit). The buffer is filled from the first byte, and the * number of characters emitted (not counting the trailing zero) is returned. -* The function is constructed in a way to optimize the code size and avoid -* any divide that could add a dependency on large external functions. */ -static __attribute__((unused)) +static __inline__ __attribute__((unused))int utoa_r (unsigned long in,char *buffer) { -unsigned long lim; -int digits =0 ; -int pos = (~0UL >0xfffffffful ) ?19 :9 ; -int dig; - -do { -for (dig =0 , lim =1 ; dig < pos; dig++) - lim *=10 ; - -if (digits || in >= lim || !pos) { -for (dig =0 ; in >= lim; dig++) - in -= lim; - buffer[digits++] ='0' + dig; - } - }while (pos--); - - buffer[digits] =0 ; -return digits; +return _nolibc_u64toa_base (in, buffer,10 ,_NOLIBC_U64TOA_RECIP (10 )); }/* Converts the signed long integer <in> to its string representation into
@@ -324,34 +360,12 @@ char *utoa(unsigned long in)* buffer <buffer>, which must be long enough to store the number and the * trailing zero (17 bytes for "ffffffffffffffff"). The buffer is filled from * the first byte, and the number of characters emitted (not counting the -* trailing zero) is returned. The function is constructed in a way to optimize -* the code size and avoid any divide that could add a dependency on large -* external functions. +* trailing zero) is returned. */ -static __attribute__((unused)) +static __inline__ __attribute__((unused))int u64toh_r (uint64_t in,char *buffer) { -signed char pos =60 ; -int digits =0 ; -int dig; - -do { -if (sizeof (long ) >=8 ) { - dig = (in >> pos) &0xF ; - }else { -/* 32-bit platforms: avoid a 64-bit shift */ -uint32_t d = (pos >=32 ) ? (in >>32 ) : in; - dig = (d >> (pos &31 )) &0xF ; - } -if (dig >9 ) - dig +='a' -'0' -10 ; - pos -=4 ; -if (dig || digits || pos <0 ) - buffer[digits++] ='0' + dig; - }while (pos >=0 ); - - buffer[digits] =0 ; -return digits; +return _nolibc_u64toa_base (in, buffer,16 ,_NOLIBC_U64TOA_RECIP (16 )); }/* converts uint64_t <in> to an hex string using the static itoa_buffer and
@@ -368,31 +382,12 @@ char *u64toh(uint64_t in)* buffer <buffer>, which must be long enough to store the number and the * trailing zero (21 bytes for 18446744073709551615). The buffer is filled from * the first byte, and the number of characters emitted (not counting the -* trailing zero) is returned. The function is constructed in a way to optimize -* the code size and avoid any divide that could add a dependency on large -* external functions. +* trailing zero) is returned. */ -static __attribute__((unused)) +static __inline__ __attribute__((unused))int u64toa_r (uint64_t in,char *buffer) { -unsigned long long lim; -int digits =0 ; -int pos =19 ;/* start with the highest possible digit */ -int dig; - -do { -for (dig =0 , lim =1 ; dig < pos; dig++) - lim *=10 ; - -if (digits || in >= lim || !pos) { -for (dig =0 ; in >= lim; dig++) - in -= lim; - buffer[digits++] ='0' + dig; - } - }while (pos--); - - buffer[digits] =0 ; -return digits; +return _nolibc_u64toa_base (in, buffer,10 ,_NOLIBC_U64TOA_RECIP (10 )); }/* Converts the signed 64-bit integer <in> to its string representation into
Signature · 1 line
2.39.5
Re: [PATCH v3 next 02/17] tools/nolibc: Optimise and common up the number to ascii functions
Quoted 14 lines · On 2026-02-23 10:17:20+0000, [email protected] wrote:
From: David Laight <[email protected]>
Implement u[64]to[ah]_r() using a common function that uses multiply by reciprocal to generate the least significant digit first and then reverses the string.
On 32bit this is five multiplies (with 64bit product) for each output digit. I think the old
utoa_r()always did 36 multiplies and a lot of subtracts - so this is likely faster even for 32bit values. Definitely better for 64bit values (especially small ones).Clearly shifts are faster for base 16, but reversing the output buffer makes a big difference.
Quoted 7 lines
Sharing the code reduces the footprint (unless gcc decides to constant fold the functions).
Definitely helpsvfprintf()where the constants get loaded and a single call is done.
Also makes it cheap to add octal support to vfprintf for completeness.Signed-off-by David Laight <[email protected]>
Applied, thanks.
Some changes I did:
* Do not use int128 for sparc on clang, as it will also use __multi3.
* Apply Willy's Ack from v1.
(...)
Re: [PATCH v3 next 02/17] tools/nolibc: Optimise and common up the number to ascii functions
Quoted 21 lines · On Wed, 25 Feb 2026 22:40:29 +0100 Thomas Weißschuh <[email protected]>…
Quoted 20 lines · On 2026-02-23 10:17:20+0000, [email protected] wrote:
From: David Laight <[email protected]>
Implement u[64]to[ah]_r() using a common function that uses multiply by reciprocal to generate the least significant digit first and then reverses the string.
On 32bit this is five multiplies (with 64bit product) for each output digit. I think the old
utoa_r()always did 36 multiplies and a lot of subtracts - so this is likely faster even for 32bit values. Definitely better for 64bit values (especially small ones).Clearly shifts are faster for base 16, but reversing the output buffer makes a big difference.
Sharing the code reduces the footprint (unless gcc decides to constant fold the functions).
Definitely helpsvfprintf()where the constants get loaded and a single call is done.
Also makes it cheap to add octal support to vfprintf for completeness.
Quoted 7 lines
Quoted 1 line
Signed-off-by David Laight <[email protected]>Applied, thanks.
Some changes I did:
* Do not use int128 for sparc on clang, as it will also use__multi3.
* Apply Willy's Ack from v1.
Thanks.
I thought I'd gone through looking for Acks - must have missed that one.
David
Quoted 2 lines
(...)
PATCH v3 3/17 selftests/nolibc: Fix build with host headers and libc
In reply to [email protected]From: David Laight <[email protected]>
Many systems don't have strlcpy() or strlcat() and readdir_r() is deprecated. This makes the tests fail to build with the host headers.
Disable the 'directories' test and define strlcpy(), strlcat() and readdir_r() using #defines so that the code compiles.
---
For v3 (no v2):
- Now part of the patch series that enhances _nolibc_printf().
- Disable the test that uses readdir_r() and #defines readdir_r() instead of disabling compiler warnings.
- No need to change is_nolibc, doesn't help.
Basically the nolibc-test program makefiles don't work without this change.
tools/testing/selftests/nolibc/nolibc-test.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
tools/testing/selftests/nolibc/nolibc-test.c +15 -1
@@ -74,6 +74,20 @@ static const int is_nolibc =#endif ; +#if !defined(NOLIBC ) +/* Some disabled tests may not compile. */ + +/* strlcat() and strlcpy() may not be in the system headers. */ +#undef strlcat +#undef strlcpy +#define strlcat (d, s, l) 0 +#define strlcpy (d, s, l) 0 + +/* readdir_r() is likely to be marked deprecated */ +#undef readdir_r +#define readdir_r (dir, dirent, result) ((errno = EINVAL), -1) +#endif +/* definition of a series of tests */ struct test {const char *name;/* test name */
@@ -1408,7 +1422,7 @@ int run_syscall(int min, int max)CASE_TEST (fork);EXPECT_SYSZR (1 ,test_fork (FORK_STANDARD ));break ;CASE_TEST (getdents64_root);EXPECT_SYSNE (1 ,test_getdents64 ("/" ),-1 );break ;CASE_TEST (getdents64_null);EXPECT_SYSER (1 ,test_getdents64 ("/dev/null" ),-1 ,ENOTDIR );break ; -CASE_TEST (directories);EXPECT_SYSZR (proc,test_dirent ());break ; +CASE_TEST (directories);EXPECT_SYSZR (is_nolibc && proc,test_dirent ());break ;CASE_TEST (getrandom);EXPECT_SYSZR (1 ,test_getrandom ());break ;CASE_TEST (gettimeofday_tv);EXPECT_SYSZR (1 ,gettimeofday (&tv,NULL ));break ;CASE_TEST (gettimeofday_tv_tz);EXPECT_SYSZR (1 ,gettimeofday (&tv, &tz));break ;
Signature · 1 line
2.39.5
Re: [PATCH v3 next 03/17] selftests/nolibc: Fix build with host headers and libc
Quoted 9 lines · On 2026-02-23 10:17:21+0000, [email protected] wrote:
From: David Laight <[email protected]>
Many systems don't have
strlcpy()orstrlcat()andreaddir_r()is deprecated. This makes the tests fail to build with the host headers.
Disable the 'directories' test and definestrlcpy(),strlcat()andreaddir_r()using #defines so that the code compiles.Fixes 6fe8360b16acb ("selftests/nolibc: also test libc-test through regular selftest framework")Signed-off-by David Laight <[email protected]>
Applied, thanks.
(...)
PATCH v3 4/17 selftests/nolibc: Improve reporting of vfprintf() errors
In reply to [email protected]From: David Laight <[email protected]>
Check the string matches before checking the returned length. Only print the string once when it matches.
Allow for skipping tests that check non-standard behaviour.
Since the 'return value' from snprintf() is really the output buffer there is no reason for the test cases to specify the numeric return value (the length of the extected output) as well as the expected output. The expected output needs to be the 'untruncated' output for tests that generate output that gets truncatedb because expect_vfprintf() uses a short buffer.
This puts all the knowledge of any truncation to expect_vfprintf(). It also easier to change the maximum size, and saves counting the length of the expected string by hand when adding tests.
Increase the size limit from 20 to 25 characters, changing the tests to match. This is needed to test octal conversions later on.
Append a '+' to the printed output (after the final ") when the output is truncated.
Additionally check that nothing beyond the end is written. The "width_trunc" test (#14) now fails because e90ce42e81381 ("tools/nolibc: implement width padding in printf()") doesn't correctly update the space in the buffer when adding pad characters. This will be addressed in a later patch.
Also correctly return 1 (the number of errors) when strcmp() fails rather than the return value from strncmp() which is the signed difference between the mismatching characters.
---
For v3:
- Patch 9 in v2, patch 5 in v1..
- Increase the size limit to 25 in preparation for testing octal.
- Change the way truncated fprintf() are handled.
- Allow for tests being skipped.
- Use a fixed value (0xa5) for the canary when detecting overwrites.
tools/testing/selftests/nolibc/nolibc-test.c | 91 ++++++++++++++------ 1 file changed, 64 insertions(+), 27 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +64 -27
@@ -1660,33 +1660,70 @@ int run_stdlib(int min, int max)return ret; } -#define EXPECT_VFPRINTF (c, expected, fmt, ...) \ - ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) +#define EXPECT_VFPRINTF (cond, expected, fmt, ...) \ + ret += expect_vfprintf(llen, cond, expected, fmt, ##__VA_ARGS__) -static int expect_vfprintf (int llen,int c,const char *expected,const char *fmt, ...) +#define VFPRINTF_LEN 25 + +static int expect_vfprintf (int llen,int cond,const char *expected,const char *fmt, ...) { -char buf[100 ]; +ssize_t written, expected_len; +char buf[VFPRINTF_LEN +80 ]; +unsigned int cmp_len;va_list args; -ssize_t w; -int ret; +if (!cond) { +result (llen,SKIPPED ); +return 0 ; + } + +/* Fill and terminate buffer to check for overlong/absent writes */ +memset (buf,0xa5 ,sizeof (buf) -1 ); + buf[sizeof (buf) -1 ] =0 ;va_start (args, fmt); -/* Only allow writing 21 bytes, to test truncation */ - w =vsnprintf (buf,21 , fmt, args); +/* Limit the buffer length to test truncation */ + written =vsnprintf (buf,VFPRINTF_LEN +1 , fmt, args);va_end (args); -if (w != c) { - llen +=printf (" written(%d) != %d" , (int )w, c); + llen +=printf (" \"%s\"" , buf); + +/* If the expected output is long it will have been truncated. */ + expected_len =strlen (expected); +if (expected_len >VFPRINTF_LEN ) { +/* Indicate truncated in test output */ + llen +=printf ("+" ); + cmp_len =VFPRINTF_LEN ; + }else { + cmp_len = expected_len; + } + +if (memcmp (expected, buf, cmp_len) || buf[cmp_len]) { +/* Copy and truncate until "%.*s" supported */ +memcpy (buf, expected, cmp_len); + buf[cmp_len] =0 ; + llen +=printf (" should be \"%s\"" , buf);result (llen,FAIL );return 1 ; } - llen +=printf (" \"%s\" = \"%s\"" , expected, buf); - ret =strncmp (expected, buf, c); +if (written != expected_len) { + llen +=printf (" written(%d) != %d" , (int )written, (int )expected_len); +result (llen,FAIL ); +return 1 ; + } -result (llen, ret ?FAIL :OK ); -return ret; +/* Check for any overwrites after the actual data. */ +while (++cmp_len <sizeof (buf) -1 ) { +if ((unsigned char )buf[cmp_len] !=0xa5 ) { + llen +=printf (" overwrote buf[%d] with 0x%x" , cmp_len, buf[cmp_len]); +result (llen,FAIL ); +return 1 ; + } + } + +result (llen,OK ); +return 0 ; }static int test_scanf (void )
@@ -1807,21 +1844,21 @@ static int run_printf(int min, int max)* test numbers. */ switch (test + __LINE__ +1 ) { -CASE_TEST (empty);EXPECT_VFPRINTF (0 ,"" ,"" );break ; -CASE_TEST (simple);EXPECT_VFPRINTF (3 ,"foo" ,"foo" );break ; -CASE_TEST (string);EXPECT_VFPRINTF (3 ,"foo" ,"%s" ,"foo" );break ; -CASE_TEST (number);EXPECT_VFPRINTF (4 ,"1234" ,"%d" ,1234 );break ; -CASE_TEST (negnumber);EXPECT_VFPRINTF (5 ,"-1234" ,"%d" ,-1234 );break ; -CASE_TEST (unsigned);EXPECT_VFPRINTF (5 ,"12345" ,"%u" ,12345 );break ; +CASE_TEST (empty);EXPECT_VFPRINTF (1 ,"" ,"" );break ; +CASE_TEST (simple);EXPECT_VFPRINTF (1 ,"foo" ,"foo" );break ; +CASE_TEST (string);EXPECT_VFPRINTF (1 ,"foo" ,"%s" ,"foo" );break ; +CASE_TEST (number);EXPECT_VFPRINTF (1 ,"1234" ,"%d" ,1234 );break ; +CASE_TEST (negnumber);EXPECT_VFPRINTF (1 ,"-1234" ,"%d" ,-1234 );break ; +CASE_TEST (unsigned);EXPECT_VFPRINTF (1 ,"12345" ,"%u" ,12345 );break ;CASE_TEST (char);EXPECT_VFPRINTF (1 ,"c" ,"%c" ,'c' );break ;CASE_TEST (hex);EXPECT_VFPRINTF (1 ,"f" ,"%x" ,0xf );break ; -CASE_TEST (pointer);EXPECT_VFPRINTF (3 ,"0x1" ,"%p" , (void *)0x1 );break ; -CASE_TEST (uintmax_t);EXPECT_VFPRINTF (20 ,"18446744073709551615" ,"%ju" ,0xffffffffffffffffULL );break ; -CASE_TEST (intmax_t);EXPECT_VFPRINTF (20 ,"-9223372036854775807" ,"%jd" ,0x8000000000000001LL );break ; -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 (width_trunc);EXPECT_VFPRINTF (25 ," " ,"%25d" ,1 );break ; +CASE_TEST (pointer);EXPECT_VFPRINTF (1 ,"0x1" ,"%p" , (void *)0x1 );break ; +CASE_TEST (uintmax_t);EXPECT_VFPRINTF (1 ,"18446744073709551615" ,"%ju" ,0xffffffffffffffffULL );break ; +CASE_TEST (intmax_t);EXPECT_VFPRINTF (1 ,"-9223372036854775807" ,"%jd" ,0x8000000000000001LL );break ; +CASE_TEST (truncation);EXPECT_VFPRINTF (1 ,"012345678901234567890123456789" ,"%s" ,"012345678901234567890123456789" );break ; +CASE_TEST (string_width);EXPECT_VFPRINTF (1 ," 1" ,"%10s" ,"1" );break ; +CASE_TEST (number_width);EXPECT_VFPRINTF (1 ," 1" ,"%10d" ,1 );break ; +CASE_TEST (width_trunc);EXPECT_VFPRINTF (1 ," 1" ,"%30d" ,1 );break ;CASE_TEST (scanf);EXPECT_ZR (1 ,test_scanf ());break ;CASE_TEST (strerror);EXPECT_ZR (1 ,test_strerror ());break ;CASE_TEST (printf_error);EXPECT_ZR (1 ,test_printf_error ());break ;
Signature · 1 line
2.39.5
Thomas Weißschuh
maintainerRe: [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors
Quoted 16 lines · On 2026-02-23 10:17:22+0000, [email protected] wrote:
From: David Laight <[email protected]>
Check the string matches before checking the returned length. Only print the string once when it matches.
Allow for skipping tests that check non-standard behaviour.Since the 'return value' from
snprintf()is really the output buffer there is no reason for the test cases to specify the numeric return value (the length of the extected output) as well as the expected output. The expected output needs to be the 'untruncated' output for tests that generate output that gets truncatedb becauseexpect_vfprintf()uses a short buffer.
This puts all the knowledge of any truncation toexpect_vfprintf(). It also easier to change the maximum size, and saves counting the length of the expected string by hand when adding tests.
Quoted 2 lines
Increase the size limit from 20 to 25 characters, changing the tests to match. This is needed to test octal conversions later on.
Then please do this right before the addition of the octal conversion.
Quoted 8 lines
Append a '+' to the printed output (after the final ") when the output is truncated.
Additionally check that nothing beyond the end is written. The "
width_trunc" test (#14) now fails becausee90ce42e81381("tools/nolibc: implement width padding inprintf()") doesn't correctly update the space in the buffer when adding pad characters. This will be addressed in a later patch.
The build bots will yell at us for this. Instead mark the test as skipped until it is fixed.
Quoted 3 lines
Also correctly return 1 (the number of errors) when
strcmp()fails rather than the return value fromstrncmp()which is the signed difference between the mismatching characters.
Please split these different steps into dedicated commits. Yes, the testcases are going to be rewritten a bunch of times, but that does not matter.
Quoted 18 lines
Signed-off-by David Laight <[email protected]>---
For v3:
- Patch 9 in v2, patch 5 in v1..
- Increase the size limit to 25 in preparation for testing octal.
- Change the way truncatedfprintf()are handled.
- Allow for tests being skipped.
- Use a fixed value (0xa5) for the canary when detecting overwrites.tools/testing/selftests/nolibc/nolibc-test.c | 91 ++++++++++++++------ 1 file changed, 64 insertions(+), 27 deletions(-)tools/testing/selftests/nolibc/nolibc-test.c +0
-0
tools/testing/selftests/nolibc/nolibc-test.c int run_stdlib(int min, int max) · line 1666
Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +3
-3@@ -1660,33 +1660,70 @@ int run_stdlib(int min, int max)return ret; } -#define EXPECT_VFPRINTF (c, expected, fmt, ...) \ - ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) +#define EXPECT_VFPRINTF (cond, expected, fmt, ...) \ + ret += expect_vfprintf(llen, cond, expected, fmt, ##__VA_ARGS__) -static int expect_vfprintf (int llen,int c,const char *expected,const char *fmt, ...) +#define VFPRINTF_LEN 25
This is only used within expect_vfprintf(), so it can be a local variable.
Quoted 5 lines
tools/testing/selftests/nolibc/nolibc-test.c +3
-1+ +static int expect_vfprintf (int llen,int cond,const char *expected,const char *fmt, ...) { -char buf[100 ]; +ssize_t written, expected_len;
tools/testing/selftests/nolibc/nolibc-test.c int run_stdlib(int min, int max) · line 1678
Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +6
-2+char buf[VFPRINTF_LEN +80 ]; +unsigned int cmp_len;va_list args; -ssize_t w; -int ret; +if (!cond) { +result (llen,SKIPPED ); +return 0 ; + }
The other EXPECT_*() macros evaluate the condition in the macro, not the corresponding function. I'm not entirely sure why that is, but please keep it consistent.
Quoted 42 lines
tools/testing/selftests/nolibc/nolibc-test.c +28
-6+ +/* Fill and terminate buffer to check for overlong/absent writes */ +memset (buf,0xa5 ,sizeof (buf) -1 ); + buf[sizeof (buf) -1 ] =0 ;va_start (args, fmt); -/* Only allow writing 21 bytes, to test truncation */ - w =vsnprintf (buf,21 , fmt, args); +/* Limit the buffer length to test truncation */ + written =vsnprintf (buf,VFPRINTF_LEN +1 , fmt, args);va_end (args); -if (w != c) { - llen +=printf (" written(%d) != %d" , (int )w, c); + llen +=printf (" \"%s\"" , buf); + +/* If the expected output is long it will have been truncated. */ + expected_len =strlen (expected); +if (expected_len >VFPRINTF_LEN ) { +/* Indicate truncated in test output */ + llen +=printf ("+" ); + cmp_len =VFPRINTF_LEN ; + }else { + cmp_len = expected_len; + } + +if (memcmp (expected, buf, cmp_len) || buf[cmp_len]) { +/* Copy and truncate until "%.*s" supported */ +memcpy (buf, expected, cmp_len); + buf[cmp_len] =0 ; + llen +=printf (" should be \"%s\"" , buf);result (llen,FAIL );return 1 ; } - llen +=printf (" \"%s\" = \"%s\"" , expected, buf); - ret =strncmp (expected, buf, c); +if (written != expected_len) { + llen +=printf (" written(%d) != %d" , (int )written, (int )expected_len); +result (llen,FAIL ); +return 1 ; + }
(...)
Re: [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors
On Wed, 25 Feb 2026 22:56:03 +0100
Thomas Weißschuh <[email protected]> wrote:
...
Quoted 4 lines
Quoted 2 lines
Increase the size limit from 20 to 25 characters, changing the tests to match. This is needed to test octal conversions later on.
Then please do this right before the addition of the octal conversion.
I also kept hitting the limit trying to write other tests. It is very easy to hit 20 chars when testing precision (three %6.2d is too long)
Although I think the tests were written with that change done later. I put it early so that I wouldn't have to change new tests that tested truncation.
Perhaps I should justify the change because it lets tests check longer data, not just octal?
I will then use longer test output in some of the other patches.
Quoted 4 lines
Quoted 4 lines
Append a '+' to the printed output (after the final ") when the output is truncated.
Quoted 8 lines
Quoted 5 lines
Additionally check that nothing beyond the end is written. The "
width_trunc" test (#14) now fails becausee90ce42e81381("tools/nolibc: implement width padding inprintf()") doesn't correctly update the space in the buffer when adding pad characters. This will be addressed in a later patch.The build bots will yell at us for this. Instead mark the test as skipped until it is fixed.
The build bots won't bleat - it is a run-time error. But I can disable it.
I'm not sure there is a #define for 'broken' and there isn't room for comments on the test cases.
So it might just be a literal 0.
Quoted 35 lines
Quoted 4 lines
Also correctly return 1 (the number of errors) when
strcmp()fails rather than the return value fromstrncmp()which is the signed difference between the mismatching characters.Please split these different steps into dedicated commits. Yes, the testcases are going to be rewritten a bunch of times, but that does not matter.
Quoted 26 lines
Signed-off-by David Laight <[email protected]>---
For v3:
- Patch 9 in v2, patch 5 in v1..
- Increase the size limit to 25 in preparation for testing octal.
- Change the way truncatedfprintf()are handled.
- Allow for tests being skipped.
- Use a fixed value (0xa5) for the canary when detecting overwrites.tools/testing/selftests/nolibc/nolibc-test.c | 91 ++++++++++++++------ 1 file changed, 64 insertions(+), 27 deletions(-)tools/testing/selftests/nolibc/nolibc-test.c +2
-2@@ -1660,33 +1660,70 @@ int run_stdlib(int min, int max)return ret; } -#define EXPECT_VFPRINTF (c, expected, fmt, ...) \ - ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) +#define EXPECT_VFPRINTF (cond, expected, fmt, ...) \ + ret += expect_vfprintf(llen, cond, expected, fmt, ##__VA_ARGS__)
Quoted 4 lines
tools/testing/selftests/nolibc/nolibc-test.c int run_stdlib(int min, int max) · line 1666
Quoted 2 lines
tools/testing/selftests/nolibc/nolibc-test.c +1
-1-static int expect_vfprintf (int llen,int c,const char *expected,const char *fmt, ...) +#define VFPRINTF_LEN 25This is only used within
expect_vfprintf(), so it can be a local variable.
I used it to size buf[] so it needs to be an 'integer constant expression'. The only simple way to do that is with a #define - which isn't scoped.
Quoted 12 lines
Quoted 12 lines
tools/testing/selftests/nolibc/nolibc-test.c +5
-3+ +static int expect_vfprintf (int llen,int cond,const char *expected,const char *fmt, ...) { -char buf[100 ]; +ssize_t written, expected_len; +char buf[VFPRINTF_LEN +80 ]; +unsigned int cmp_len;va_list args; -ssize_t w; -int ret;
Quoted 8 lines
tools/testing/selftests/nolibc/nolibc-test.c int run_stdlib(int min, int max) · line 1678
Quoted 4 lines
tools/testing/selftests/nolibc/nolibc-test.c +4
-0+if (!cond) { +result (llen,SKIPPED ); +return 0 ; + }The other EXPECT_*() macros evaluate the condition in the macro, not the corresponding function. I'm not entirely sure why that is, but please keep it consistent.
Most of the other ones call the function in the #define and just report the success/fail in the function. The SKIP test has to be before the function call - so has to be in the #define.
For vfprintf (should be snprintf) the test is in the function. So really it should be TEST_SNPRINTF().
The only other similar one is EXPECT_STRTOX().
But I can change it for consistency.
I might just re-post patches to this file.
The later patches will depend on it, but that won't matter if it get applied.
Where do these patches get applied to?
So I can base new versions on the changes.
I guess they'll get picked up into 'next' fairly quickly.
David
Thomas Weißschuh
maintainerRe: [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors
Quoted 16 lines · David Laight
On Wed, 25 Feb 2026 22:56:03 +0100
Thomas Weißschuh <[email protected]> wrote:...
Quoted 4 lines
Quoted 2 lines
Increase the size limit from 20 to 25 characters, changing the tests to match. This is needed to test octal conversions later on.
Then please do this right before the addition of the octal conversion.
I also kept hitting the limit trying to write other tests. It is very easy to hit 20 chars when testing precision (three %6.2d is too long)
Although I think the tests were written with that change done later. I put it early so that I wouldn't have to change new tests that tested truncation.
Quoted 3 lines
Perhaps I should justify the change because it lets tests check longer data, not just octal?
I will then use longer test output in some of the other patches.
That sounds good. But please in a dedicated patch.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Append a '+' to the printed output (after the final ") when the output is truncated.
Additionally check that nothing beyond the end is written. The "
width_trunc" test (#14) now fails becausee90ce42e81381("tools/nolibc: implement width padding inprintf()") doesn't correctly update the space in the buffer when adding pad characters. This will be addressed in a later patch.
Quoted 4 lines
Quoted 2 lines
The build bots will yell at us for this. Instead mark the test as skipped until it is fixed.
The build bots won't bleat - it is a run-time error.
LKP also does some selftests. It will break sometime. Also I run the tests for each commit and don't want broken intermediate states.
Quoted 1 line
But I can disable it.
Can we not just move the fix to printf before this commit? Then the problem just goes away.
Quoted 3 lines
I'm not sure there is a #define for 'broken' and there isn't room for comments on the test cases.
So it might just be a literal 0.
If it needs to be disabled use "0 /* FIXME */" and mention it in the commit message. But reordering the commits would be better.
Quoted 34 lines
Quoted 34 lines
Quoted 3 lines
Also correctly return 1 (the number of errors) when
strcmp()fails rather than the return value fromstrncmp()which is the signed difference between the mismatching characters.Please split these different steps into dedicated commits. Yes, the testcases are going to be rewritten a bunch of times, but that does not matter.
Quoted 26 lines
Signed-off-by David Laight <[email protected]>---
For v3:
- Patch 9 in v2, patch 5 in v1..
- Increase the size limit to 25 in preparation for testing octal.
- Change the way truncatedfprintf()are handled.
- Allow for tests being skipped.
- Use a fixed value (0xa5) for the canary when detecting overwrites.tools/testing/selftests/nolibc/nolibc-test.c | 91 ++++++++++++++------ 1 file changed, 64 insertions(+), 27 deletions(-)tools/testing/selftests/nolibc/nolibc-test.c +2
-2@@ -1660,33 +1660,70 @@ int run_stdlib(int min, int max)return ret; } -#define EXPECT_VFPRINTF (c, expected, fmt, ...) \ - ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) +#define EXPECT_VFPRINTF (cond, expected, fmt, ...) \ + ret += expect_vfprintf(llen, cond, expected, fmt, ##__VA_ARGS__)
Quoted 7 lines
Quoted 4 lines
tools/testing/selftests/nolibc/nolibc-test.c int run_stdlib(int min, int max) · line 1666
Quoted 2 lines
tools/testing/selftests/nolibc/nolibc-test.c +1
-1-static int expect_vfprintf (int llen,int c,const char *expected,const char *fmt, ...) +#define VFPRINTF_LEN 25This is only used within
expect_vfprintf(), so it can be a local variable.I used it to size buf[] so it needs to be an 'integer constant expression'. The only simple way to do that is with a #define - which isn't scoped.
Yeah, ok.
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/nolibc/nolibc-test.c +5
-3+ +static int expect_vfprintf (int llen,int cond,const char *expected,const char *fmt, ...) { -char buf[100 ]; +ssize_t written, expected_len; +char buf[VFPRINTF_LEN +80 ]; +unsigned int cmp_len;va_list args; -ssize_t w; -int ret;Quoted 8 lines
tools/testing/selftests/nolibc/nolibc-test.c int run_stdlib(int min, int max) · line 1678
Quoted 4 lines
tools/testing/selftests/nolibc/nolibc-test.c +4
-0+if (!cond) { +result (llen,SKIPPED ); +return 0 ; + }The other EXPECT_*() macros evaluate the condition in the macro, not the corresponding function. I'm not entirely sure why that is, but please keep it consistent.
Quoted 8 lines
Most of the other ones call the function in the #define and just report the success/fail in the function. The SKIP test has to be before the function call - so has to be in the #define.
For vfprintf (should be snprintf) the test is in the function. So really it should beTEST_SNPRINTF().
The only other similar one isEXPECT_STRTOX().But I can change it for consistency.
Please do.
Quoted 1 line
I might just re-post patches to this file.
Please repost the whole series. Or just a small series with the already reviewed bits (patches 1-9) which I can apply easily and wait for proper review of the big new stuff.
Quoted 4 lines
The later patches will depend on it, but that won't matter if it get applied.
Where do these patches get applied to?
https://git.kernel.org/pub/scm/linux/kernel/git/nolibc/linux-nolibc.git/log/?h=for-next
Quoted 2 lines
So I can base new versions on the changes.
I guess they'll get picked up into 'next' fairly quickly.
They are already in today's -next.
Thomas
PATCH v3 5/17 tools/nolibc: Implement strerror() in terms of strerror_r()
In reply to [email protected]From: David Laight <[email protected]>
strerror() can be the only part of a program that has a .data section. This requres 4k in the program file.
Add a simple implementation of strerror_r() (ignores the length) and use that in strerror() so that the "errno=" string is copied at run-time. Use __builtin_memcpy() because that optimises away the input string and just writes the required constants to the target buffer.
Put the check for NOLIBC_IGNORE_ERRNO into strerror_r(). Always call strerror() from __nolibc_printf().
Code size change largely depends on whether the inlining decision for strerror() changes.
Change the tests to use the normal EXPECT_VFPRINTF() when testing %m. Skip the tests when !is_nolibc.
---
New patch for v3.
tools/include/nolibc/stdio.h | 29 ++++++++++++++------ tools/testing/selftests/nolibc/nolibc-test.c | 20 ++------------ 2 files changed, 23 insertions(+), 26 deletions(-)
tools/include/nolibc/stdio.h +21 -8
@@ -373,11 +373,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char outstr="(null)" ; }else if (c =='m' ) { -#ifdef NOLIBC_IGNORE_ERRNO - outstr ="unknown error" ; -#else outstr =strerror (errno); -#endif /* NOLIBC_IGNORE_ERRNO */ }else if (c =='%' ) {/* queue it verbatim */
@@ -682,14 +678,31 @@ int setvbuf(FILE *stream __attribute__((unused)),return 0 ; } +static __attribute__((unused,)) +int strerror_r (int errnum,char *buf,size_t buflen __attribute__((unused))) +{ +#ifdef NOLIBC_IGNORE_ERRNO +__builtin_memcpy (buf,"unknown error" ,14 ); +return 13 ; +#else +__builtin_memcpy (buf,"errno=" ,6 ); +return 6 +i64toa_r (errnum, buf +6 ); +#endif +} +static __attribute__((unused)) -const char *strerror (int errno) +const char *strerror (int errnum) { -static char buf[18 ] ="errno=" ; +static char buf[18 ]; +char *b = buf; + +/* Force gcc to use 'register offset' to access buf[]. */ +_NOLIBC_OPTIMIZER_HIDE_VAR (b); -i64toa_r (errno, &buf[6 ]); +/* Use strerror_r() to avoid having the only .data in small programs. */ +strerror_r (errnum, b,sizeof (buf)); -return buf; +return b; }#endif /* _NOLIBC_STDIO_H */
tools/testing/selftests/nolibc/nolibc-test.c +2 -18
@@ -1793,23 +1793,6 @@ static int test_scanf(void)return 0 ; } -int test_strerror (void ) -{ -char buf[100 ]; -ssize_t ret; - -memset (buf,'A' ,sizeof (buf)); - - errno =EINVAL ; - ret =snprintf (buf,sizeof (buf),"%m" ); -if (is_nolibc) { -if (ret <6 ||memcmp (buf,"errno=" ,6 )) -return 1 ; - } - -return 0 ; -} -static int test_printf_error (void ) {int fd, ret, saved_errno;
@@ -1859,8 +1842,9 @@ static int run_printf(int min, int max)CASE_TEST (string_width);EXPECT_VFPRINTF (1 ," 1" ,"%10s" ,"1" );break ;CASE_TEST (number_width);EXPECT_VFPRINTF (1 ," 1" ,"%10d" ,1 );break ;CASE_TEST (width_trunc);EXPECT_VFPRINTF (1 ," 1" ,"%30d" ,1 );break ; +CASE_TEST (errno);EXPECT_VFPRINTF (is_nolibc,"22:errno=22" ,"%d:%m" , errno=22 );break ; +CASE_TEST (errno-neg);EXPECT_VFPRINTF (is_nolibc,"-22: errno=-22" ,"%d:%12m" , errno=-22 );break ;CASE_TEST (scanf);EXPECT_ZR (1 ,test_scanf ());break ; -CASE_TEST (strerror);EXPECT_ZR (1 ,test_strerror ());break ;CASE_TEST (printf_error);EXPECT_ZR (1 ,test_printf_error ());break ;case __LINE__:return ret;/* must be last */
Signature · 1 line
2.39.5
Thomas Weißschuh
maintainerRe: [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r()
Quoted 5 lines · On 2026-02-23 10:17:23+0000, [email protected] wrote:
From: David Laight <[email protected]>
strerror()can be the only part of a program that has a .data section. This requres 4k in the program file.
Quoted 7 lines
Add a simple implementation of
strerror_r()(ignores the length) and use that instrerror()so that the "errno=" string is copied at run-time. Use__builtin_memcpy()because that optimises away the input string and just writes the required constants to the target buffer.Put the check for
NOLIBC_IGNORE_ERRNOintostrerror_r(). Always callstrerror()from__nolibc_printf().
Why?
Quoted 32 lines
Code size change largely depends on whether the inlining decision for
strerror()changes.Change the tests to use the normal
EXPECT_VFPRINTF()when testing %m. Skip the tests when !is_nolibc.Signed-off-by David Laight <[email protected]>---
New patch for v3.
tools/include/nolibc/stdio.h | 29 ++++++++++++++------ tools/testing/selftests/nolibc/nolibc-test.c | 20 ++------------ 2 files changed, 23 insertions(+), 26 deletions(-)tools/include/nolibc/stdio.h +0
-4@@ -373,11 +373,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char outstr="(null)" ; }else if (c =='m' ) { -#ifdef NOLIBC_IGNORE_ERRNO - outstr ="unknown error" ; -#else outstr =strerror (errno); -#endif /* NOLIBC_IGNORE_ERRNO */ }else if (c =='%' ) {/* queue it verbatim */
tools/include/nolibc/stdio.h int setvbuf(FILE *stream __attribute__((unused)), · line 682
Quoted 5 lines
tools/include/nolibc/stdio.h +2
-0@@ -682,14 +678,31 @@ int setvbuf(FILE *stream __attribute__((unused)),return 0 ; } +static __attribute__((unused,)) +int strerror_r (int errnum,char *buf,size_t buflen __attribute__((unused)))
This is the GNU variant, but the XSI variant would be better.
Why is the buflen parameter not used?
tools/include/nolibc/stdio.h int setvbuf(FILE *stream __attribute__((unused)), · line 684
Quoted 2 lines
tools/include/nolibc/stdio.h +2
-0+{ +#ifdef NOLIBC_IGNORE_ERRNO
Why did this move here?
The application can have error numbers without errno.
tools/include/nolibc/stdio.h int setvbuf(FILE *stream __attribute__((unused)), · line 694
Quoted 12 lines
tools/include/nolibc/stdio.h +9
-1+__builtin_memcpy (buf,"unknown error" ,14 ); +return 13 ; +#else +__builtin_memcpy (buf,"errno=" ,6 ); +return 6 +i64toa_r (errnum, buf +6 ); +#endif +} +static __attribute__((unused)) -const char *strerror (int errno) +const char *strerror (int errnum)
Good point. But also a candidate for its own patch.
Quoted 46 lines
tools/include/nolibc/stdio.h +8
-3{ -static char buf[18 ] ="errno=" ; +static char buf[18 ]; +char *b = buf; + +/* Force gcc to use 'register offset' to access buf[]. */ +_NOLIBC_OPTIMIZER_HIDE_VAR (b); -i64toa_r (errno, &buf[6 ]); +/* Use strerror_r() to avoid having the only .data in small programs. */ +strerror_r (errnum, b,sizeof (buf)); -return buf; +return b; }#endif /*
_NOLIBC_STDIO_H*/tools/testing/selftests/nolibc/nolibc-test.c +0
-17@@ -1793,23 +1793,6 @@ static int test_scanf(void)return 0 ; } -int test_strerror (void ) -{ -char buf[100 ]; -ssize_t ret; - -memset (buf,'A' ,sizeof (buf)); - - errno =EINVAL ; - ret =snprintf (buf,sizeof (buf),"%m" ); -if (is_nolibc) { -if (ret <6 ||memcmp (buf,"errno=" ,6 )) -return 1 ; - } - -return 0 ; -} -static int test_printf_error (void ) {int fd, ret, saved_errno;
tools/testing/selftests/nolibc/nolibc-test.c static int run_printf(int min, int max) · line 1846
Quoted 5 lines
tools/testing/selftests/nolibc/nolibc-test.c +2
-0@@ -1859,8 +1842,9 @@ static int run_printf(int min, int max)CASE_TEST (string_width);EXPECT_VFPRINTF (1 ," 1" ,"%10s" ,"1" );break ;CASE_TEST (number_width);EXPECT_VFPRINTF (1 ," 1" ,"%10d" ,1 );break ;CASE_TEST (width_trunc);EXPECT_VFPRINTF (1 ," 1" ,"%30d" ,1 );break ; +CASE_TEST (errno);EXPECT_VFPRINTF (is_nolibc,"22:errno=22" ,"%d:%m" , errno=22 );break ; +CASE_TEST (errno-neg);EXPECT_VFPRINTF (is_nolibc,"-22: errno=-22" ,"%d:%12m" , errno=-22 );break ;
Assigning errno from the printf argument is sneaky. Please avoid sneakyness.
Quoted 8 lines
tools/testing/selftests/nolibc/nolibc-test.c +0
-1CASE_TEST (scanf);EXPECT_ZR (1 ,test_scanf ());break ; -CASE_TEST (strerror);EXPECT_ZR (1 ,test_strerror ());break ;CASE_TEST (printf_error);EXPECT_ZR (1 ,test_printf_error ());break ;case __LINE__:return ret;/* must be last */ Signature · 1 line
2.39.5
Re: [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r()
Quoted 51 lines · On Wed, 25 Feb 2026 23:09:25 +0100 Thomas Weißschuh <[email protected]>…
Quoted 5 lines · On 2026-02-23 10:17:23+0000, [email protected] wrote:
From: David Laight <[email protected]>
strerror()can be the only part of a program that has a .data section. This requres 4k in the program file.Quoted 7 lines
Add a simple implementation of
strerror_r()(ignores the length) and use that instrerror()so that the "errno=" string is copied at run-time. Use__builtin_memcpy()because that optimises away the input string and just writes the required constants to the target buffer.Put the check for
NOLIBC_IGNORE_ERRNOintostrerror_r(). Always callstrerror()from__nolibc_printf().Why?
Quoted 35 lines
Code size change largely depends on whether the inlining decision for
strerror()changes.Change the tests to use the normal
EXPECT_VFPRINTF()when testing %m. Skip the tests when !is_nolibc.Signed-off-by David Laight <[email protected]>---
New patch for v3.
tools/include/nolibc/stdio.h | 29 ++++++++++++++------ tools/testing/selftests/nolibc/nolibc-test.c | 20 ++------------ 2 files changed, 23 insertions(+), 26 deletions(-)tools/include/nolibc/stdio.h +0
-4@@ -373,11 +373,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char outstr="(null)" ; }else if (c =='m' ) { -#ifdef NOLIBC_IGNORE_ERRNO - outstr ="unknown error" ; -#else outstr =strerror (errno); -#endif /* NOLIBC_IGNORE_ERRNO */ }else if (c =='%' ) {/* queue it verbatim */ @@ -682,14 +678,31 @@ int setvbuf(FILE *stream __attribute__((unused)),return 0 ; }
Quoted 4 lines
tools/include/nolibc/stdio.h int setvbuf(FILE *stream __attribute__((unused)), · line 682
Quoted 2 lines
tools/include/nolibc/stdio.h +2
-0+static __attribute__((unused,)) +int strerror_r (int errnum,char *buf,size_t buflen __attribute__((unused)))This is the GNU variant, but the XSI variant would be better.
man strerror gives me:
int strerror_r(int errnum, char buf[.buflen], size_t buflen);
/* XSI-compliant */
char *strerror_r(int errnum, char buf[.buflen], size_t buflen);
/* GNU-specific */so I implemented the one that returns the length.
Quoted 2 lines
Why is the buflen parameter not used?
Because the i64toa_r() code doesn't have a buffer length parameter. I could add one, but it would be effectively unused everywhere else. Or I could add extra code that did extra copies (etc), but in practise the buffer will be long enough (given the short output) and the limited number of programs that use nolibc.
Not that truncating the output makes any sense.
The best would be "?", "errno=?" or "errno=value" for len > 17.
Using strerror_r() came from a discussion with Willy. He wanted to avoid the static initialised data and it looks better to have strerror() implemented using strerror_r().
Quoted 6 lines
tools/include/nolibc/stdio.h int setvbuf(FILE *stream __attribute__((unused)), · line 684
Quoted 3 lines
tools/include/nolibc/stdio.h +2
-0+{ +#ifdef NOLIBC_IGNORE_ERRNO Why did this move here?
The application can have error numbers without errno.
Where would they come from.
ISTR there are a small number of unusual functions that do return an errno value - but nothing common.
Quoted 61 lines
tools/include/nolibc/stdio.h int setvbuf(FILE *stream __attribute__((unused)), · line 694
Quoted 13 lines
tools/include/nolibc/stdio.h +9
-1+__builtin_memcpy (buf,"unknown error" ,14 ); +return 13 ; +#else +__builtin_memcpy (buf,"errno=" ,6 ); +return 6 +i64toa_r (errnum, buf +6 ); +#endif +} +static __attribute__((unused)) -const char *strerror (int errno) +const char *strerror (int errnum)Good point. But also a candidate for its own patch.
Quoted 45 lines
tools/include/nolibc/stdio.h +8
-3{ -static char buf[18 ] ="errno=" ; +static char buf[18 ]; +char *b = buf; + +/* Force gcc to use 'register offset' to access buf[]. */ +_NOLIBC_OPTIMIZER_HIDE_VAR (b); -i64toa_r (errno, &buf[6 ]); +/* Use strerror_r() to avoid having the only .data in small programs. */ +strerror_r (errnum, b,sizeof (buf)); -return buf; +return b; }#endif /*
_NOLIBC_STDIO_H*/tools/testing/selftests/nolibc/nolibc-test.c +0
-17@@ -1793,23 +1793,6 @@ static int test_scanf(void)return 0 ; } -int test_strerror (void ) -{ -char buf[100 ]; -ssize_t ret; - -memset (buf,'A' ,sizeof (buf)); - - errno =EINVAL ; - ret =snprintf (buf,sizeof (buf),"%m" ); -if (is_nolibc) { -if (ret <6 ||memcmp (buf,"errno=" ,6 )) -return 1 ; - } - -return 0 ; -} -static int test_printf_error (void ) {int fd, ret, saved_errno;
Quoted 8 lines
tools/testing/selftests/nolibc/nolibc-test.c static int run_printf(int min, int max) · line 1846
Quoted 6 lines
tools/testing/selftests/nolibc/nolibc-test.c +2
-0@@ -1859,8 +1842,9 @@ static int run_printf(int min, int max)CASE_TEST (string_width);EXPECT_VFPRINTF (1 ," 1" ,"%10s" ,"1" );break ;CASE_TEST (number_width);EXPECT_VFPRINTF (1 ," 1" ,"%10d" ,1 );break ;CASE_TEST (width_trunc);EXPECT_VFPRINTF (1 ," 1" ,"%30d" ,1 );break ; +CASE_TEST (errno);EXPECT_VFPRINTF (is_nolibc,"22:errno=22" ,"%d:%m" , errno=22 );break ; +CASE_TEST (errno-neg);EXPECT_VFPRINTF (is_nolibc,"-22: errno=-22" ,"%d:%12m" , errno=-22 );break ;Assigning errno from the printf argument is sneaky.
That's not 'sneaky', I can do 'proper sneaky' :-)
Quoted 1 line
Please avoid sneakyness.
How about:
CASE_TEST(errno); errno = 22; EXPECT_VFPRINTF(is_nolibc, "errno=22", "%m"); break; David
Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/nolibc/nolibc-test.c +0
-1CASE_TEST (scanf);EXPECT_ZR (1 ,test_scanf ());break ; -CASE_TEST (strerror);EXPECT_ZR (1 ,test_strerror ());break ;CASE_TEST (printf_error);EXPECT_ZR (1 ,test_printf_error ());break ;case __LINE__:return ret;/* must be last */ Signature · 1 line
2.39.5
PATCH v3 6/17 tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to 'outbuf[]'
In reply to [email protected]From: David Laight <[email protected]>
Changing 'c' makes the code slightly easier to read because the variable stands out from the single character literals (especially 'c').
Change tmpbuf[] to outbuf[] because 'out' points into it.
The following patches pretty much rewrite the function so the churn is limited.
---
Changes for v3:
- Patch 1 in v2.
- Rename tmpbuf[] to outbuf[] as well.
tools/include/nolibc/stdio.h | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-)
tools/include/nolibc/stdio.h +19 -19
@@ -301,16 +301,16 @@ typedef int (*__nolibc_printf_cb)(intptr_t state, const char *buf, size_t size);static __attribute__((unused,format (printf,4 ,0 )))int __nolibc_printf (__nolibc_printf_cb cb,intptr_t state,size_t n,const char *fmt,va_list args) { -char escape, lpref, c; +char escape, lpref, ch;unsigned long long v;unsigned int written, width;size_t len, ofs, w; -char tmpbuf[21 ]; +char outbuf[21 ];const char *outstr; written = ofs = escape = lpref =0 ;while (1 ) { - c = fmt[ofs++]; + ch = fmt[ofs++]; width =0 ;if (escape) {
@@ -318,17 +318,17 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char escape =0 ;/* width */ -while (c >='0' && c <='9' ) { +while (ch >='0' && ch <='9' ) { width *=10 ; - width += c -'0' ; + width += ch -'0' ; - c = fmt[ofs++]; + ch = fmt[ofs++]; } -if (c =='c' || c =='d' || c =='u' || c =='x' || c =='p' ) { -char *out = tmpbuf; +if (ch =='c' || ch =='d' || ch =='u' || ch =='x' || ch =='p' ) { +char *out = outbuf; -if (c =='p' ) +if (ch =='p' ) v = va_arg(args, unsigned long);else if (lpref) {if (lpref >1 )
@@ -338,7 +338,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char }else v = va_arg(args, unsigned int); -if (c =='d' ) { +if (ch =='d' ) {/* sign-extend the value */ if (lpref ==0 ) v = (long long )(int )v;
@@ -346,7 +346,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char v = (long long )(long )v; } -switch (c) { +switch (ch) {case 'c' : out[0 ] = v; out[1 ] =0 ;
@@ -365,26 +365,26 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const charu64toh_r (v, out);break ; } - outstr = tmpbuf; + outstr = outbuf; } -else if (c =='s' ) { +else if (ch =='s' ) { outstr = va_arg(args, char *);if (!outstr) outstr="(null)" ; } -else if (c =='m' ) { +else if (ch =='m' ) { outstr =strerror (errno); } -else if (c =='%' ) { +else if (ch =='%' ) {/* queue it verbatim */ continue ; }else {/* modifiers or final 0 */ -if (c =='l' ) { +if (ch =='l' ) {/* long format prefix, maintain the escape */ lpref++; - }else if (c =='j' ) { + }else if (ch =='j' ) { lpref =2 ; } escape =1 ;
@@ -395,7 +395,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char }/* not an escape sequence */ -if (c ==0 || c =='%' ) { +if (ch ==0 || ch =='%' ) {/* flush pending data on escape or end */ escape =1 ; lpref =0 ;
@@ -416,7 +416,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char written += len; do_escape: -if (c ==0 ) +if (ch ==0 )break ; fmt += ofs; ofs =0 ;
Signature · 1 line
2.39.5
Re: [PATCH v3 next 06/17] tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to 'outbuf[]'
Quoted 5 lines · On 2026-02-23 10:17:24+0000, [email protected] wrote:
From: David Laight <[email protected]>
Changing 'c' makes the code slightly easier to read because the variable stands out from the single character literals (especially 'c').
Quoted 8 lines
Change tmpbuf[] to outbuf[] because 'out' points into it.
The following patches pretty much rewrite the function so the churn is limited.
Acked-by Willy Tarreau <[email protected]>Signed-off-by David Laight <[email protected]>
Applied, thanks. I applied it out-of-order and fixed it up to get it out of the way.
FYI I won't finish the whole review today and for the meaty new bits Willy will also want to take a look.
(...)
PATCH v3 7/17 tools/nolibc/printf: Move snprintf length check to callback
In reply to [email protected]From: David Laight <[email protected]>
Move output truncation to the snprintf() callback.
This simplifies the main code and fixes truncation of padded fields.
Add a zero length callback to 'finalise' the buffer rather than doing it in snprintf() itself.
Fixes e90ce42e81381 ("tools/nolibc: implement width padding in printf()")
---
For v3:
- Patch 2 in v2, patch 1 in v1.
- Note that it fixes e90ce42e81381.
- Update comments (again).
- Rename size => space in snprintf 'state.
- Copy state->space to a local rather than relying on the compiler doing CSE, changes the code slightly for x86 (but not the size).
- Unconditionally write a '\0' to terminate the old data before overwriting it with new data.
Saves a few bytes of object code.
tools/include/nolibc/stdio.h | 92 +++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 27 deletions(-)
tools/include/nolibc/stdio.h +65 -27
@@ -295,16 +295,25 @@ int fseek(FILE *stream, long offset, int whence)* - %[l*]{d,u,c,x,p} * - %s * - unknown modifiers are ignored. +* +* Called by vfprintf() and snprintf() to do the actual formatting. +* The callers provide a callback function to save the formatted data. +* The callback function is called multiple times: +* - for each group of literal characters in the format string. +* - for field padding. +* - for each conversion specifier. +* - with (NULL, 0) at the end of the __nolibc_printf. +* If the callback returns non-zero __nolibc_printf() immediately returns -1. */ -typedef int (*__nolibc_printf_cb )(intptr_t state,const char *buf,size_t size); +typedef int (*__nolibc_printf_cb )(void *state,const char *buf,size_t size); -static __attribute__((unused,format (printf,4 ,0 ))) -int __nolibc_printf (__nolibc_printf_cb cb,intptr_t state,size_t n,const char *fmt,va_list args) +static __attribute__((unused,format (printf,3 ,0 ))) +int __nolibc_printf (__nolibc_printf_cb cb,void *state,const char *fmt,va_list args) {char escape, lpref, ch;unsigned long long v;unsigned int written, width; -size_t len, ofs, w; +size_t len, ofs;char outbuf[21 ];const char *outstr;
@@ -402,17 +411,13 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char outstr = fmt; len = ofs -1 ; flush_str: -if (n) { - w = len < n ? len : n; - n -= w; -while (width-- > w) { -if (cb (state," " ,1 ) !=0 ) -return -1 ; - written +=1 ; - } -if (cb (state, outstr, w) !=0 ) +while (width-- > len) { +if (cb (state," " ,1 ) !=0 )return -1 ; + written +=1 ; } +if (cb (state, outstr, len) !=0 ) +return -1 ; written += len; do_escape:
@@ -425,18 +430,25 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char/* literal char, just queue it */ } + +/* Request a final '\0' be added to the snprintf() output. +* This may be the only call of the cb() function. +*/ +if (cb (state,NULL ,0 ) !=0 ) +return -1 ; +return written; } -static int __nolibc_fprintf_cb (intptr_t state,const char *buf,size_t size) +static int __nolibc_fprintf_cb (void *stream,const char *buf,size_t size) { -return _fwrite (buf, size, (FILE *)state); +return _fwrite (buf, size, stream); }static __attribute__((unused,format (printf,2 ,0 )))int vfprintf (FILE *stream,const char *fmt,va_list args) { -return __nolibc_printf (__nolibc_fprintf_cb, (intptr_t )stream,SIZE_MAX , fmt, args); +return __nolibc_printf (__nolibc_fprintf_cb, stream, fmt, args); } static __attribute__((unused, format(printf, 1, 0)))
@@ -494,26 +506,52 @@ int dprintf(int fd, const char *fmt, ...)return ret; } -static int __nolibc_sprintf_cb (intptr_t _state,const char *buf,size_t size) +struct __nolibc_sprintf_cb_state { +char *buf; +size_t space; +}; + +static int __nolibc_sprintf_cb (void *v_state,const char *buf,size_t size) { -char **state = (char **)_state; +struct __nolibc_sprintf_cb_state *state = v_state; +size_t space = state->space; +char *tgt; + +/* Truncate the request to fit in the output buffer space. +* The last byte is reserved for the terminating '\0'. +* state->space can only be zero for snprintf(NULL, 0, fmt, args) +* so this normally lets through calls with 'size == 0'. +*/ +if (size >= space) { +if (space <=1 ) +return 0 ; + size = space -1 ; + } + tgt = state->buf; + +/* __nolibc_printf() ends with cb(state, NULL, 0) to request the output +* buffer be '\0' terminated. +* That will be the only cb() call for, eg, snprintf(buf, sz, ""). +* Zero lengths can occur at other times (eg "%s" for an empty string). +* Unconditionally write the '\0' byte to reduce code size, it is +* normally overwritten by the data being output. +* There is no point adding a '\0' after copied data - there is always +* another call. +*/ + *tgt ='\0' ; + state->space = space - size; + state->buf = tgt + size; +memcpy (tgt, buf, size); -memcpy (*state, buf, size); - *state += size;return 0 ; }static __attribute__((unused,format (printf,3 ,0 )))int vsnprintf (char *buf,size_t size,const char *fmt,va_list args) { -char *state = buf; -int ret; +struct __nolibc_sprintf_cb_state state = { .buf = buf, .space = size }; - ret =__nolibc_printf (__nolibc_sprintf_cb, (intptr_t )&state, size, fmt, args); -if (ret <0 ) -return ret; - buf[(size_t )ret < size ? (size_t )ret : size -1 ] ='\0' ; -return ret; +return __nolibc_printf (__nolibc_sprintf_cb, &state, fmt, args); } static __attribute__((unused, format(printf, 3, 4)))
Signature · 1 line
2.39.5
Thomas Weißschuh
maintainerRe: [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback
On 2026-02-23 10:17:25+0000, [email protected] wrote:
(...)
Quoted 13 lines
tools/include/nolibc/stdio.h +7
-0@@ -425,18 +430,25 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char/* literal char, just queue it */ } + +/* Request a final '\0' be added to the snprintf() output. +* This may be the only call of the cb() function. +*/ +if (cb (state,NULL ,0 ) !=0 ) +return -1 ; +return written; }
(...)
Quoted 22 lines
tools/include/nolibc/stdio.h +20
-1+static int __nolibc_sprintf_cb (void *v_state,const char *buf,size_t size) { -char **state = (char **)_state; +struct __nolibc_sprintf_cb_state *state = v_state; +size_t space = state->space; +char *tgt; + +/* Truncate the request to fit in the output buffer space. +* The last byte is reserved for the terminating '\0'. +* state->space can only be zero for snprintf(NULL, 0, fmt, args) +* so this normally lets through calls with 'size == 0'. +*/ +if (size >= space) { +if (space <=1 ) +return 0 ; + size = space -1 ; + } + tgt = state->buf; + +/* __nolibc_printf() ends with cb(state, NULL, 0) to request the output +* buffer be '\0' terminated. +* That will be the only cb() call for, eg, snprintf(buf, sz, "").
tools/include/nolibc/stdio.h int dprintf(int fd, const char *fmt, ...) · line 544
Quoted 10 lines
tools/include/nolibc/stdio.h +10
-0+* Zero lengths can occur at other times (eg "%s" for an empty string). +* Unconditionally write the '\0' byte to reduce code size, it is +* normally overwritten by the data being output. +* There is no point adding a '\0' after copied data - there is always +* another call. +*/ + *tgt ='\0' ; + state->space = space - size; + state->buf = tgt + size; +memcpy (tgt, buf, size);
This trips UBSAN for me when 'buf == NULL'.
if (cb(state, NULL, 0) != 0)
return -1;
It can be fixed by adding a NULL check around memcpy(), but I'd rather not do this as a random fixup.
Quoted 5 lines
tools/include/nolibc/stdio.h +0
-2-memcpy (*state, buf, size); - *state += size;return 0 ; }
(...)
Re: [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback
Quoted 55 lines · On Wed, 25 Feb 2026 23:37:42 +0100 Thomas Weißschuh <[email protected]>…
On 2026-02-23 10:17:25+0000, [email protected] wrote:
(...)
Quoted 13 lines
tools/include/nolibc/stdio.h +7
-0@@ -425,18 +430,25 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char/* literal char, just queue it */ } + +/* Request a final '\0' be added to the snprintf() output. +* This may be the only call of the cb() function. +*/ +if (cb (state,NULL ,0 ) !=0 ) +return -1 ; +return written; }(...)
Quoted 22 lines
tools/include/nolibc/stdio.h +20
-1+static int __nolibc_sprintf_cb (void *v_state,const char *buf,size_t size) { -char **state = (char **)_state; +struct __nolibc_sprintf_cb_state *state = v_state; +size_t space = state->space; +char *tgt; + +/* Truncate the request to fit in the output buffer space. +* The last byte is reserved for the terminating '\0'. +* state->space can only be zero for snprintf(NULL, 0, fmt, args) +* so this normally lets through calls with 'size == 0'. +*/ +if (size >= space) { +if (space <=1 ) +return 0 ; + size = space -1 ; + } + tgt = state->buf; + +/* __nolibc_printf() ends with cb(state, NULL, 0) to request the output +* buffer be '\0' terminated. +* That will be the only cb() call for, eg, snprintf(buf, sz, ""). tools/include/nolibc/stdio.h int dprintf(int fd, const char *fmt, ...) · line 544
Quoted 10 lines
tools/include/nolibc/stdio.h +10
-0+* Zero lengths can occur at other times (eg "%s" for an empty string). +* Unconditionally write the '\0' byte to reduce code size, it is +* normally overwritten by the data being output. +* There is no point adding a '\0' after copied data - there is always +* another call. +*/ + *tgt ='\0' ; + state->space = space - size; + state->buf = tgt + size; +memcpy (tgt, buf, size);This trips UBSAN for me when 'buf == NULL'.
Quoted 5 lines
if (
cb(state, NULL, 0)!= 0)return -1;It can be fixed by adding a NULL check around
memcpy(), but I'd rather not do this as a random fixup.
Blame Willy, he made me remove the 'if (size)' check to reduce the code size.
The '*tgt = 0' line is (only) needed when size is zero, the other lines are clearly pointless.
So the 'random fixup' is adding 'if (size)' rather than a NULL pointer checkprintf("%s", "") will give a zero size with non-NULL buf.
IIRC the C standard make memcpy(tgt, NULL, 0) UB because some old system no one has used for 40+ years would trap when NULL was loaded into an 'address register' and they wanted it to be compliant.
David
Quoted 8 lines
Quoted 6 lines
tools/include/nolibc/stdio.h +0
-2-memcpy (*state, buf, size); - *state += size;return 0 ; }(...)
Thomas Weißschuh
maintainerRe: [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback
Quoted 58 lines · David Laight
Quoted 55 lines · On Wed, 25 Feb 2026 23:37:42 +0100 Thomas Weißschuh <[email protected]>…
On 2026-02-23 10:17:25+0000, [email protected] wrote:
(...)
Quoted 13 lines
tools/include/nolibc/stdio.h +7
-0@@ -425,18 +430,25 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char/* literal char, just queue it */ } + +/* Request a final '\0' be added to the snprintf() output. +* This may be the only call of the cb() function. +*/ +if (cb (state,NULL ,0 ) !=0 ) +return -1 ; +return written; }(...)
Quoted 22 lines
tools/include/nolibc/stdio.h +20
-1+static int __nolibc_sprintf_cb (void *v_state,const char *buf,size_t size) { -char **state = (char **)_state; +struct __nolibc_sprintf_cb_state *state = v_state; +size_t space = state->space; +char *tgt; + +/* Truncate the request to fit in the output buffer space. +* The last byte is reserved for the terminating '\0'. +* state->space can only be zero for snprintf(NULL, 0, fmt, args) +* so this normally lets through calls with 'size == 0'. +*/ +if (size >= space) { +if (space <=1 ) +return 0 ; + size = space -1 ; + } + tgt = state->buf; + +/* __nolibc_printf() ends with cb(state, NULL, 0) to request the output +* buffer be '\0' terminated. +* That will be the only cb() call for, eg, snprintf(buf, sz, ""). tools/include/nolibc/stdio.h int dprintf(int fd, const char *fmt, ...) · line 544
Quoted 10 lines
tools/include/nolibc/stdio.h +10
-0+* Zero lengths can occur at other times (eg "%s" for an empty string). +* Unconditionally write the '\0' byte to reduce code size, it is +* normally overwritten by the data being output. +* There is no point adding a '\0' after copied data - there is always +* another call. +*/ + *tgt ='\0' ; + state->space = space - size; + state->buf = tgt + size; +memcpy (tgt, buf, size);This trips UBSAN for me when 'buf == NULL'.
Quoted 8 lines
Quoted 5 lines
if (
cb(state, NULL, 0)!= 0)return -1;It can be fixed by adding a NULL check around
memcpy(), but I'd rather not do this as a random fixup.Blame Willy, he made me remove the 'if (size)' check to reduce the code size.
Done. But we still can't hard-break our own testsuite. If we can detect UBSAN at build-time, that could work. But I would prefer to just add the check.
Quoted 5 lines
The '*tgt = 0' line is (only) needed when size is zero, the other lines are clearly pointless.
So the 'random fixup' is adding 'if (size)' rather than a NULL pointer checkprintf("%s", "")will give a zero size with non-NULL buf.
Can you roll this into the next revision?
Quoted 3 lines
IIRC the C standard make
memcpy(tgt, NULL, 0)UB because some old system no one has used for 40+ years would trap when NULL was loaded into an 'address register' and they wanted it to be compliant.
Fair enough. But how would this work for functions where NULL is an allowed argument value like 'free()'? Anyways, we use UBSAN in the testsuite and it actually found a bunch of issues, so I'd like to keep it.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
tools/include/nolibc/stdio.h +0
-2-memcpy (*state, buf, size); - *state += size;return 0 ; }(...)
Re: [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback
Quoted 72 lines · On Thu, 26 Feb 2026 22:29:07 +0100 Thomas Weißschuh <[email protected]>…
Quoted 58 lines · David Laight
Quoted 55 lines · On Wed, 25 Feb 2026 23:37:42 +0100 Thomas Weißschuh <[email protected]>…
On 2026-02-23 10:17:25+0000, [email protected] wrote:
(...)
Quoted 13 lines
tools/include/nolibc/stdio.h +7
-0@@ -425,18 +430,25 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char/* literal char, just queue it */ } + +/* Request a final '\0' be added to the snprintf() output. +* This may be the only call of the cb() function. +*/ +if (cb (state,NULL ,0 ) !=0 ) +return -1 ; +return written; }(...)
Quoted 22 lines
tools/include/nolibc/stdio.h +20
-1+static int __nolibc_sprintf_cb (void *v_state,const char *buf,size_t size) { -char **state = (char **)_state; +struct __nolibc_sprintf_cb_state *state = v_state; +size_t space = state->space; +char *tgt; + +/* Truncate the request to fit in the output buffer space. +* The last byte is reserved for the terminating '\0'. +* state->space can only be zero for snprintf(NULL, 0, fmt, args) +* so this normally lets through calls with 'size == 0'. +*/ +if (size >= space) { +if (space <=1 ) +return 0 ; + size = space -1 ; + } + tgt = state->buf; + +/* __nolibc_printf() ends with cb(state, NULL, 0) to request the output +* buffer be '\0' terminated. +* That will be the only cb() call for, eg, snprintf(buf, sz, ""). tools/include/nolibc/stdio.h int dprintf(int fd, const char *fmt, ...) · line 544
Quoted 10 lines
tools/include/nolibc/stdio.h +10
-0+* Zero lengths can occur at other times (eg "%s" for an empty string). +* Unconditionally write the '\0' byte to reduce code size, it is +* normally overwritten by the data being output. +* There is no point adding a '\0' after copied data - there is always +* another call. +*/ + *tgt ='\0' ; + state->space = space - size; + state->buf = tgt + size; +memcpy (tgt, buf, size);This trips UBSAN for me when 'buf == NULL'.
Quoted 8 lines
Quoted 5 lines
if (
cb(state, NULL, 0)!= 0)return -1;It can be fixed by adding a NULL check around
memcpy(), but I'd rather not do this as a random fixup.Blame Willy, he made me remove the 'if (size)' check to reduce the code size.
Done. But we still can't hard-break our own testsuite. If we can detect UBSAN at build-time, that could work. But I would prefer to just add the check.
Quoted 7 lines
Quoted 5 lines
The '*tgt = 0' line is (only) needed when size is zero, the other lines are clearly pointless.
So the 'random fixup' is adding 'if (size)' rather than a NULL pointer checkprintf("%s", "")will give a zero size with non-NULL buf.Can you roll this into the next revision?
I'll add the zero check back.
Probably get around to it tomorrow.
At least I don't have to completely re-order the patches again. I've got about 15 old branches from failed attempts lurking.
David
Quoted 17 lines
Quoted 4 lines
IIRC the C standard make
memcpy(tgt, NULL, 0)UB because some old system no one has used for 40+ years would trap when NULL was loaded into an 'address register' and they wanted it to be compliant.Fair enough. But how would this work for functions where NULL is an allowed argument value like '
free()'? Anyways, we use UBSAN in the testsuite and it actually found a bunch of issues, so I'd like to keep it.Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
tools/include/nolibc/stdio.h +0
-2-memcpy (*state, buf, size); - *state += size;return 0 ; }(...)
PATCH v3 8/17 tools/nolibc/printf: Output pad characters in 16 byte chunks
In reply to [email protected]From: David Laight <[email protected]>
Simple to do and saves calls to the callback function.
Change variables written, width and len to 'signed int' to get better code.
---
For v3:
- Change to signed variables here rather than a later patch.
tools/include/nolibc/stdio.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
tools/include/nolibc/stdio.h +9 -5
@@ -312,8 +312,8 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list {char escape, lpref, ch;unsigned long long v; -unsigned int written, width; -size_t len, ofs; +int written, width, len; +size_t ofs;char outbuf[21 ];const char *outstr;
@@ -411,10 +411,14 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list outstr = fmt; len = ofs -1 ; flush_str: -while (width-- > len) { -if (cb (state," " ,1 ) !=0 ) + width -= len; +while (width >0 ) { +/* Output pad in 16 byte blocks with the small block first. */ +int pad_len = ((width -1 ) &15 ) +1 ; + width -= pad_len; + written += pad_len; +if (cb (state," " , pad_len) !=0 )return -1 ; - written +=1 ; }if (cb (state, outstr, len) !=0 )return -1 ;
Signature · 1 line
2.39.5
PATCH v3 9/17 tools/nolibc/printf: Simplify __nolibc_printf()
In reply to [email protected]From: David Laight <[email protected]>
Move the check for the length modifiers into the format processing between the field width and conversion specifier.
This lets the loop be simplified and a 'fast scan' for a format start used.
If an error is detected (eg an invalid conversion specifier) then copy the invalid format to the output buffer.
Reduces code size by about 10% on x86-64.
Acked-By; Willy Tarreau <[email protected]>
---
Some versions of gcc bloat this version by generating a jump table. All goes away in the later patches.
No change for v3, patch 5 in v2.
tools/include/nolibc/stdio.h | 104 ++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 51 deletions(-)
tools/include/nolibc/stdio.h +53 -51
@@ -310,28 +310,52 @@ typedef int (*__nolibc_printf_cb)(void *state, const char *buf, size_t size);static __attribute__((unused,format (printf,3 ,0 )))int __nolibc_printf (__nolibc_printf_cb cb,void *state,const char *fmt,va_list args) { -char escape, lpref, ch; +char lpref, ch;unsigned long long v;int written, width, len; -size_t ofs;char outbuf[21 ];const char *outstr; - written = ofs = escape = lpref =0 ; + written =0 ;while (1 ) { - ch = fmt[ofs++]; + outstr = fmt; + ch = *fmt++; +if (!ch) +break ; + width =0 ; +if (ch !='%' ) { +while (*fmt && *fmt !='%' ) + fmt++; +/* Output characters from the format string. */ + len = fmt - outstr; + }else { +/* we're in a format sequence */ -if (escape) { -/* we're in an escape sequence, ofs == 1 */ - escape =0 ; + ch = *fmt++;/* width */ while (ch >='0' && ch <='9' ) { width *=10 ; width += ch -'0' ; - ch = fmt[ofs++]; + ch = *fmt++; + } + +/* Length modifiers */ +if (ch =='l' ) { + lpref =1 ; + ch = *fmt++; +if (ch =='l' ) { + lpref =2 ; + ch = *fmt++; + } + }else if (ch =='j' ) { +/* intmax_t is long long */ + lpref =2 ; + ch = *fmt++; + }else { + lpref =0 ; }if (ch =='c' || ch =='d' || ch =='u' || ch =='x' || ch =='p' ) {
@@ -383,56 +407,34 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list }else if (ch =='m' ) { outstr =strerror (errno); - } -else if (ch =='%' ) { -/* queue it verbatim */ -continue ; - } -else { -/* modifiers or final 0 */ -if (ch =='l' ) { -/* long format prefix, maintain the escape */ - lpref++; - }else if (ch =='j' ) { - lpref =2 ; + }else { +if (ch !='%' ) { +/* Invalid format: back up to output the format characters */ + fmt = outstr +1 ; +/* and output a '%' now. */ } - escape =1 ; - goto do_escape; +/* %% is documented as a 'conversion specifier'. +* Any flags, precision or length modifier are ignored. +*/ + width =0 ; + outstr ="%" ; } len =strlen (outstr); - goto flush_str; } -/* not an escape sequence */ -if (ch ==0 || ch =='%' ) { -/* flush pending data on escape or end */ - escape =1 ; - lpref =0 ; - outstr = fmt; - len = ofs -1 ; - flush_str: - width -= len; -while (width >0 ) { -/* Output pad in 16 byte blocks with the small block first. */ -int pad_len = ((width -1 ) &15 ) +1 ; - width -= pad_len; - written += pad_len; -if (cb (state," " , pad_len) !=0 ) -return -1 ; - } -if (cb (state, outstr, len) !=0 ) -return -1 ; + written += len; - written += len; - do_escape: -if (ch ==0 ) -break ; - fmt += ofs; - ofs =0 ; -continue ; + width -= len; +while (width >0 ) { +/* Output pad in 16 byte blocks with the small block first. */ +int pad_len = ((width -1 ) &15 ) +1 ; + width -= pad_len; + written += pad_len; +if (cb (state," " , pad_len) !=0 ) +return -1 ; } - -/* literal char, just queue it */ +if (cb (state, outstr, len) !=0 ) +return -1 ; }/* Request a final '\0' be added to the snprintf() output.
Signature · 1 line
2.39.5
PATCH v3 10/17 tools/nolibc/printf: Use goto and reduce indentation
In reply to [email protected]From: David Laight <[email protected]>
Upcoming changes will need to use goto to jump to the code that outputs characters.
Use 'goto do_output' to output a known number of characters. Use 'goto do_strlen_output' to output a '\0' terminated string.
Removes a level of indentation from the format processing code.
---
New patch for v3.
Makes the final code look better and there is less to change if done early.
tools/include/nolibc/stdio.h | 168 +++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 77 deletions(-)
tools/include/nolibc/stdio.h +91 -77
@@ -329,99 +329,113 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list fmt++;/* Output characters from the format string. */ len = fmt - outstr; - }else { -/* we're in a format sequence */ + goto do_output; + } - ch = *fmt++; +/* we're in a format sequence */ -/* width */ -while (ch >='0' && ch <='9' ) { - width *=10 ; - width += ch -'0' ; + ch = *fmt++; - ch = *fmt++; - } +/* width */ +while (ch >='0' && ch <='9' ) { + width *=10 ; + width += ch -'0' ; + + ch = *fmt++; + } -/* Length modifiers */ +/* Length modifiers */ +if (ch =='l' ) { + lpref =1 ; + ch = *fmt++;if (ch =='l' ) { - lpref =1 ; - ch = *fmt++; -if (ch =='l' ) { - lpref =2 ; - ch = *fmt++; - } - }else if (ch =='j' ) { -/* intmax_t is long long */ lpref =2 ; ch = *fmt++; - }else { - lpref =0 ; } + }else if (ch =='j' ) { +/* intmax_t is long long */ + lpref =2 ; + ch = *fmt++; + }else { + lpref =0 ; + } -if (ch =='c' || ch =='d' || ch =='u' || ch =='x' || ch =='p' ) { -char *out = outbuf; +if (ch =='c' || ch =='d' || ch =='u' || ch =='x' || ch =='p' ) { +char *out = outbuf; -if (ch =='p' ) +if (ch =='p' ) + v = va_arg(args, unsigned long); +else if (lpref) { +if (lpref >1 ) + v = va_arg(args, unsigned long long); +else v = va_arg(args, unsigned long); -else if (lpref) { -if (lpref >1 ) - v = va_arg(args, unsigned long long); -else - v = va_arg(args, unsigned long); - }else - v = va_arg(args, unsigned int); - -if (ch =='d' ) { -/* sign-extend the value */ -if (lpref ==0 ) - v = (long long )(int )v; -else if (lpref ==1 ) - v = (long long )(long )v; - } + }else + v = va_arg(args, unsigned int); -switch (ch) { -case 'c' : - out[0 ] = v; - out[1 ] =0 ; -break ; -case 'd' : -i64toa_r (v, out); -break ; -case 'u' : -u64toa_r (v, out); -break ; -case 'p' : - *(out++) ='0' ; - *(out++) ='x' ; - __nolibc_fallthrough; -default :/* 'x' and 'p' above */ -u64toh_r (v, out); -break ; - } - outstr = outbuf; - } -else if (ch =='s' ) { - outstr = va_arg(args, char *); -if (!outstr) - outstr="(null)" ; +if (ch =='d' ) { +/* sign-extend the value */ +if (lpref ==0 ) + v = (long long )(int )v; +else if (lpref ==1 ) + v = (long long )(long )v; } -else if (ch =='m' ) { - outstr =strerror (errno); - }else { -if (ch !='%' ) { -/* Invalid format: back up to output the format characters */ - fmt = outstr +1 ; -/* and output a '%' now. */ - } -/* %% is documented as a 'conversion specifier'. -* Any flags, precision or length modifier are ignored. -*/ - width =0 ; - outstr ="%" ; + +switch (ch) { +case 'c' : + out[0 ] = v; + out[1 ] =0 ; +break ; +case 'd' : +i64toa_r (v, out); +break ; +case 'u' : +u64toa_r (v, out); +break ; +case 'p' : + *(out++) ='0' ; + *(out++) ='x' ; + __nolibc_fallthrough; +default :/* 'x' and 'p' above */ +u64toh_r (v, out); +break ; } - len =strlen (outstr); + outstr = outbuf; + goto do_strlen_output; } +if (ch =='s' ) { + outstr = va_arg(args, char *); +if (!outstr) + outstr="(null)" ; + goto do_strlen_output; + } + +if (ch =='m' ) { + outstr =strerror (errno); + goto do_strlen_output; + } + +if (ch !='%' ) { +/* Invalid format: back up to output the format characters */ + fmt = outstr +1 ; +/* and output a '%' now. */ + } +/* %% is documented as a 'conversion specifier'. +* Any flags, precision or length modifier are ignored. +*/ + len =1 ; + width =0 ; + outstr = fmt -1 ; + goto do_output; + +do_strlen_output: +/* Open coded strlen() (slightly smaller). */ +for (len =0 ;; len++) +if (!outstr[len]) +break ; + +do_output: written += len; width -= len;
Signature · 1 line
2.39.5
PATCH v3 11/17 tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion chars
In reply to [email protected]From: David Laight <[email protected]>
Use flags bits (1u << (ch & 31)) for the flags, length modifiers, and conversion specifiers.
This makes it easy to test for multiple values at once.
Detect the conversion flags " #+-0" although they are currently all ignored.
Add support for length modifiers 't' and 'z' (both long) and 'q' and 'L' (both long long).
Add support for "%i" (the same as %d") and "%X" (treated at "%x").
Unconditionally generate the signed values (for %d) to remove a second set of checks for the size.
Separate out the formatting of single characters from numbers. Output the sign for negative values then negate and treat as unsigned.
Change/add tests to use conversions i and X, and length modifiers L and ll. Use the correct minimum value for "%Li".
---
Changes for v3:
- Patch 6 in v2.
- Move all the variable definitions to the top of the function. The loop body is a bit long to hide definitions at its top.
- Avoid -Wtype-limits validating format characters.
- Include changes to the selftests.
Changes for v2:
- Use #defines to make the code a lot more readable.
- Include the changes from the old patch 10 that used masks for the conversion specifiers.
- Detect all the valid flag characters even though they are not implemented.
- Support for left justifying field is moved to patch 7.
tools/include/nolibc/stdio.h | 162 +++++++++++++------ tools/testing/selftests/nolibc/nolibc-test.c | 14 +- 2 files changed, 124 insertions(+), 52 deletions(-)
tools/include/nolibc/stdio.h +113 -49
@@ -291,10 +291,15 @@ int fseek(FILE *stream, long offset, int whence) } -/* minimal printf(). It supports the following formats: -* - %[l*]{d,u,c,x,p} -* - %s -* - unknown modifiers are ignored. +/* printf(). Supports most of the normal integer and string formats. +* - %[#-+ 0][width][{l,t,z,ll,L,j,q}]{c,d,i,u,x,X,p,s,m,%} +* - %% generates a single % +* - %m outputs strerror(errno). +* - %X outputs a..f the same as %x. +* - The modifiers [#-+ 0] are currently ignored. +* - No support for precision or variable widths. +* - No support for floating point or wide characters. +* - Invalid formats are copied to the output buffer. * * Called by vfprintf() and snprintf() to do the actual formatting. * The callers provide a callback function to save the formatted data.
@@ -305,15 +310,43 @@ int fseek(FILE *stream, long offset, int whence)* - with (NULL, 0) at the end of the __nolibc_printf. * If the callback returns non-zero __nolibc_printf() immediately returns -1. */ +typedef int (*__nolibc_printf_cb )(void *state,const char *buf,size_t size); +/* This code uses 'flag' variables that are indexed by the low 6 bits +* of characters to optimise checks for multiple characters. +* +* _NOLIBC_PF_FLAGS_CONTAIN(flags, 'a', 'b'. ...) +* returns non-zero if the bit for any of the specified characters is set. +* +* _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'a', 'b'. ...) +* returns the flag bit for ch if it is one of the specified characters. +* All the characters must be in the same 32 character block (non-alphabetic, +* upper case, or lower case) of the ASCII character set. +*/ +#define _NOLIBC_PF_FLAG (ch) (1u << ((ch) & 0x1f)) +#define _NOLIBC_PF_FLAG_NZ (ch) ((ch) ? _NOLIBC_PF_FLAG(ch) : 0) +#define _NOLIBC_PF_FLAG8 (cmp_1, cmp_2, cmp_3, cmp_4, cmp_5, cmp_6, cmp_7, cmp_8, ...) \ + (_NOLIBC_PF_FLAG_NZ(cmp_1) | _NOLIBC_PF_FLAG_NZ(cmp_2) | \ + _NOLIBC_PF_FLAG_NZ(cmp_3) | _NOLIBC_PF_FLAG_NZ(cmp_4) | \ + _NOLIBC_PF_FLAG_NZ(cmp_5) | _NOLIBC_PF_FLAG_NZ(cmp_6) | \ + _NOLIBC_PF_FLAG_NZ(cmp_7) | _NOLIBC_PF_FLAG_NZ(cmp_8)) +#define _NOLIBC_PF_FLAGS_CONTAIN (flags, ...) \ + ((flags) & _NOLIBC_PF_FLAG8(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0)) +#define _NOLIBC_PF_CHAR_IS_ONE_OF (ch, cmp_1, ...) \ + ((unsigned int)(ch) - (cmp_1 & 0xe0) > 0x1f ? 0 : \ + _NOLIBC_PF_FLAGS_CONTAIN(_NOLIBC_PF_FLAG(ch), cmp_1, __VA_ARGS__)) +static __attribute__((unused,format (printf,3 ,0 )))int __nolibc_printf (__nolibc_printf_cb cb,void *state,const char *fmt,va_list args) { -char lpref, ch; +char ch;unsigned long long v; +long long signed_v;int written, width, len; +unsigned int flags, ch_flag;char outbuf[21 ]; +char *out;const char *outstr; written =0 ;
@@ -324,6 +357,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_listbreak ; width =0 ; + flags =0 ;if (ch !='%' ) {while (*fmt && *fmt !='%' ) fmt++;
@@ -334,7 +368,14 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list/* we're in a format sequence */ - ch = *fmt++; +/* Conversion flag characters */ +while (1 ) { + ch = *fmt++; + ch_flag =_NOLIBC_PF_CHAR_IS_ONE_OF (ch,' ' ,'#' ,'+' ,'-' ,'0' ); +if (!ch_flag) +break ; + flags |= ch_flag; + }/* width */ while (ch >='0' && ch <='9' ) {
@@ -344,62 +385,82 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list ch = *fmt++; } -/* Length modifiers */ -if (ch =='l' ) { - lpref =1 ; - ch = *fmt++; -if (ch =='l' ) { - lpref =2 ; - ch = *fmt++; +/* Length modifier. +* They miss the conversion flags characters " #+-0" so can go into flags. +* Change both L and ll to q. +*/ +if (ch =='L' ) + ch ='q' ; + ch_flag =_NOLIBC_PF_CHAR_IS_ONE_OF (ch,'l' ,'t' ,'z' ,'j' ,'q' ); +if (ch_flag !=0 ) { +if (ch =='l' && fmt[0 ] =='l' ) { + fmt++; + ch_flag =_NOLIBC_PF_FLAG ('q' ); } - }else if (ch =='j' ) { -/* intmax_t is long long */ - lpref =2 ; + flags |= ch_flag; ch = *fmt++; - }else { - lpref =0 ; } -if (ch =='c' || ch =='d' || ch =='u' || ch =='x' || ch =='p' ) { -char *out = outbuf; +/* Conversion specifiers. */ -if (ch =='p' ) +/* Numeric and pointer conversion specifiers. +* +* Use an explicit bound check (rather than _NOLIBC_PF_CHAR_IS_ONE_OF()) +* so that 'X' can be allowed through. +* 'X' gets treated and 'x' because _NOLIBC_PF_FLAG() returns the same +* value for both. +*/ + ch_flag =_NOLIBC_PF_FLAG (ch); +if (((ch >='a' && ch <='z' ) || ch =='X' ) && +_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'c' ,'d' ,'i' ,'u' ,'x' ,'p' )) { +/* 'long' is needed for pointer conversions and ltz lengths. +* A single test can be used provided 'p' (the same bit as '0') +* is masked from flags. +*/ +if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag | (flags & ~_NOLIBC_PF_FLAG ('p' )), +'p' ,'l' ,'t' ,'z' )) { v = va_arg(args, unsigned long); -else if (lpref) { -if (lpref >1 ) - v = va_arg(args, unsigned long long); -else - v = va_arg(args, unsigned long); - }else + signed_v = (long )v; + }else if (_NOLIBC_PF_FLAGS_CONTAIN (flags,'j' ,'q' )) { + v = va_arg(args, unsigned long long); + signed_v = v; + }else { v = va_arg(args, unsigned int); + signed_v = (int )v; + } -if (ch =='d' ) { -/* sign-extend the value */ -if (lpref ==0 ) - v = (long long )(int )v; -else if (lpref ==1 ) - v = (long long )(long )v; +if (ch =='c' ) { +/* "%c" - single character. */ + outbuf[0 ] = v; + len =1 ; + outstr = outbuf; + goto do_output; } -switch (ch) { -case 'c' : - out[0 ] = v; - out[1 ] =0 ; -break ; -case 'd' : -i64toa_r (v, out); -break ; -case 'u' : + out = outbuf; + +if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'d' ,'i' )) { +/* "%d" and "%i" - signed decimal numbers. */ +if (signed_v <0 ) { + *out++ ='-' ; + v = -(signed_v +1 ); + v++; + } + } + +/* Convert the number to ascii in the required base. */ +if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'d' ,'i' ,'u' )) { +/* Base 10 */ u64toa_r (v, out); -break ; -case 'p' : - *(out++) ='0' ; - *(out++) ='x' ; - __nolibc_fallthrough; -default :/* 'x' and 'p' above */ + }else { +/* Base 16 */ +if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'p' )) { + *(out++) ='0' ; + *(out++) ='x' ; + }u64toh_r (v, out); -break ; } + outstr = outbuf; goto do_strlen_output; }
@@ -438,6 +499,9 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list do_output: written += len; +/* Stop gcc back-merging this code into one of the conditionals above. */ +_NOLIBC_OPTIMIZER_HIDE_VAR (len); + width -= len;while (width >0 ) {/* Output pad in 16 byte blocks with the small block first. */
tools/testing/selftests/nolibc/nolibc-test.c +11 -3
@@ -1833,11 +1833,19 @@ static int run_printf(int min, int max)CASE_TEST (number);EXPECT_VFPRINTF (1 ,"1234" ,"%d" ,1234 );break ;CASE_TEST (negnumber);EXPECT_VFPRINTF (1 ,"-1234" ,"%d" ,-1234 );break ;CASE_TEST (unsigned);EXPECT_VFPRINTF (1 ,"12345" ,"%u" ,12345 );break ; +CASE_TEST (signed_max);EXPECT_VFPRINTF (1 ,"2147483647" ,"%i" , ~0u >>1 );break ; +CASE_TEST (signed_min);EXPECT_VFPRINTF (1 ,"-2147483648" ,"%i" , (~0u >>1 ) +1 );break ; +CASE_TEST (unsigned_max);EXPECT_VFPRINTF (1 ,"4294967295" ,"%u" , ~0u );break ;CASE_TEST (char);EXPECT_VFPRINTF (1 ,"c" ,"%c" ,'c' );break ; -CASE_TEST (hex);EXPECT_VFPRINTF (1 ,"f" ,"%x" ,0xf );break ; +CASE_TEST (hex_nolibc);EXPECT_VFPRINTF (is_nolibc,"|f|d|" ,"|%x|%X|" ,0xf ,0xd );break ; +CASE_TEST (hex_libc);EXPECT_VFPRINTF (!is_nolibc,"|f|D|" ,"|%x|%X|" ,0xf ,0xd );break ;CASE_TEST (pointer);EXPECT_VFPRINTF (1 ,"0x1" ,"%p" , (void *)0x1 );break ; -CASE_TEST (uintmax_t);EXPECT_VFPRINTF (1 ,"18446744073709551615" ,"%ju" ,0xffffffffffffffffULL );break ; -CASE_TEST (intmax_t);EXPECT_VFPRINTF (1 ,"-9223372036854775807" ,"%jd" ,0x8000000000000001LL );break ; +CASE_TEST (percent);EXPECT_VFPRINTF (1 ,"a%d42%69%" ,"a%%d%d%%%d%%" ,42 ,69 );break ; +CASE_TEST (perc_qual);EXPECT_VFPRINTF (1 ,"a%d2" ,"a%-14l%d%d" ,2 );break ; +CASE_TEST (invalid);EXPECT_VFPRINTF (1 ,"a%12yx3%y42%P" ,"a%12yx%d%y%d%P" ,3 ,42 );break ; +CASE_TEST (intmax_max);EXPECT_VFPRINTF (1 ,"9223372036854775807" ,"%lld" , ~0ULL >>1 );break ; +CASE_TEST (intmax_min);EXPECT_VFPRINTF (1 ,"-9223372036854775808" ,"%Li" , (~0ULL >>1 ) +1 );break ; +CASE_TEST (uintmax_max);EXPECT_VFPRINTF (1 ,"18446744073709551615" ,"%ju" , ~0ULL );break ;CASE_TEST (truncation);EXPECT_VFPRINTF (1 ,"012345678901234567890123456789" ,"%s" ,"012345678901234567890123456789" );break ;CASE_TEST (string_width);EXPECT_VFPRINTF (1 ," 1" ,"%10s" ,"1" );break ;CASE_TEST (number_width);EXPECT_VFPRINTF (1 ," 1" ,"%10d" ,1 );break ;
Signature · 1 line
2.39.5
PATCH v3 12/17 tools/nolibc/printf: Handle "%s" with the numeric formats
In reply to [email protected]From: David Laight <[email protected]>
Avoids the extra va_arg() call with is non-trivial on a lot of modern ABI.
---
Changes for v3:
- Moved to its own patch (part of patch 7 in v2)..
- Fix 32bit compile.
tools/include/nolibc/stdio.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
tools/include/nolibc/stdio.h +12 -10
@@ -412,13 +412,13 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list*/ ch_flag =_NOLIBC_PF_FLAG (ch);if (((ch >='a' && ch <='z' ) || ch =='X' ) && -_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'c' ,'d' ,'i' ,'u' ,'x' ,'p' )) { -/* 'long' is needed for pointer conversions and ltz lengths. +_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'c' ,'d' ,'i' ,'u' ,'x' ,'p' ,'s' )) { +/* 'long' is needed for pointer/string conversions and ltz lengths. * A single test can be used provided 'p' (the same bit as '0') * is masked from flags. */ if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag | (flags & ~_NOLIBC_PF_FLAG ('p' )), -'p' ,'l' ,'t' ,'z' )) { +'p' ,'s' ,'l' ,'t' ,'z' )) { v = va_arg(args, unsigned long); signed_v = (long )v; }else if (_NOLIBC_PF_FLAGS_CONTAIN (flags,'j' ,'q' )) {
@@ -437,6 +437,15 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list goto do_output; } +if (ch =='s' ) { +/* "%s" - character string. */ + outstr = (const char *)(uintptr_t )v; +if (!outstr) { + outstr ="(null)" ; + } + goto do_strlen_output; + } + out = outbuf;if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'d' ,'i' )) {
@@ -465,13 +474,6 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list goto do_strlen_output; } -if (ch =='s' ) { - outstr = va_arg(args, char *); -if (!outstr) - outstr="(null)" ; - goto do_strlen_output; - } -if (ch =='m' ) { outstr =strerror (errno); goto do_strlen_output;
Signature · 1 line
2.39.5
PATCH v3 13/17 tools/nolibc/printf: Add support for conversion flags
In reply to [email protected]From: David Laight <[email protected]>
Add support for:
'+' and ' ' Sign characters for non-negative numbers. '#' adds 0x to hex numbers.
outputting "(nil)" for NULL pointers.
Special code for 0 is needed because "%#x" needs to generate "0" not "0x0".
The code is changed to use the length returned by u64toh_r() and prepend the sign (or "0x") after conversion to reduce the changes for the next patch that inserts zero padding between the sign and digits.
Add tests for "%#x", "% d", "%+d" and passing NULL to "%p".
---
Changes for v3:
- Was patch 7 in v2.
- Changes for left alignment moved to the next patch.
- Rename 'sign' 'sign_prefix'.
- Include tests
Changes for v2:
- Add support for left justifying fields (removed from patch 6).
- Merge in the changes from the old patch 8.
- Add support for "%#x" (formally part of patch 9).
tools/include/nolibc/stdio.h | 68 +++++++++++++++----- tools/testing/selftests/nolibc/nolibc-test.c | 3 + 2 files changed, 55 insertions(+), 16 deletions(-)
tools/include/nolibc/stdio.h +52 -16
@@ -296,7 +296,7 @@ int fseek(FILE *stream, long offset, int whence)* - %% generates a single % * - %m outputs strerror(errno). * - %X outputs a..f the same as %x. -* - The modifiers [#-+ 0] are currently ignored. +* - The modifiers [-0] are currently ignored. * - No support for precision or variable widths. * - No support for floating point or wide characters. * - Invalid formats are copied to the output buffer.
@@ -345,9 +345,10 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_listlong long signed_v;int written, width, len;unsigned int flags, ch_flag; -char outbuf[21 ]; +char outbuf[2 +22 +1 ];char *out;const char *outstr; +unsigned int sign_prefix; written =0 ;while (1 ) {
@@ -409,8 +410,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list* so that 'X' can be allowed through. * 'X' gets treated and 'x' because _NOLIBC_PF_FLAG() returns the same * value for both. +* +* We need to check for "%p" or "%#x" later, merging here gives better code. +* But '#' collides with 'c' so shift right. */ - ch_flag =_NOLIBC_PF_FLAG (ch); + ch_flag =_NOLIBC_PF_FLAG (ch) | (flags &_NOLIBC_PF_FLAG ('#' )) >>1 ;if (((ch >='a' && ch <='z' ) || ch =='X' ) &&_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'c' ,'d' ,'i' ,'u' ,'x' ,'p' ,'s' )) {/* 'long' is needed for pointer/string conversions and ltz lengths.
@@ -446,32 +450,64 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list goto do_strlen_output; } - out = outbuf; +/* The 'sign_prefix' can be zero, one or two ("0x") characters. */ + sign_prefix =0 ;if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'d' ,'i' )) {/* "%d" and "%i" - signed decimal numbers. */ if (signed_v <0 ) { - *out++ ='-' ; + sign_prefix ='-' ; v = -(signed_v +1 ); v++; + }else if (_NOLIBC_PF_FLAGS_CONTAIN (flags,'+' )) { + sign_prefix ='+' ; + }else if (_NOLIBC_PF_FLAGS_CONTAIN (flags,' ' )) { + sign_prefix =' ' ; } } -/* Convert the number to ascii in the required base. */ -if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'d' ,'i' ,'u' )) { -/* Base 10 */ -u64toa_r (v, out); - }else { -/* Base 16 */ +/* The value is converted offset into the buffer so that +* the sign/prefix can be added in front. +* The longest digit string is 22 + 1 for octal conversions, the +* space is reserved even though octal isn't currently supported. +*/ + out = outbuf +2 ; + +if (v ==0 ) { +/* There are special rules for zero. */ if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'p' )) { - *(out++) ='0' ; - *(out++) ='x' ; +/* "%p" match glibc, precision is ignored */ + outstr ="(nil)" ; + len =5 ; + goto do_output; + } +/* All other formats (including "%#x") just output "0". */ + out[0 ] ='0' ; + len =1 ; + }else { +/* Convert the number to ascii in the required base. */ +if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'d' ,'i' ,'u' )) { +/* Base 10 */ + len =u64toa_r (v, out); + }else { +/* Base 16 */ +if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'p' ,'#' -1 )) { +/* "%p" and "%#x" need "0x" prepending. */ + sign_prefix ='x' |'0' <<8 ; + } + len =u64toh_r (v, out); } -u64toh_r (v, out); } - outstr = outbuf; - goto do_strlen_output; +/* Add the 0, 1 or 2 ("0x") sign/prefix characters at the front. */ +for (; sign_prefix; sign_prefix >>=8 ) { +/* Force gcc to increment len inside the loop. */ +_NOLIBC_OPTIMIZER_HIDE_VAR (len); + len++; + *--out = sign_prefix; + } + outstr = out; + goto do_output; }if (ch =='m' ) {
tools/testing/selftests/nolibc/nolibc-test.c +3 -0
@@ -1832,6 +1832,7 @@ static int run_printf(int min, int max)CASE_TEST (string);EXPECT_VFPRINTF (1 ,"foo" ,"%s" ,"foo" );break ;CASE_TEST (number);EXPECT_VFPRINTF (1 ,"1234" ,"%d" ,1234 );break ;CASE_TEST (negnumber);EXPECT_VFPRINTF (1 ,"-1234" ,"%d" ,-1234 );break ; +CASE_TEST (num_sign);EXPECT_VFPRINTF (1 ,"| 1|+2|+3|+4|5|" ,"|% d|%+d|% +d|%+ d|%#d|" ,1 ,2 ,3 ,4 ,5 );break ;CASE_TEST (unsigned);EXPECT_VFPRINTF (1 ,"12345" ,"%u" ,12345 );break ;CASE_TEST (signed_max);EXPECT_VFPRINTF (1 ,"2147483647" ,"%i" , ~0u >>1 );break ;CASE_TEST (signed_min);EXPECT_VFPRINTF (1 ,"-2147483648" ,"%i" , (~0u >>1 ) +1 );break ;
@@ -1840,6 +1841,7 @@ static int run_printf(int min, int max)CASE_TEST (hex_nolibc);EXPECT_VFPRINTF (is_nolibc,"|f|d|" ,"|%x|%X|" ,0xf ,0xd );break ;CASE_TEST (hex_libc);EXPECT_VFPRINTF (!is_nolibc,"|f|D|" ,"|%x|%X|" ,0xf ,0xd );break ;CASE_TEST (pointer);EXPECT_VFPRINTF (1 ,"0x1" ,"%p" , (void *)0x1 );break ; +CASE_TEST (pointer_NULL);EXPECT_VFPRINTF (1 ,"(nil)" ,"%p" , (void *)0 );break ;CASE_TEST (percent);EXPECT_VFPRINTF (1 ,"a%d42%69%" ,"a%%d%d%%%d%%" ,42 ,69 );break ;CASE_TEST (perc_qual);EXPECT_VFPRINTF (1 ,"a%d2" ,"a%-14l%d%d" ,2 );break ;CASE_TEST (invalid);EXPECT_VFPRINTF (1 ,"a%12yx3%y42%P" ,"a%12yx%d%y%d%P" ,3 ,42 );break ;
@@ -1850,6 +1852,7 @@ static int run_printf(int min, int max)CASE_TEST (string_width);EXPECT_VFPRINTF (1 ," 1" ,"%10s" ,"1" );break ;CASE_TEST (number_width);EXPECT_VFPRINTF (1 ," 1" ,"%10d" ,1 );break ;CASE_TEST (width_trunc);EXPECT_VFPRINTF (1 ," 1" ,"%30d" ,1 );break ; +CASE_TEST (hex_alt);EXPECT_VFPRINTF (1 ,"|0x1| 0x2| 0|" ,"|%#x|%#5x|%#5x|" ,1 ,2 ,0 );break ;CASE_TEST (errno);EXPECT_VFPRINTF (is_nolibc,"22:errno=22" ,"%d:%m" , errno=22 );break ;CASE_TEST (errno-neg);EXPECT_VFPRINTF (is_nolibc,"-22: errno=-22" ,"%d:%12m" , errno=-22 );break ;CASE_TEST (scanf);EXPECT_ZR (1 ,test_scanf ());break ;
Signature · 1 line
2.39.5
PATCH v3 14/17 tools/nolibc/printf: Add support for left aligning fields
In reply to [email protected]From: David Laight <[email protected]>
Output the characters before or after the pad - writing the pad takes more code.
Include additional/changed tests
---
Changes for v3:
- Formally part of patch 7.
tools/include/nolibc/stdio.h | 6 +++++- tools/testing/selftests/nolibc/nolibc-test.c | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-)
tools/include/nolibc/stdio.h +5 -1
@@ -540,7 +540,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list/* Stop gcc back-merging this code into one of the conditionals above. */ _NOLIBC_OPTIMIZER_HIDE_VAR (len); +/* Output the characters on the required side of any padding. */ width -= len; + flags =_NOLIBC_PF_FLAGS_CONTAIN (flags,'-' ); +if (flags &&cb (state, outstr, len) !=0 ) +return -1 ;while (width >0 ) {/* Output pad in 16 byte blocks with the small block first. */ int pad_len = ((width -1 ) &15 ) +1 ;
@@ -549,7 +553,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_listif (cb (state," " , pad_len) !=0 )return -1 ; } -if (cb (state, outstr, len) !=0 ) +if (!flags &&cb (state, outstr, len) !=0 )return -1 ; }
tools/testing/selftests/nolibc/nolibc-test.c +3 -1
@@ -1851,9 +1851,11 @@ static int run_printf(int min, int max)CASE_TEST (truncation);EXPECT_VFPRINTF (1 ,"012345678901234567890123456789" ,"%s" ,"012345678901234567890123456789" );break ;CASE_TEST (string_width);EXPECT_VFPRINTF (1 ," 1" ,"%10s" ,"1" );break ;CASE_TEST (number_width);EXPECT_VFPRINTF (1 ," 1" ,"%10d" ,1 );break ; +CASE_TEST (number_left);EXPECT_VFPRINTF (1 ,"|-5 |" ,"|%-8d|" ,-5 );break ; +CASE_TEST (string_align);EXPECT_VFPRINTF (1 ,"|foo |" ,"|%-8s|" ,"foo" );break ;CASE_TEST (width_trunc);EXPECT_VFPRINTF (1 ," 1" ,"%30d" ,1 );break ;CASE_TEST (hex_alt);EXPECT_VFPRINTF (1 ,"|0x1| 0x2| 0|" ,"|%#x|%#5x|%#5x|" ,1 ,2 ,0 );break ; -CASE_TEST (errno);EXPECT_VFPRINTF (is_nolibc,"22:errno=22" ,"%d:%m" , errno=22 );break ; +CASE_TEST (errno);EXPECT_VFPRINTF (is_nolibc,"22:errno=22 " ,"%d:%-12m" , errno=22 );break ;CASE_TEST (errno-neg);EXPECT_VFPRINTF (is_nolibc,"-22: errno=-22" ,"%d:%12m" , errno=-22 );break ;CASE_TEST (scanf);EXPECT_ZR (1 ,test_scanf ());break ;CASE_TEST (printf_error);EXPECT_ZR (1 ,test_printf_error ());break ;
Signature · 1 line
2.39.5
PATCH v3 15/17 tools/nolibc/printf: Add support for zero padding and field precision
In reply to [email protected]From: David Laight <[email protected]>
Includes support for variable field widths (eg "%*.*d").
Zero padding is limited to 31 zero characters.
This is wider than the largest numeric field so shouldn't be a problem.
All the standard printf formats are now supported except octal and floating point.
Add tests for new features
---
Changes for v3:
- Formerly patch 8
- Extra comments.
- Adjust offsets in outbuf[], support adding 31 '0' digits while still allowing enough room for octal support to be added and keeping outbuf[] at a nice round 56 bytes.
Changes for v2:
- These changes were previously in patch 9.
However you need to apply the old patch 10 to get anything like the same source. The files then more of less match apart from 'c' being renamed 'ch' and the 'magic' #defines.
tools/include/nolibc/stdio.h | 77 ++++++++++++++++---- tools/testing/selftests/nolibc/nolibc-test.c | 18 ++++- 2 files changed, 79 insertions(+), 16 deletions(-)
tools/include/nolibc/stdio.h +62 -15
@@ -292,12 +292,10 @@ int fseek(FILE *stream, long offset, int whence)/* printf(). Supports most of the normal integer and string formats. -* - %[#-+ 0][width][{l,t,z,ll,L,j,q}]{c,d,i,u,x,X,p,s,m,%} +* - %[#0-+ ][width|*[.precision|*}][{l,t,z,ll,L,j,q}]{c,d,i,u,x,X,p,s,m,%} * - %% generates a single % * - %m outputs strerror(errno). * - %X outputs a..f the same as %x. -* - The modifiers [-0] are currently ignored. -* - No support for precision or variable widths. * - No support for floating point or wide characters. * - Invalid formats are copied to the output buffer. *
@@ -343,9 +341,9 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_listchar ch;unsigned long long v;long long signed_v; -int written, width, len; +int written, width, precision, len;unsigned int flags, ch_flag; -char outbuf[2 +22 +1 ]; +char outbuf[2 +31 +22 +1 ];char *out;const char *outstr;unsigned int sign_prefix;
@@ -378,12 +376,24 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list flags |= ch_flag; } -/* width */ -while (ch >='0' && ch <='9' ) { - width *=10 ; - width += ch -'0' ; - - ch = *fmt++; +/* Width and precision */ +for (;; ch = *fmt++) { +if (ch =='*' ) { + precision = va_arg(args, unsigned int); + ch = *fmt++; + }else { +for (precision =0 ; ch >='0' && ch <='9' ; ch = *fmt++) + precision = precision *10 + (ch -'0' ); + } +if (_NOLIBC_PF_FLAGS_CONTAIN (flags,'.' )) +break ; + width = precision; +if (ch !='.' ) { +/* Default precision for strings */ + precision =INT_MAX ; +break ; + } + flags |=_NOLIBC_PF_FLAG ('.' ); }/* Length modifier.
@@ -446,6 +456,9 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list outstr = (const char *)(uintptr_t )v;if (!outstr) { outstr ="(null)" ; +/* Match glibc, nothing output if precision too small */ + len = precision >=6 ?6 :0 ; + goto do_output; } goto do_strlen_output; }
@@ -467,11 +480,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list }/* The value is converted offset into the buffer so that -* the sign/prefix can be added in front. +* 31 zero pad characters and the sign/prefix can be added in front. * The longest digit string is 22 + 1 for octal conversions, the * space is reserved even though octal isn't currently supported. */ - out = outbuf +2 ; + out = outbuf +2 +31 ;if (v ==0 ) {/* There are special rules for zero. */
@@ -481,6 +494,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list len =5 ; goto do_output; } +if (!precision) { +/* Explicit %nn.0d, no digits output */ + len =0 ; + goto prepend_sign; + }/* All other formats (including "%#x") just output "0". */ out[0 ] ='0' ; len =1 ;
@@ -499,6 +517,35 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list } } +/* Add zero padding */ +if (_NOLIBC_PF_FLAGS_CONTAIN (flags,'0' ,'.' )) { +if (!_NOLIBC_PF_FLAGS_CONTAIN (flags,'.' )) { +if (_NOLIBC_PF_FLAGS_CONTAIN (flags,'-' )) +/* Left justify overrides zero pad */ + goto prepend_sign; +/* eg "%05d", Zero pad to field width less sign. +* Note that precision can end up negative so all +* the variables have to be 'signed int'. +*/ + precision = width; +if (sign_prefix) { + precision--; +if (sign_prefix >=256 ) + precision--; + } + } +if (precision >31 ) +/* Don't run off the start of outbuf[], arbitrary limit +* longer than the longest number field. */ + precision =31 ; +for (; len < precision; len++) { +/* Stop gcc generating horrid code and memset(). */ +_NOLIBC_OPTIMIZER_HIDE_VAR (len); + *--out ='0' ; + } + } + +prepend_sign:/* Add the 0, 1 or 2 ("0x") sign/prefix characters at the front. */ for (; sign_prefix; sign_prefix >>=8 ) {/* Force gcc to increment len inside the loop. */
@@ -529,8 +576,8 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list goto do_output; do_strlen_output: -/* Open coded strlen() (slightly smaller). */ -for (len =0 ;; len++) +/* Open coded strnlen() (slightly smaller). */ +for (len =0 ; len < precision; len++)if (!outstr[len])break ;
tools/testing/selftests/nolibc/nolibc-test.c +17 -1
@@ -1838,10 +1838,12 @@ static int run_printf(int min, int max)CASE_TEST (signed_min);EXPECT_VFPRINTF (1 ,"-2147483648" ,"%i" , (~0u >>1 ) +1 );break ;CASE_TEST (unsigned_max);EXPECT_VFPRINTF (1 ,"4294967295" ,"%u" , ~0u );break ;CASE_TEST (char);EXPECT_VFPRINTF (1 ,"c" ,"%c" ,'c' );break ; +CASE_TEST (char);EXPECT_VFPRINTF (1 ,"|c|d| e|" ,"|%c|%.0c|%4c|" ,'c' ,'d' ,'e' );break ;CASE_TEST (hex_nolibc);EXPECT_VFPRINTF (is_nolibc,"|f|d|" ,"|%x|%X|" ,0xf ,0xd );break ;CASE_TEST (hex_libc);EXPECT_VFPRINTF (!is_nolibc,"|f|D|" ,"|%x|%X|" ,0xf ,0xd );break ;CASE_TEST (pointer);EXPECT_VFPRINTF (1 ,"0x1" ,"%p" , (void *)0x1 );break ; -CASE_TEST (pointer_NULL);EXPECT_VFPRINTF (1 ,"(nil)" ,"%p" , (void *)0 );break ; +CASE_TEST (pointer_NULL);EXPECT_VFPRINTF (1 ,"|(nil)|(nil)|" ,"|%p|%.4p|" , (void *)0 , (void *)0 );break ; +CASE_TEST (string_NULL);EXPECT_VFPRINTF (1 ,"|(null)||(null)|" ,"|%s|%.5s|%.6s|" , (void *)0 , (void *)0 , (void *)0 );break ;CASE_TEST (percent);EXPECT_VFPRINTF (1 ,"a%d42%69%" ,"a%%d%d%%%d%%" ,42 ,69 );break ;CASE_TEST (perc_qual);EXPECT_VFPRINTF (1 ,"a%d2" ,"a%-14l%d%d" ,2 );break ;CASE_TEST (invalid);EXPECT_VFPRINTF (1 ,"a%12yx3%y42%P" ,"a%12yx%d%y%d%P" ,3 ,42 );break ;
@@ -1850,11 +1852,25 @@ static int run_printf(int min, int max)CASE_TEST (uintmax_max);EXPECT_VFPRINTF (1 ,"18446744073709551615" ,"%ju" , ~0ULL );break ;CASE_TEST (truncation);EXPECT_VFPRINTF (1 ,"012345678901234567890123456789" ,"%s" ,"012345678901234567890123456789" );break ;CASE_TEST (string_width);EXPECT_VFPRINTF (1 ," 1" ,"%10s" ,"1" );break ; +CASE_TEST (string_trunc);EXPECT_VFPRINTF (1 ," 12345" ,"%10.5s" ,"1234567890" );break ;CASE_TEST (number_width);EXPECT_VFPRINTF (1 ," 1" ,"%10d" ,1 );break ;CASE_TEST (number_left);EXPECT_VFPRINTF (1 ,"|-5 |" ,"|%-8d|" ,-5 );break ;CASE_TEST (string_align);EXPECT_VFPRINTF (1 ,"|foo |" ,"|%-8s|" ,"foo" );break ;CASE_TEST (width_trunc);EXPECT_VFPRINTF (1 ," 1" ,"%30d" ,1 );break ; +CASE_TEST (width_tr_lft);EXPECT_VFPRINTF (1 ,"1 " ,"%-30d" ,1 );break ; +CASE_TEST (number_pad);EXPECT_VFPRINTF (1 ,"0000000005" ,"%010d" ,5 );break ; +CASE_TEST (number_pad);EXPECT_VFPRINTF (1 ,"|0000000005|0x1234|" ,"|%010d|%#01x|" ,5 ,0x1234 );break ; +CASE_TEST (num_pad_neg);EXPECT_VFPRINTF (1 ,"-000000005" ,"%010d" ,-5 );break ; +CASE_TEST (num_pad_hex);EXPECT_VFPRINTF (1 ,"00fffffffb" ,"%010x" ,-5 );break ; +CASE_TEST (num_pad_trunc);EXPECT_VFPRINTF (is_nolibc," 0000000000000000000000000000005" ,"%035d" ,5 );break ;/* max 31 '0' can be added */ +CASE_TEST (num_p_tr_libc);EXPECT_VFPRINTF (!is_nolibc,"00000000000000000000000000000000005" ,"%035d" ,5 );break ; +CASE_TEST (number_prec);EXPECT_VFPRINTF (1 ," 00005" ,"%10.5d" ,5 );break ; +CASE_TEST (num_prec_neg);EXPECT_VFPRINTF (1 ," -00005" ,"%10.5d" ,-5 );break ; +CASE_TEST (num_prec_var);EXPECT_VFPRINTF (1 ," -00005" ,"%*.*d" ,10 ,5 ,-5 );break ; +CASE_TEST (num_0_prec_0);EXPECT_VFPRINTF (1 ,"|| |+||||" ,"|%.0d|% .0d|%+.0d|%.0u|%.0x|%#.0x|" ,0 ,0 ,0 ,0 ,0 ,0 );break ;CASE_TEST (hex_alt);EXPECT_VFPRINTF (1 ,"|0x1| 0x2| 0|" ,"|%#x|%#5x|%#5x|" ,1 ,2 ,0 );break ; +CASE_TEST (hex_alt_prec);EXPECT_VFPRINTF (1 ,"| 0x02|0x03| 0x123|" ,"|%#5.2x|%#04x|%#6.2x|" ,2 ,3 ,0x123 );break ; +CASE_TEST (hex_0_alt);EXPECT_VFPRINTF (1 ,"|0|0000| 00|" ,"|%#x|%#04x|%#5.2x|" ,0 ,0 ,0 );break ;CASE_TEST (errno);EXPECT_VFPRINTF (is_nolibc,"22:errno=22 " ,"%d:%-12m" , errno=22 );break ;CASE_TEST (errno-neg);EXPECT_VFPRINTF (is_nolibc,"-22: errno=-22" ,"%d:%12m" , errno=-22 );break ;CASE_TEST (scanf);EXPECT_ZR (1 ,test_scanf ());break ;
Signature · 1 line
2.39.5
PATCH v3 16/17 tools/nolibc/printf: Add support for octal output
In reply to [email protected]From: David Laight <[email protected]>
Octal output isn't often used, but adding it costs very little.
Supporting "%#o" is mildly annoying, it has to add a leading '0' if there isn't one present. In simple cases this is the same as adding a sign of '0' - but that adds an extra '0' in a few places.
So you need 3 tests, %o, # and no leading '0' (which can only be checked after the zero pad for precision).
If all the test are deferred until after zero padding then too many values are 'live' across the call to _nolibc_u64toa_base() and get spilled to stack. Hence the check that ignores the 'sign' if it is the same as the first character of the output string.
Add tests for octal output.
---
New patch for v3.
Relies on the changes to u64toa_r() etc in stdlib.h (patch 2).
tools/include/nolibc/stdio.h | 50 ++++++++++++++------ tools/testing/selftests/nolibc/nolibc-test.c | 3 ++ 2 files changed, 38 insertions(+), 15 deletions(-)
tools/include/nolibc/stdio.h +35 -15
@@ -292,7 +292,7 @@ int fseek(FILE *stream, long offset, int whence)/* printf(). Supports most of the normal integer and string formats. -* - %[#0-+ ][width|*[.precision|*}][{l,t,z,ll,L,j,q}]{c,d,i,u,x,X,p,s,m,%} +* - %[#0-+ ][width|*[.precision|*}][{l,t,z,ll,L,j,q}]{c,d,i,u,o,x,X,p,s,m,%} * - %% generates a single % * - %m outputs strerror(errno). * - %X outputs a..f the same as %x.
@@ -426,7 +426,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list*/ ch_flag =_NOLIBC_PF_FLAG (ch) | (flags &_NOLIBC_PF_FLAG ('#' )) >>1 ;if (((ch >='a' && ch <='z' ) || ch =='X' ) && -_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'c' ,'d' ,'i' ,'u' ,'x' ,'p' ,'s' )) { +_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'c' ,'d' ,'i' ,'u' ,'o' ,'x' ,'p' ,'s' )) {/* 'long' is needed for pointer/string conversions and ltz lengths. * A single test can be used provided 'p' (the same bit as '0') * is masked from flags.
@@ -477,12 +477,19 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list }else if (_NOLIBC_PF_FLAGS_CONTAIN (flags,' ' )) { sign_prefix =' ' ; } + }else { +/* "#o" requires that the output always starts with a '0'. +* This needs another check after any zero padding to avoid +* adding an extra leading '0'. +*/ +if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'o' ) && +_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'#' -1 )) + sign_prefix ='0' ; }/* The value is converted offset into the buffer so that * 31 zero pad characters and the sign/prefix can be added in front. -* The longest digit string is 22 + 1 for octal conversions, the -* space is reserved even though octal isn't currently supported. +* The longest digit string is 22 + 1 for octal conversions. */ out = outbuf +2 +31 ;
@@ -495,7 +502,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list goto do_output; }if (!precision) { -/* Explicit %nn.0d, no digits output */ +/* Explicit %nn.0d, no digits output (except for %#.0o) */ len =0 ; goto prepend_sign; }
@@ -504,17 +511,23 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list len =1 ; }else {/* Convert the number to ascii in the required base. */ +unsigned long long recip; +unsigned int base;if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'d' ,'i' ,'u' )) { -/* Base 10 */ - len =u64toa_r (v, out); + base =10 ; + recip =_NOLIBC_U64TOA_RECIP (10 ); + }else if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'o' )) { + base =8 ; + recip =_NOLIBC_U64TOA_RECIP (8 ); }else { -/* Base 16 */ + base =16 ; + recip =_NOLIBC_U64TOA_RECIP (16 );if (_NOLIBC_PF_FLAGS_CONTAIN (ch_flag,'p' ,'#' -1 )) {/* "%p" and "%#x" need "0x" prepending. */ sign_prefix ='x' |'0' <<8 ; } - len =u64toh_r (v, out); } + len =_nolibc_u64toa_base (v, out, base, recip); }/* Add zero padding */
@@ -545,13 +558,20 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list } } +/* %#o has set sign_prefix to '0', but we don't want so add an extra +* leading zero here. +* Since the only other byte values of sign_prefix are ' ', '+' and '-' +* it is enough to check that out[] doesn't already start with sign_prefix. +*/ +if (sign_prefix != *out) { prepend_sign: -/* Add the 0, 1 or 2 ("0x") sign/prefix characters at the front. */ -for (; sign_prefix; sign_prefix >>=8 ) { -/* Force gcc to increment len inside the loop. */ -_NOLIBC_OPTIMIZER_HIDE_VAR (len); - len++; - *--out = sign_prefix; +/* Add the 0, 1 or 2 ("0x") sign/prefix characters at the front. */ +for (; sign_prefix; sign_prefix >>=8 ) { +/* Force gcc to increment len inside the loop. */ +_NOLIBC_OPTIMIZER_HIDE_VAR (len); + len++; + *--out = sign_prefix; + } } outstr = out; goto do_output;
tools/testing/selftests/nolibc/nolibc-test.c +3 -0
@@ -1839,6 +1839,9 @@ static int run_printf(int min, int max)CASE_TEST (unsigned_max);EXPECT_VFPRINTF (1 ,"4294967295" ,"%u" , ~0u );break ;CASE_TEST (char);EXPECT_VFPRINTF (1 ,"c" ,"%c" ,'c' );break ;CASE_TEST (char);EXPECT_VFPRINTF (1 ,"|c|d| e|" ,"|%c|%.0c|%4c|" ,'c' ,'d' ,'e' );break ; +CASE_TEST (octal);EXPECT_VFPRINTF (1 ,"|17| 0033||" ,"|%o|%6.4o|%.0o|" ,017 ,033 ,0 );break ; +CASE_TEST (octal_max);EXPECT_VFPRINTF (1 ,"1777777777777777777777" ,"%llo" , ~0ULL );break ; +CASE_TEST (octal_alt);EXPECT_VFPRINTF (1 ,"|0|01|02|034|0|" ,"|%#o|%#o|%#02o|%#02o|%#.0o|" ,0 ,1 ,2 ,034 ,0 );break ;CASE_TEST (hex_nolibc);EXPECT_VFPRINTF (is_nolibc,"|f|d|" ,"|%x|%X|" ,0xf ,0xd );break ;CASE_TEST (hex_libc);EXPECT_VFPRINTF (!is_nolibc,"|f|D|" ,"|%x|%X|" ,0xf ,0xd );break ;CASE_TEST (pointer);EXPECT_VFPRINTF (1 ,"0x1" ,"%p" , (void *)0x1 );break ;
Signature · 1 line
2.39.5
PATCH v3 17/17 selftests/nolibc: Use printf variable field widths and precisions
In reply to [email protected]From: David Laight <[email protected]>
Now that printf supports '*' for field widths and precisions then can be used to simplify the test output.
- aligning the "[OK]" strings.
- reporting the expected sprintf() output when there is a mismatch.
---
Changes for v3:
- Change code in expect_vfprintf() (code added in v3).
tools/testing/selftests/nolibc/nolibc-test.c | 26 ++++---------------- 1 file changed, 5 insertions(+), 21 deletions(-)
tools/testing/selftests/nolibc/nolibc-test.c +5 -21
@@ -156,21 +156,6 @@ static const char *errorname(int err) } } -static void align_result (size_t llen) -{ -const size_t align =64 ; -char buf[align]; -size_t n; - -if (llen >= align) -return ; - - n = align - llen; -memset (buf,' ' , n); - buf[n] ='\0' ; -fputs (buf, stdout); -} -enum RESULT {OK ,FAIL ,
@@ -188,8 +173,10 @@ static void result(int llen, enum RESULT r)else msg =" [FAIL]" ; -align_result (llen); -puts (msg); + llen =64 - llen; +if (llen <0 ) + llen =0 ; +printf ("%*s%s\n" , llen,"" , msg); }/* The tests below are intended to be used by the macroes, which evaluate
@@ -1699,10 +1686,7 @@ static int expect_vfprintf(int llen, int cond, const char *expected, const char }if (memcmp (expected, buf, cmp_len) || buf[cmp_len]) { -/* Copy and truncate until "%.*s" supported */ -memcpy (buf, expected, cmp_len); - buf[cmp_len] =0 ; - llen +=printf (" should be \"%s\"" , buf); + llen +=printf (" should be \"%.*s\"" ,VFPRINTF_LEN , expected);result (llen,FAIL );return 1 ; }
Signature · 1 line
2.39.5