public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 next 00/17] Enhance printf()
@ 2026-02-23 10:17 david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h david.laight.linux
                   ` (16 more replies)
  0 siblings, 17 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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

-- 
2.39.5


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

* [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-25 21:25   ` Thomas Weißschuh
  2026-02-23 10:17 ` [PATCH v3 next 02/17] tools/nolibc: Optimise and common up the number to ascii functions david.laight.linux
                   ` (15 subsequent siblings)
  16 siblings, 1 reply; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

Needed to stop compiler 'optimisations' bloating code.
Equivalent to the definition in include/linux/compiler.h

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

New patch for v3. 

 tools/include/nolibc/compiler.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/include/nolibc/compiler.h b/tools/include/nolibc/compiler.h
index a8c7619dcdde..8e0eef434eec 100644
--- a/tools/include/nolibc/compiler.h
+++ b/tools/include/nolibc/compiler.h
@@ -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 */
-- 
2.39.5


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

* [PATCH v3 next 02/17] tools/nolibc: Optimise and common up the number to ascii functions
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-25 21:40   ` Thomas Weißschuh
  2026-02-23 10:17 ` [PATCH v3 next 03/17] selftests/nolibc: Fix build with host headers and libc david.laight.linux
                   ` (14 subsequent siblings)
  16 siblings, 1 reply; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index f184e108ed0a..186635656c15 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -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
-- 
2.39.5


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

* [PATCH v3 next 03/17] selftests/nolibc: Fix build with host headers and libc
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 02/17] tools/nolibc: Optimise and common up the number to ascii functions david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-25 21:24   ` Thomas Weißschuh
  2026-02-23 10:17 ` [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors david.laight.linux
                   ` (13 subsequent siblings)
  16 siblings, 1 reply; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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.

Fixes: 6fe8360b16acb  ("selftests/nolibc: also test libc-test through regular selftest framework")
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 1b9d3b2e2491..0e8b3b9a86ef 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (2 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 03/17] selftests/nolibc: Fix build with host headers and libc david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-25 21:56   ` Thomas Weißschuh
  2026-02-23 10:17 ` [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r() david.laight.linux
                   ` (12 subsequent siblings)
  16 siblings, 1 reply; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 0e8b3b9a86ef..029ed63e1ae4 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r()
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (3 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-25 22:09   ` Thomas Weißschuh
  2026-02-23 10:17 ` [PATCH v3 next 06/17] tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to 'outbuf[]' david.laight.linux
                   ` (11 subsequent siblings)
  16 siblings, 1 reply; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 233318b0d0f0..2267f50d03b4 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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 */
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 029ed63e1ae4..61968fdfeec0 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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 */
-- 
2.39.5


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

* [PATCH v3 next 06/17] tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to 'outbuf[]'
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (4 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r() david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-25 22:23   ` Thomas Weißschuh
  2026-02-23 10:17 ` [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback david.laight.linux
                   ` (10 subsequent siblings)
  16 siblings, 1 reply; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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.

Acked-by: Willy Tarreau <w@1wt.eu>
Acked-by: "Thomas Weißschuh" <linux@weissschuh.net>
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 2267f50d03b4..a271059cf67a 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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 char
 					u64toh_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;
-- 
2.39.5


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

* [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (5 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 06/17] tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to 'outbuf[]' david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-25 22:37   ` Thomas Weißschuh
  2026-02-23 10:17 ` [PATCH v3 next 08/17] tools/nolibc/printf: Output pad characters in 16 byte chunks david.laight.linux
                   ` (9 subsequent siblings)
  16 siblings, 1 reply; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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()")
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index a271059cf67a..5c8f233336b9 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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)))
-- 
2.39.5


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

