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-8-david.laight.linux@gmail.com>
Date: Tue,  3 Feb 2026 10:29:55 +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 07/12] tools/nolibc/printf: Prepend the sign after a numeric conversion

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

Needed so that zero can be correctly padded.
Add support for the "+ " modifiers for non-negative integers.

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

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 1ce4d357a802..e4792625c1ec 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)
 
 
 /* simple printf(). It supports the following formats:
- *  - %[-][width][{l,t,z,ll,L,j,q}]{d,u,c,x,p,s,m}
+ *  - %[-+ ][width][{l,t,z,ll,L,j,q}]{d,u,c,x,p,s,m}
  *  - %%
  *  - invalid formats are copied to the output buffer
  */
@@ -254,7 +254,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 	char c;
 	int len, written, width;
 	unsigned int flags;
-	char tmpbuf[21];
+	char tmpbuf[64];
 	const char *outstr;
 
 	written = 0;
@@ -277,7 +277,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 
 			/* Flag characters */
 			for (; c >= 0x20 && c <= 0x3f; c = *fmt++) {
-				if ((__PF_FLAG(c) & (__PF_FLAG('-'))) == 0)
+				if ((__PF_FLAG(c) & (__PF_FLAG('-') | __PF_FLAG(' ') | __PF_FLAG('+'))) == 0)
 					break;
 				flags |= __PF_FLAG(c);
 			}
@@ -307,7 +307,8 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 			if (c == 'c' || c == 'd' || c == 'u' || c == 'x' || c == 'p') {
 				unsigned long long v;
 				long long signed_v;
-				char *out = tmpbuf;
+				char *out = tmpbuf + 32;
+				int sign = 0;
 
 				if ((c == 'p') || (flags & (__PF_FLAG('l') | __PF_FLAG('t') | __PF_FLAG('z')))) {
 					v = va_arg(args, unsigned long);
@@ -322,24 +323,35 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 
 				switch (c) {
 				case 'c':
-					out[0] = v;
-					out[1] = 0;
-					break;
+					tmpbuf[0] = v;
+					len = 1;
+					outstr = tmpbuf;
+					goto do_output;
 				case 'd':
-					i64toa_r(signed_v, out);
-					break;
+					if (signed_v < 0) {
+						sign = '-';
+						v = -(signed_v + 1);
+						v++;
+					} else if (flags & __PF_FLAG('+')) {
+						sign = '+';
+					} else if (flags & __PF_FLAG(' ')) {
+						sign = ' ';
+					}
+					__nolibc_fallthrough;
 				case 'u':
 					u64toa_r(v, out);
 					break;
 				case 'p':
-					*(out++) = '0';
-					*(out++) = 'x';
+					sign = 'x' | '0' << 8;
 					__nolibc_fallthrough;
 				default: /* 'x' and 'p' above */
 					u64toh_r(v, out);
 					break;
 				}
-				outstr = tmpbuf;
+				for (; sign; sign >>= 8) {
+					*--out = sign;
+				}
+				outstr = out;
 			}
 			else if (c == 's') {
 				outstr = va_arg(args, char *);
@@ -365,6 +377,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 			len = strlen(outstr);
 		}
 
+do_output:
 		written += len;
 
                 /* An OPTIMIZER_HIDE_VAR() seems to stop gcc back-merging this
-- 
2.39.5


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