From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 4052527E1DC for ; Fri, 30 Jan 2026 10:02:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769767376; cv=none; b=h0ftnj3PMCKhhjUDN5U2LQHrqMJuZ4cC4ADtmK3e62Dm28FfNrHNIopi6+lF6RDclxIStlppIn9FUFz+eFNLMvXPXcOdtL3bWDmj92+y7WvxNr2FNGM3xS82Hr7xU0Z46Mx4SaHo3f1aiHNByeHDhQIkur43hLuJQlWUSSucNms= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769767376; c=relaxed/simple; bh=M93AyMJJzza+izliDM1spYOadwu42O2XoRKlMiGzc18=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GqdAuOE6jqKgBW5DtrCgTuw+gzh0IFCe2H0Yy+3MJxb2ZR6kKYe9J1ZMZXrXsmfyN7JUtmGTfCw/pucNozi0EvBe3yW8C8I1bEfs82TKNdcN1TwfMGGJP6V+UDpL85A6zT6ZCm9i6B0ibSetdlsh/xH3nTedNv9yCGw3QyYkfGo= 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=AZgdfWie; arc=none smtp.client-ip=209.85.221.53 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="AZgdfWie" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-435f177a8f7so1052000f8f.1 for ; Fri, 30 Jan 2026 02:02:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769767373; x=1770372173; 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=NfBDSmsX2TUwNK5INpMuD9MTLbdKFGQ6RpzOJpDMccg=; b=AZgdfWieTJbxJuEovDCJU1daQ7ZAlcIIZU91byUAvr+HqkVr3M0HYzwFdBZH1k95/a 19DYVWNgOb8Yyf+EHUOYwGsOTc6E0a074Wstz0KcI4ZUzGzfztM44She8Ck7DJ0Q/HPW UwLO0rzyLiBgdtHWQcycH5vSI/rYDfsRbPPRN7I3i3BEtzKJW9twXMTvwX3fy8We33d3 iQsRJ/SawtF10S5MFzWZ+2D9qQQKTSxUiUgqFWJehmjF7nvr+XUmmCso7xURXddVk6tV 6+t7roAMlnaddAq6/Qtzb9OU9t+oL6gwIjgXefOmdL3GtJVRjF44SyN4m98wGdf+Wgms 2cWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769767373; x=1770372173; 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=NfBDSmsX2TUwNK5INpMuD9MTLbdKFGQ6RpzOJpDMccg=; b=jia31IurwSMLb6PD38WFovgtQnPype3oKuLLy/+y8hmBrwUrzx/Lw7tmS8AxUGGeWB xwM8xs18iglCPIp3k2YCUpmQv35d5NS5AgFnX+sF3oyfRs+fiSuWq4gzajvqGNB+Df5y D7TYgUqETyXVIG/0Ldldg+kHC0Xgvp3/tRGZmR65WyeIqkz+7js3GJm4hPkSEKjiA9bK 5P0m5DwU3IQZP5+SukvL4bxKvWXYjW4345XYYFRJLctqyt1PQQjN+vixUIZRvGQ6TTYa GRLKmhO2/MnKGYVq/M3JToEN/JQFQ5GIvZlHm+1ZE4tIwukMz4/3u3e8llW9sete0byD uclQ== X-Forwarded-Encrypted: i=1; AJvYcCUD+9UvMr5ZwV7IbEm/TgO3uYLXmsClLoBqSQyWtkWdv2t9021cvsacgB7gFzlcisLJul6v6vbchQCcoYs=@vger.kernel.org X-Gm-Message-State: AOJu0YzCZYqxyPxROYutSHyfSKwm1rrlvPaZjtuKFlndypoC6Tf0REJu m6FL4Kw+uBA795o6wCbEBkqLF1YXbbOdN2mHKnTilIQutQLddJt/FptnAgXIyg== X-Gm-Gg: AZuq6aIgoGXk7gTUiyUQRj8S/0PMsbQvVJiSe+hgPiwO4sOljszdrgnlBAyDIW+FeI8 14l/s5DHp33mtfnajyYzJtFl2SIS7KOKW8wa0AvrEGrEYGet8359m5obFkxpyxvIqByL0tQVWUj HMOAsywZqEJmNdQYXUGgCmt2xFz1OKuIFS829ZXDQ46smuRqilNP+vyvdtsM/8SRnTv9stJC7zv 8sZEQ4UP+iQ4U0IbvcOX9Cu7LrS5og8wnDzGPNBy2n6mD843Olz7nDsTONPWOIm4LH+3Yf+dcI1 GGWDZTEsgIacYoxvvXyVhgQ41kue3eL+DCLPOOpAuPvSNsLattuWoQJvNneFK36cQTLqtFYUCDz 3cx48OqqNunWiMVb7Jl3rrLbHsm8mPfMzGlnacJvhmMY6gTJUKFgauXHUthtW2oded1oVOkYq7f q1hcMlslhChWDkG3th/OoA8/fmW5zjIyVQn3ow1KbDgfNpqISC0tFA X-Received: by 2002:a05:6000:2011:b0:435:9325:47fe with SMTP id ffacd0b85a97d-435f3a6a873mr3476450f8f.9.1769767373465; Fri, 30 Jan 2026 02:02:53 -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 ffacd0b85a97d-435e10ed952sm21324218f8f.10.2026.01.30.02.02.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jan 2026 02:02:53 -0800 (PST) Date: Fri, 30 Jan 2026 10:02:52 +0000 From: David Laight To: "licheng.li" Cc: Willy Tarreau , Thomas =?UTF-8?B?V2Vpw59zY2h1aA==?= , linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] tools/nolibc: add support zero pad (0) in printf Message-ID: <20260130100252.343902c6@pumpkin> In-Reply-To: <20260130083737.1123-1-im.lechain@gmail.com> References: <20260130083737.1123-1-im.lechain@gmail.com> 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=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 30 Jan 2026 16:37:35 +0800 "licheng.li" wrote: > From: Cheng Li > > 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 > --- > 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--; > }