* [PATCH v3 next 08/17] tools/nolibc/printf: Output pad characters in 16 byte chunks
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (6 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 09/17] tools/nolibc/printf: Simplify __nolibc_printf() david.laight.linux
                   ` (8 subsequent siblings)
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

Simple to do and saves calls to the callback function.

Change variables written, width and len to 'signed int' to get
better code.

Acked-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 5c8f233336b9..ea6c514c4b7a 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 09/17] tools/nolibc/printf: Simplify __nolibc_printf()
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (7 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 08/17] tools/nolibc/printf: Output pad characters in 16 byte chunks david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 10/17] tools/nolibc/printf: Use goto and reduce indentation david.laight.linux
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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 <w@1wt.eu>
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index ea6c514c4b7a..52c4c4476102 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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.
-- 
2.39.5


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

* [PATCH v3 next 10/17] tools/nolibc/printf: Use goto and reduce indentation
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (8 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 09/17] tools/nolibc/printf: Simplify __nolibc_printf() david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 11/17] tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion chars david.laight.linux
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 52c4c4476102..ae96b7bebbfe 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 11/17] tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion chars
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (9 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 10/17] tools/nolibc/printf: Use goto and reduce indentation david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 12/17] tools/nolibc/printf: Handle "%s" with the numeric formats david.laight.linux
                   ` (5 subsequent siblings)
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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

Acked-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
                        
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(-)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index ae96b7bebbfe..6cb106367e3b 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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_list
 			break;
 
 		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. */
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 61968fdfeec0..498d3125eb24 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 12/17] tools/nolibc/printf: Handle "%s" with the numeric formats
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (10 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 11/17] tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion chars david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 13/17] tools/nolibc/printf: Add support for conversion flags david.laight.linux
                   ` (4 subsequent siblings)
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

Avoids the extra va_arg() call with is non-trivial on a lot of
modern ABI.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 6cb106367e3b..6dd27473ab7f 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 13/17] tools/nolibc/printf: Add support for conversion flags
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (11 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 12/17] tools/nolibc/printf: Handle "%s" with the numeric formats david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 14/17] tools/nolibc/printf: Add support for left aligning fields david.laight.linux
                   ` (3 subsequent siblings)
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 6dd27473ab7f..c21abe085fa6 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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_list
 	long 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') {
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 498d3125eb24..f746711af777 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 14/17] tools/nolibc/printf: Add support for left aligning fields
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (12 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 13/17] tools/nolibc/printf: Add support for conversion flags david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 15/17] tools/nolibc/printf: Add support for zero padding and field precision david.laight.linux
                   ` (2 subsequent siblings)
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

Output the characters before or after the pad - writing the pad takes more code.

Include additional/changed tests

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index c21abe085fa6..482e5b143c86 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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_list
 			if (cb(state, "                ", pad_len) != 0)
 				return -1;
 		}
-		if (cb(state, outstr, len) != 0)
+		if (!flags && cb(state, outstr, len) != 0)
 			return -1;
 	}
 
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index f746711af777..cc59c0116855 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 15/17] tools/nolibc/printf: Add support for zero padding and field precision
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (13 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 14/17] tools/nolibc/printf: Add support for left aligning fields david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 16/17] tools/nolibc/printf: Add support for octal output david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 17/17] selftests/nolibc: Use printf variable field widths and precisions david.laight.linux
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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

Acked-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 482e5b143c86..cc2870a6a7cb 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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_list
 	char 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;
 
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index cc59c0116855..e0cfe8d12ed1 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 16/17] tools/nolibc/printf: Add support for octal output
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (14 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 15/17] tools/nolibc/printf: Add support for zero padding and field precision david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  2026-02-23 10:17 ` [PATCH v3 next 17/17] selftests/nolibc: Use printf variable field widths and precisions david.laight.linux
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index cc2870a6a7cb..e6aca894bf71 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -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;
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index e0cfe8d12ed1..b1f27fb1dd74 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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;
-- 
2.39.5


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

* [PATCH v3 next 17/17] selftests/nolibc: Use printf variable field widths and precisions
  2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
                   ` (15 preceding siblings ...)
  2026-02-23 10:17 ` [PATCH v3 next 16/17] tools/nolibc/printf: Add support for octal output david.laight.linux
