From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C0D8A3EBF24 for ; Sat, 31 Jan 2026 12:32:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769862734; cv=none; b=YJMzeV7quIYTAmtANmRomgm/FCPth2Vt1w8DXFS//1PntKC/j/+6QtUnUIZ1JbYRsDJfh01TUVCEaS9DvT2pnYodzvRMVwScu1Ku9ogna8vnLgrJGDq7UhArowiwO1oim+ExtpzB0X9a/P2KyCO1fFtv9iwgRR/oxFa+/bVgMbQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769862734; c=relaxed/simple; bh=7jLM7aAFcf7FhMlEWe/D0MdRv17+pQDm5oS9gwzGlFE=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GhBHdwHu5qC+TsRv6madxvMlmmSbEKlCf+a8UeBqn1mCCfvxtHXd1jN04Mr1mthIp5dcAe2u673+OQ49lFCR7VZtYGj5uz+bygYzdhufM/Tf1bkWcM59IolyDbs/YfC1rhprEOOWYO7WY6aHHKWiXZ8D4xYzEMdB7WHFpJWJA+A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CEWLoKby; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CEWLoKby" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-47ee76e8656so39312675e9.0 for ; Sat, 31 Jan 2026 04:32:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769862731; x=1770467531; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=GnS7Jf5C+y3ggBQyF8Bl58+GnC6mbLLtZG3IeafprRU=; b=CEWLoKby5jqE7LO3urZErPDmr8VGCnJNBNlsAY1y6Y+kiFtN4AApl6TGup4NbpoPIJ 8i6t0Vz8VR0HBoIDWiTzXYaJPfebmnb0nbuwm0rCYWGJkZwEmbQnN/Ei3S4vRNyobisy dWqxKEhW9jCSHsbHncIQNYdq96CeST6E8FMXxae+KevrqNuPPIFVNBmqKUj38Tl9aawE Aj+CSJKbTau8WZBTMeUSLN7Yv9Qy6Y1EdujNkWLZTyL71ykMhsMkE5sG7zOmzepxgrPe xiQ+U2QOycJskGs8eUphVcaL0Wlx6eeoh3LAcCquCn8ZTmDRsuRQPcUV1w9oW/tkBeKj Agkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769862731; x=1770467531; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=GnS7Jf5C+y3ggBQyF8Bl58+GnC6mbLLtZG3IeafprRU=; b=e9jfJIdJAo/csiW8BsZTSH3Zz9jJ9vTnFYLb2d5lvotXGUYA6G70UJ1fn2aABgE20O 9Ah13X3r3U52V4ETrBXuuzER0ugcVw3l3UumEjT4vmGHhT21xPuNge4pYCyokEaxNmve RCefjpNZZ6S7vy0NDHsiG9Lz3yO9+0/zck1u0T5pkMacbH1RC/xBg7CpXUgA8/r5PFDu vz6pjLmyWPoCfe7hcj5+AubD5fYZE6n5o+FUVCl7I1N5KE06ZSwQSd2S1PF2WZ7QL4u4 RKKwhTYsBOv1K1Y2vZgCNFz/mrEiopgIosV5/j+N5qg1+qt8WqkjYzdjGQpjLDpGYAro Gvlg== X-Forwarded-Encrypted: i=1; AJvYcCWKR4A4HpXlB+Kf6J2qpKwYfP/+jgHaJxdrSL1PlFDLaafbDN9ARH6BpXQxoe/wJLybrmIt5PaPEIovTiw=@vger.kernel.org X-Gm-Message-State: AOJu0Yxp3cs8sfowgtRHtO/ThFgnSb6+AtZMvwFiBchO+Gz2RYpbM2L0 JEBLmaow3zSIMS8Apv7GWALxX90rmgkzC4s8PXVt4RMTos7pf/C+cvmT X-Gm-Gg: AZuq6aLdveW57bFeVeCYIFXS5ogA70IHGhN0lbsJx4n6eVMz1qCusJp3cCn5HC7Y68N /NG/lKOHl6UDgPsKSUBWLqPvnW40NWcmMtx2+3cRwfb8zCs/Ik7OTGJNDB04Hpouqv21+X4R+Fa 0Oe8S3d1YIBocoZ/0IHkH/Da2qwWFEyhvlHLIv8hA2RRGNzYJRfn9FZaHLeL4kq/yMdGgaVNksn OJS7d2C6jYVi70zHCYpi1Z1JI3q6aFJr8/eKmCD0Wfmaigz3CudCKkJxEgKDHXWdBvPSVoyN2y4 xU1WSVqLGIjNqSM8AORiVHVvg3V0tDiK9SmtGwB0ePLJ+ExH0MIab7tjRbuisQqukHuOJbL3uM5 HKoqVkEilAL8fx6LwAX20gv3Lovjyx673G7fB5RAWczRiMhIXh08B5sd0VVKmfzUhwlC/SqTbB/ E+BZUYxiKTr2w/SJu+matsuI/o4O4avmLcsXuj/GFIA2zHAkKmjM9M X-Received: by 2002:a05:600c:3f16:b0:477:54cd:200e with SMTP id 5b1f17b1804b1-482db4569c0mr72006455e9.1.1769862730839; Sat, 31 Jan 2026 04:32:10 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4806ce5ec6bsm278872225e9.15.2026.01.31.04.32.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 31 Jan 2026 04:32:10 -0800 (PST) Date: Sat, 31 Jan 2026 12:32:09 +0000 From: David Laight To: Thomas =?UTF-8?B?V2Vpw59zY2h1aA==?= Cc: "licheng.li" , Willy Tarreau , linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] tools/nolibc: add support zero pad (0) in printf Message-ID: <20260131123209.1ed5df80@pumpkin> In-Reply-To: <222455c7-17a8-4b1f-a574-4e945467eb12@weissschuh.net> References: <20260130083737.1123-1-im.lechain@gmail.com> <222455c7-17a8-4b1f-a574-4e945467eb12@weissschuh.net> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Sat, 31 Jan 2026 11:18:49 +0100 Thomas Wei=C3=9Fschuh wrote: > Hey Cheng, >=20 > Jan 30, 2026 09:37:51 licheng.li : >=20 > > From: Cheng Li > > > > This patch correctly implements the '0' flag in __nolibc_printf() to > > allow zero-padding for numeric and pointer outputs. =20 >=20 > Thanks for (all of) your patches. > I am not sure when exactly I can take a proper look at them. > As we are currently fairly late in the 6.20/7.0 development cycle I would= like move your patches into the next one. > We can still discuss the patches and you can send new revisions and patch= es, > but they won't be picked up until in a few weeks. Gives me time to re-write them :-) There is still a bug in the 'align left' code as well. snprintf(buf, 21, "%-25s", "abcd") outputs 20 spaces not "abcd" followed by= 16. Easiest fix is to move the truncation in the cb() function. David >=20 > > Thanks to David for pointing out the errors in the previous implementat= ion. > > > > 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"). > > > > Examples of the corrected padding logic: > > - ("%05d", -5)=C2=A0 -> "-0005" > > - ("%05p", ptr)=C2=A0 -> "0x00..." > > > > Signed-off-by: Cheng Li > > --- > > 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 > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* w= e're in an escape sequence, ofs =3D=3D 1 */ > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 esca= pe =3D 0; > > > > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (c =3D= =3D '-') { > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (c =3D= =3D '-' || c =3D=3D '0') { > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 padc =3D c; > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 c =3D fmt[ofs++]; > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } > > @@ -364,9 +364,21 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_= t state, size_t n, const char > > =C2=A0=C2=A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