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: <20260130100252.343902c6@pumpkin>
Date: Fri, 30 Jan 2026 10:02:52 +0000
From: David Laight <david.laight.linux@...il.com>
To: "licheng.li" <im.lechain@...il.com>
Cc: Willy Tarreau <w@....eu>, Thomas Weißschuh
 <linux@...ssschuh.net>, linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/2] tools/nolibc: add support zero pad (0) in printf

On Fri, 30 Jan 2026 16:37:35 +0800
"licheng.li" <im.lechain@...il.com> wrote:

> From: Cheng Li <im.lechain@...il.com>
> 
> This patch correctly implements the '0' flag in __nolibc_printf() to
> allow zero-padding for numeric and pointer outputs.
> 
> Thanks to David for pointing out the errors in the previous implementation.
> 
> The logic ensures that the sign ('-') for negative numbers or the prefix
> ('0x') for pointers is printed before the padding zeros, adhering to
> standard printf behavior (e.g., producing "-0005" instead of "000-5").

I think it would be much better to change the contents of tmpbuf[]
where it is filled with the converted number.
You'd need to limit the number of zeros added (or the precision) to (say) 32
and then set 'outbuf = tmpbuf + 32' so that the zeros and sign/0x can be
added at the front.

There is the (annoying) subtle difference between %#05x and %#.5x just
waiting to catch the unwary.

	David

> 
> Examples of the corrected padding logic:
>  - ("%05d", -5)  -> "-0005"
>  - ("%05p", ptr)  -> "0x00..."
> 
> Signed-off-by: Cheng Li <im.lechain@...il.com>
> ---
>  tools/include/nolibc/stdio.h | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> index f31b77f61d3b..8a4af259a31b 100644
> --- a/tools/include/nolibc/stdio.h
> +++ b/tools/include/nolibc/stdio.h
> @@ -267,7 +267,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
>  			/* we're in an escape sequence, ofs == 1 */
>  			escape = 0;
>  
> -			if (c == '-') {
> +			if (c == '-' || c == '0') {
>  				padc = c;
>  				c = fmt[ofs++];
>  			}
> @@ -364,9 +364,21 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
>  			if (n) {
>  				w = len < n ? len : n;
>  				n -= w;
> +				if (padc == '0') {
> +					if (outstr[0] == '-') {
> +						if (cb(state, outstr, 1) != 0)
> +							return -1;
> +						outstr++;
> +					}
> +					if (outstr[0] == '0' && outstr[1] == 'x') {
> +						if (cb(state, outstr, 2) != 0)
> +							return -1;
> +						outstr += 2;
> +					}
> +				}
>  				while (width > w && padc != '-') {
>  					written += 1;
> -					if (cb(state, " ", 1) != 0)
> +					if (cb(state, &padc, 1) != 0)
>  						return -1;
>  					width--;
>  				}


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