@ 2026-02-23 10:17 ` david.laight.linux
  16 siblings, 0 replies; 34+ messages in thread
From: david.laight.linux @ 2026-02-23 10:17 UTC (permalink / raw)
  To: Willy Tarreau, Thomas Weißschuh, linux-kernel, Cheng Li; +Cc: David Laight

From: David Laight <david.laight.linux@gmail.com>

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.

Acked-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

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

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index b1f27fb1dd74..6c043e5e1220 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -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;
 	}
-- 
2.39.5


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

* Re: [PATCH v3 next 03/17] selftests/nolibc: Fix build with host headers and libc
  2026-02-23 10:17 ` [PATCH v3 next 03/17] selftests/nolibc: Fix build with host headers and libc david.laight.linux
@ 2026-02-25 21:24   ` Thomas Weißschuh
  0 siblings, 0 replies; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-25 21:24 UTC (permalink / raw)
  To: david.laight.linux; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-23 10:17:21+0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> 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.
> 
> Fixes: 6fe8360b16acb  ("selftests/nolibc: also test libc-test through regular selftest framework")
> Signed-off-by: David Laight <david.laight.linux@gmail.com>

Applied, thanks.

(...)

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

* Re: [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h
  2026-02-23 10:17 ` [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h david.laight.linux
@ 2026-02-25 21:25   ` Thomas Weißschuh
  2026-02-25 22:17     ` David Laight
  0 siblings, 1 reply; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-25 21:25 UTC (permalink / raw)
  To: david.laight.linux; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-23 10:17:19+0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> Needed to stop compiler 'optimisations' bloating code.
> Equivalent to the definition in include/linux/compiler.h
> 
> Signed-off-by: David Laight <david.laight.linux@gmail.com>
> ---
> 
> New patch for v3. 
> 
>  tools/include/nolibc/compiler.h | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/tools/include/nolibc/compiler.h b/tools/include/nolibc/compiler.h
> index a8c7619dcdde..8e0eef434eec 100644
> --- a/tools/include/nolibc/compiler.h
> +++ b/tools/include/nolibc/compiler.h
> @@ -71,4 +71,9 @@
>  #  define __nolibc_static_assert(_t)
>  #endif
>  
> +#ifndef _NOLIBC_OPTIMIZER_HIDE_VAR

The ifndef guard should not be necessary.

> +/* Make the optimizer believe the variable can be manipulated arbitrarily. */
> +#define _NOLIBC_OPTIMIZER_HIDE_VAR(var)	__asm__ ("" : "+r" (var))
> +#endif
> +
>  #endif /* _NOLIBC_COMPILER_H */
> -- 
> 2.39.5
> 

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

* Re: [PATCH v3 next 02/17] tools/nolibc: Optimise and common up the number to ascii functions
  2026-02-23 10:17 ` [PATCH v3 next 02/17] tools/nolibc: Optimise and common up the number to ascii functions david.laight.linux
@ 2026-02-25 21:40   ` Thomas Weißschuh
  2026-02-25 22:09     ` David Laight
  0 siblings, 1 reply; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-25 21:40 UTC (permalink / raw)
  To: david.laight.linux; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-23 10:17:20+0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> 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.
> 
> Signed-off-by: David Laight <david.laight.linux@gmail.com>

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.

(...)

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

* Re: [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors
  2026-02-23 10:17 ` [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors david.laight.linux
@ 2026-02-25 21:56   ` Thomas Weißschuh
  2026-02-26 10:12     ` David Laight
  0 siblings, 1 reply; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-25 21:56 UTC (permalink / raw)
  To: david.laight.linux; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-23 10:17:22+0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> 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.

Then please do this right before the addition of the octal conversion.

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

The build bots will yell at us for this.
Instead mark the test as skipped until it is fixed.

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

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.

> Signed-off-by: David Laight <david.laight.linux@gmail.com>
> ---
> 
> 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(-)
> 
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index 0e8b3b9a86ef..029ed63e1ae4 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -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.

> +
> +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;
> +	}

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.

> +
> +	/* 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;
> +	}

(...)

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

* Re: [PATCH v3 next 02/17] tools/nolibc: Optimise and common up the number to ascii functions
  2026-02-25 21:40   ` Thomas Weißschuh
@ 2026-02-25 22:09     ` David Laight
  0 siblings, 0 replies; 34+ messages in thread
From: David Laight @ 2026-02-25 22:09 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On Wed, 25 Feb 2026 22:40:29 +0100
Thomas Weißschuh <linux@weissschuh.net> wrote:

> On 2026-02-23 10:17:20+0000, david.laight.linux@gmail.com wrote:
> > From: David Laight <david.laight.linux@gmail.com>
> > 
> > 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.
> > 
> > Signed-off-by: David Laight <david.laight.linux@gmail.com>  
> 
> 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

> 
> (...)


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

* Re: [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r()
  2026-02-23 10:17 ` [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r() david.laight.linux
@ 2026-02-25 22:09   ` Thomas Weißschuh
  2026-02-25 22:58     ` David Laight
  0 siblings, 1 reply; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-25 22:09 UTC (permalink / raw)
  To: david.laight.linux; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-23 10:17:23+0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> 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().

Why?

> 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 <david.laight.linux@gmail.com>
> ---
> 
> 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(-)
> 
> diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> index 233318b0d0f0..2267f50d03b4 100644
> --- a/tools/include/nolibc/stdio.h
> +++ b/tools/include/nolibc/stdio.h
> @@ -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)))

This is the GNU variant, but the XSI variant would be better.

Why is the buflen parameter not used?

> +{
> +#ifdef NOLIBC_IGNORE_ERRNO

Why did this move here?
The application can have error numbers without 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)

Good point. But also a candidate for its own patch.

>  {
> -	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 */
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index 029ed63e1ae4..61968fdfeec0 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -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;

Assigning errno from the printf argument is sneaky.
Please avoid sneakyness.

>  		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 */
> -- 
> 2.39.5
> 

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

* Re: [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h
  2026-02-25 21:25   ` Thomas Weißschuh
@ 2026-02-25 22:17     ` David Laight
  2026-02-25 22:24       ` Thomas Weißschuh
  0 siblings, 1 reply; 34+ messages in thread
From: David Laight @ 2026-02-25 22:17 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On Wed, 25 Feb 2026 22:25:12 +0100
Thomas Weißschuh <linux@weissschuh.net> wrote:

> On 2026-02-23 10:17:19+0000, david.laight.linux@gmail.com wrote:
> > From: David Laight <david.laight.linux@gmail.com>
> > 
> > Needed to stop compiler 'optimisations' bloating code.
> > Equivalent to the definition in include/linux/compiler.h
> > 
> > Signed-off-by: David Laight <david.laight.linux@gmail.com>
> > ---
> > 
> > New patch for v3. 
> > 
> >  tools/include/nolibc/compiler.h | 5 +++++
> >  1 file changed, 5 insertions(+)
> > 
> > diff --git a/tools/include/nolibc/compiler.h b/tools/include/nolibc/compiler.h
> > index a8c7619dcdde..8e0eef434eec 100644
> > --- a/tools/include/nolibc/compiler.h
> > +++ b/tools/include/nolibc/compiler.h
> > @@ -71,4 +71,9 @@
> >  #  define __nolibc_static_assert(_t)
> >  #endif
> >  
> > +#ifndef _NOLIBC_OPTIMIZER_HIDE_VAR  
> 
> The 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

> 
> > +/* Make the optimizer believe the variable can be manipulated arbitrarily. */
> > +#define _NOLIBC_OPTIMIZER_HIDE_VAR(var)	__asm__ ("" : "+r" (var))
> > +#endif
> > +
> >  #endif /* _NOLIBC_COMPILER_H */
> > -- 
> > 2.39.5
> >   


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

* Re: [PATCH v3 next 06/17] tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to 'outbuf[]'
  2026-02-23 10:17 ` [PATCH v3 next 06/17] tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to 'outbuf[]' david.laight.linux
@ 2026-02-25 22:23   ` Thomas Weißschuh
  0 siblings, 0 replies; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-25 22:23 UTC (permalink / raw)
  To: david.laight.linux; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-23 10:17:24+0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> 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.
> 
> Acked-by: Willy Tarreau <w@1wt.eu>
> Acked-by: "Thomas Weißschuh" <linux@weissschuh.net>
> Signed-off-by: David Laight <david.laight.linux@gmail.com>

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.

(...)

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

* Re: [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h
  2026-02-25 22:17     ` David Laight
@ 2026-02-25 22:24       ` Thomas Weißschuh
  0 siblings, 0 replies; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-25 22:24 UTC (permalink / raw)
  To: David Laight; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-25 22:17:02+0000, David Laight wrote:
> On Wed, 25 Feb 2026 22:25:12 +0100
> Thomas Weißschuh <linux@weissschuh.net> wrote:
> 
> > On 2026-02-23 10:17:19+0000, david.laight.linux@gmail.com wrote:
> > > From: David Laight <david.laight.linux@gmail.com>
> > > 
> > > Needed to stop compiler 'optimisations' bloating code.
> > > Equivalent to the definition in include/linux/compiler.h
> > > 
> > > Signed-off-by: David Laight <david.laight.linux@gmail.com>
> > > ---
> > > 
> > > New patch for v3. 
> > > 
> > >  tools/include/nolibc/compiler.h | 5 +++++
> > >  1 file changed, 5 insertions(+)
> > > 
> > > diff --git a/tools/include/nolibc/compiler.h b/tools/include/nolibc/compiler.h
> > > index a8c7619dcdde..8e0eef434eec 100644
> > > --- a/tools/include/nolibc/compiler.h
> > > +++ b/tools/include/nolibc/compiler.h
> > > @@ -71,4 +71,9 @@
> > >  #  define __nolibc_static_assert(_t)
> > >  #endif
> > >  
> > > +#ifndef _NOLIBC_OPTIMIZER_HIDE_VAR  
> > 
> > The 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.
> Can you take it out?

Sure, I can do that when applying the patch.
But a new revision will be necessary anyways.

> > 
> > > +/* Make the optimizer believe the variable can be manipulated arbitrarily. */
> > > +#define _NOLIBC_OPTIMIZER_HIDE_VAR(var)	__asm__ ("" : "+r" (var))
> > > +#endif
> > > +
> > >  #endif /* _NOLIBC_COMPILER_H */
> > > -- 
> > > 2.39.5
> > >   
> 

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

* Re: [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback
  2026-02-23 10:17 ` [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback david.laight.linux
@ 2026-02-25 22:37   ` Thomas Weißschuh
  2026-02-25 23:12     ` David Laight
  0 siblings, 1 reply; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-25 22:37 UTC (permalink / raw)
  To: david.laight.linux; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-23 10:17:25+0000, david.laight.linux@gmail.com wrote:

(...)

> @@ -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_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);

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.

>  
> -	memcpy(*state, buf, size);
> -	*state += size;
>  	return 0;
>  }

(...)

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

* Re: [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r()
  2026-02-25 22:09   ` Thomas Weißschuh
@ 2026-02-25 22:58     ` David Laight
  0 siblings, 0 replies; 34+ messages in thread
From: David Laight @ 2026-02-25 22:58 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On Wed, 25 Feb 2026 23:09:25 +0100
Thomas Weißschuh <linux@weissschuh.net> wrote:

> On 2026-02-23 10:17:23+0000, david.laight.linux@gmail.com wrote:
> > From: David Laight <david.laight.linux@gmail.com>
> > 
> > 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().  
> 
> Why?
> 
> > 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 <david.laight.linux@gmail.com>
> > ---
> > 
> > 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(-)
> > 
> > diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> > index 233318b0d0f0..2267f50d03b4 100644
> > --- a/tools/include/nolibc/stdio.h
> > +++ b/tools/include/nolibc/stdio.h
> > @@ -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)))  
> 
> 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.

