lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260203103000.20206-11-david.laight.linux@gmail.com>
Date: Tue,  3 Feb 2026 10:29:58 +0000
From: david.laight.linux@...il.com
To: Willy Tarreau <w@....eu>,
	Thomas Weißschuh <linux@...ssschuh.net>,
	linux-kernel@...r.kernel.org,
	Cheng Li <lechain@...il.com>
Cc: David Laight <david.laight.linux@...il.com>
Subject: [PATCH next 10/12] tools/nolibc/printf: Use bit-pattern for integral formats

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

Generates better code when code is common for multiple formats.
In particular it stops two calls to u64toa_r().

Signed-off-by: David Laight <david.laight.linux@...il.com>
---
 tools/include/nolibc/stdio.h | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index bad528921590..23fe8b8e7767 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -241,7 +241,7 @@ char *fgets(char *s, int size, FILE *stream)
 
 
 /* printf(). Supports most of the normal integer and string formats.
- *  - %[#0-+ ][width|*[.precision|*]][{l,t,z,ll,L,j,q}]{d,i,u,c,x,X,p,s,m}
+ *  - %[#0-+ ][width|*[.precision|*]][{l,t,z,ll,L,j,q}]{d,i,u,c,x,X,p,s,m,%}
  *  - %% generates a single %
  *  - %m outputs strerror(errno).
  *  - # only affects %x and prepends 0x to non-zero values.
@@ -258,7 +258,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 {
 	char c;
 	int len, written, width, precision;
-	unsigned int flags;
+	unsigned int flags, c_flag;
 	char tmpbuf[32 + 24];
 	const char *outstr;
 
@@ -326,8 +326,12 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 			if (!((c >= 'a' && c <= 'z') || (c == 'X' && (c = 'x'))))
 				goto bad_conversion_specifier;
 
-			/* Numeric and pointer conversion specifiers */
-			if (__PF_FLAG(c) & (__PF_FLAG('c') | __PF_FLAG('d') | __PF_FLAG('i') | __PF_FLAG('u') |
+			/* Numeric and pointer conversion specifiers.
+			 * We need to check for "%p" or "%#x" later, merging here gives better code.
+			 * But '#' collides with 'c' so shift right.
+			 */
+			c_flag = __PF_FLAG(c) | (flags & __PF_FLAG('#')) >> 1;
+			if (c_flag & (__PF_FLAG('c') | __PF_FLAG('d') | __PF_FLAG('i') | __PF_FLAG('u') |
 					    __PF_FLAG('x') | __PF_FLAG('p') | __PF_FLAG('s'))) {
 				unsigned long long v;
 				long long signed_v;
@@ -335,7 +339,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 				int sign = 0;
 
 				/* Annoying 'p' === '0' so mask from flags */
-				if ((__PF_FLAG(c) | (flags & ~__PF_FLAG('p'))) &
+				if ((c_flag | (flags & ~__PF_FLAG('p'))) &
 				    (__PF_FLAG('p') | __PF_FLAG('s') | __PF_FLAG('l') | __PF_FLAG('t') | __PF_FLAG('z'))) {
 					v = va_arg(args, unsigned long);
 					signed_v = (long)v;
@@ -347,13 +351,14 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 					signed_v = (int)v;
 				}
 
-				switch (c) {
-				case 'c':
+				if (c_flag & __PF_FLAG('c')) {
 					tmpbuf[0] = v;
 					len = 1;
 					outstr = tmpbuf;
 					goto do_output;
-				case 's':
+				}
+
+				if (c_flag & __PF_FLAG('s')) {
 					if (!v) {
 						outstr = "(null)";
 						/* Match glibc, nothing output if precision too small */
@@ -364,8 +369,9 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 do_strnlen_output:
 					len = strnlen(outstr, precision);
 					goto do_output;
-				case 'd':
-				case 'i':
+				}
+
+				if (c_flag & (__PF_FLAG('d') | __PF_FLAG('i'))) {
 					if (signed_v < 0) {
 						sign = '-';
 						v = -(signed_v + 1);
@@ -375,12 +381,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 					} else if (flags & __PF_FLAG(' ')) {
 						sign = ' ';
 					}
-					c = 'u';
 				}
 
 				if (v == 0) {
 					/* There are special rules for zero. */
-					if (c == 'p') {
+					if (c_flag & __PF_FLAG('p')) {
 						/* match glibc, precision is ignored */
 						outstr = "(nil)";
 						len = 5;
@@ -395,11 +400,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 					*out = '0';
 					len = 1;
 				} else {
-					if (c == 'u') {
+					if (c_flag & (__PF_FLAG('d') | __PF_FLAG('i') | __PF_FLAG('u'))) {
 						len = u64toa_r(v, out);
 					} else {
 						len = u64toh_r(v, out);
-						if (c == 'p' || (flags & __PF_FLAG('#')))
+						if (c_flag & (__PF_FLAG('p') | __PF_FLAG('#' - 1)))
 							sign = 'x' | '0' << 8;
 					}
 				}
-- 
2.39.5


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