> 
> 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().
 
> 
> > +{
> > +#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.

> 
> > +	__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.
> 
> >  {
> > -	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 */
> > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> > index 029ed63e1ae4..61968fdfeec0 100644
> > --- a/tools/testing/selftests/nolibc/nolibc-test.c
> > +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> > @@ -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;  
> 
> Assigning errno from the printf argument is sneaky.
That's not 'sneaky', I can do 'proper sneaky' :-)
> Please avoid sneakyness.

How about:
		CASE_TEST(errno);        errno = 22; EXPECT_VFPRINTF(is_nolibc, "errno=22", "%m"); break;

	David

> 
> >  		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 */
> > -- 
> > 2.39.5
> >   


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

* Re: [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback
  2026-02-25 22:37   ` Thomas Weißschuh
@ 2026-02-25 23:12     ` David Laight
  2026-02-26 21:29       ` Thomas Weißschuh
  0 siblings, 1 reply; 34+ messages in thread
From: David Laight @ 2026-02-25 23:12 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On Wed, 25 Feb 2026 23:37:42 +0100
Thomas Weißschuh <linux@weissschuh.net> wrote:

> On 2026-02-23 10:17:25+0000, david.laight.linux@gmail.com wrote:
> 
> (...)
> 
> > @@ -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_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);  
> 
> 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.

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 check
printf("%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

> 
> >  
> > -	memcpy(*state, buf, size);
> > -	*state += size;
> >  	return 0;
> >  }  
> 
> (...)


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

* Re: [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors
  2026-02-25 21:56   ` Thomas Weißschuh
@ 2026-02-26 10:12     ` David Laight
  2026-02-26 21:39       ` Thomas Weißschuh
  0 siblings, 1 reply; 34+ messages in thread
From: David Laight @ 2026-02-26 10:12 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On Wed, 25 Feb 2026 22:56:03 +0100
Thomas Weißschuh <linux@weissschuh.net> wrote:

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

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

> 
> > 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.  
> 
> 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.
> 
> > Signed-off-by: David Laight <david.laight.linux@gmail.com>
> > ---
> > 
> > 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(-)
> > 
> > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> > index 0e8b3b9a86ef..029ed63e1ae4 100644
> > --- a/tools/testing/selftests/nolibc/nolibc-test.c
> > +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> > @@ -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.

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.

> 
> > +
> > +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;
> > +	}  
> 
> 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

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

* Re: [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback
  2026-02-25 23:12     ` David Laight
@ 2026-02-26 21:29       ` Thomas Weißschuh
  2026-02-26 22:11         ` David Laight
  0 siblings, 1 reply; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-26 21:29 UTC (permalink / raw)
  To: David Laight; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-25 23:12:21+0000, David Laight wrote:
> On Wed, 25 Feb 2026 23:37:42 +0100
> Thomas Weißschuh <linux@weissschuh.net> wrote:
> 
> > On 2026-02-23 10:17:25+0000, david.laight.linux@gmail.com wrote:
> > 
> > (...)
> > 
> > > @@ -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_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);  
> > 
> > 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.
> 
> 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.

> 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 check
> printf("%s", "") will give a zero size with non-NULL buf.

Can you roll this into the next revision?

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

> > >  
> > > -	memcpy(*state, buf, size);
> > > -	*state += size;
> > >  	return 0;
> > >  }  
> > 
> > (...)

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

* Re: [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors
  2026-02-26 10:12     ` David Laight
@ 2026-02-26 21:39       ` Thomas Weißschuh
  0 siblings, 0 replies; 34+ messages in thread
From: Thomas Weißschuh @ 2026-02-26 21:39 UTC (permalink / raw)
  To: David Laight; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On 2026-02-26 10:12:46+0000, David Laight wrote:
> On Wed, 25 Feb 2026 22:56:03 +0100
> Thomas Weißschuh <linux@weissschuh.net> wrote:
> 
> ... 
> > > 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.

That sounds good. But please in a dedicated patch.

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

> But I can disable it.

Can we not just move the fix to printf before this commit?
Then the problem just goes away.

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

> > > 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.  
> > 
> > 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.
> > 
> > > Signed-off-by: David Laight <david.laight.linux@gmail.com>
> > > ---
> > > 
> > > 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(-)
> > > 
> > > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> > > index 0e8b3b9a86ef..029ed63e1ae4 100644
> > > --- a/tools/testing/selftests/nolibc/nolibc-test.c
> > > +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> > > @@ -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.
> 
> 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.

> > > +
> > > +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;
> > > +	}  
> > 
> > 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.

Please do.

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

> 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

> 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

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

* Re: [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback
  2026-02-26 21:29       ` Thomas Weißschuh
@ 2026-02-26 22:11         ` David Laight
  0 siblings, 0 replies; 34+ messages in thread
From: David Laight @ 2026-02-26 22:11 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: Willy Tarreau, linux-kernel, Cheng Li

On Thu, 26 Feb 2026 22:29:07 +0100
Thomas Weißschuh <linux@weissschuh.net> wrote:

> On 2026-02-25 23:12:21+0000, David Laight wrote:
> > On Wed, 25 Feb 2026 23:37:42 +0100
> > Thomas Weißschuh <linux@weissschuh.net> wrote:
> >   
> > > On 2026-02-23 10:17:25+0000, david.laight.linux@gmail.com wrote:
> > > 
> > > (...)
> > >   
> > > > @@ -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_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);    
> > > 
> > > 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.  
> > 
> > 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.
> 
> > 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 check
> > printf("%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

> 
> > 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.
> 
> > > >  
> > > > -	memcpy(*state, buf, size);
> > > > -	*state += size;
> > > >  	return 0;
> > > >  }    
> > > 
> > > (...)  


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

end of thread, other threads:[~2026-02-26 22:11 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-23 10:17 [PATCH v3 next 00/17] Enhance printf() david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 01/17] tools/nolibc: Add _NOLIBC_OPTIMIZER_HIDE_VAR() to compiler.h david.laight.linux
2026-02-25 21:25   ` Thomas Weißschuh
2026-02-25 22:17     ` David Laight
2026-02-25 22:24       ` Thomas Weißschuh
2026-02-23 10:17 ` [PATCH v3 next 02/17] tools/nolibc: Optimise and common up the number to ascii functions david.laight.linux
2026-02-25 21:40   ` Thomas Weißschuh
2026-02-25 22:09     ` David Laight
2026-02-23 10:17 ` [PATCH v3 next 03/17] selftests/nolibc: Fix build with host headers and libc david.laight.linux
2026-02-25 21:24   ` Thomas Weißschuh
2026-02-23 10:17 ` [PATCH v3 next 04/17] selftests/nolibc: Improve reporting of vfprintf() errors david.laight.linux
2026-02-25 21:56   ` Thomas Weißschuh
2026-02-26 10:12     ` David Laight
2026-02-26 21:39       ` Thomas Weißschuh
2026-02-23 10:17 ` [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r() david.laight.linux
2026-02-25 22:09   ` Thomas Weißschuh
2026-02-25 22:58     ` David Laight
2026-02-23 10:17 ` [PATCH v3 next 06/17] tools/nolibc/printf: Change variables 'c' to 'ch' and 'tmpbuf[]' to 'outbuf[]' david.laight.linux
2026-02-25 22:23   ` Thomas Weißschuh
2026-02-23 10:17 ` [PATCH v3 next 07/17] tools/nolibc/printf: Move snprintf length check to callback david.laight.linux
2026-02-25 22:37   ` Thomas Weißschuh
2026-02-25 23:12     ` David Laight
2026-02-26 21:29       ` Thomas Weißschuh
2026-02-26 22:11         ` David Laight
2026-02-23 10:17 ` [PATCH v3 next 08/17] tools/nolibc/printf: Output pad characters in 16 byte chunks david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 09/17] tools/nolibc/printf: Simplify __nolibc_printf() david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 10/17] tools/nolibc/printf: Use goto and reduce indentation david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 11/17] tools/nolibc/printf: Use bit-masks to hold requested flag, length and conversion chars david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 12/17] tools/nolibc/printf: Handle "%s" with the numeric formats david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 13/17] tools/nolibc/printf: Add support for conversion flags david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 14/17] tools/nolibc/printf: Add support for left aligning fields david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 15/17] tools/nolibc/printf: Add support for zero padding and field precision david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 16/17] tools/nolibc/printf: Add support for octal output david.laight.linux
2026-02-23 10:17 ` [PATCH v3 next 17/17] selftests/nolibc: Use printf variable field widths and precisions david.laight.linux

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox