From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30E4A399036 for ; Mon, 2 Mar 2026 10:18:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772446734; cv=none; b=nAsp5URs2XbOXaN4g0JRJO5AHfkHKTJRk2HH96YTKs5L0FuWASfPYhgRKTNrO+jzwXwytCVU3VWUTBfFIH30dktpZyk4lmZEIACYI9y3bZe78k+svt++4pvby9WijzNCghJr2Zcz5FGaA01Q23flJHzBjGF5Sdeh9XeX5RSBfSA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772446734; c=relaxed/simple; bh=rT1izaWZp644rPOJMRiRSLG3lvtvISAe0h4eBE3L6ec=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Y5nFQNp1AyFK61zsn5knIWXdwnMXCHnpe5+vfWlfnvfDWDRCgk/UQjn5bbRuo4aTFqslrBWY4wtnHoD2mgnBmVH8c/WtsNNs0jcPR54qwuS77piAv7iQfJWenbJXj8R50Q3bWFA7sPFiFAIP4gCo20DukKt/vctGyMG/G8L2dFk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=EZfoiGos; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="EZfoiGos" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vx0MU-003Bac-Da; Mon, 02 Mar 2026 11:18:50 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=+K22pr+zYjs+WNaEw7LiybMWACKGA99hlKFAYu09hVc=; b=EZfoiGos9IEiY9q2ROZd0W9Q6f 13QusR1v1FrVsR8OmvX/IgrVG6mz5NEDvL8F0HaUMAXMWwNVpio8G/nTQPRk5BtZkotp/ZzcoPI97 hd3wZ25edbMXaVNDi3jWsMExTulM+wpHz9zLPf+blZHdIFXMm947hYX+yFU0E4XMknTWPYQP+5+Re JdPmuS0RPFercHf7yId46axSA6gtpmzJM6ZZxGZZ+W7V0++wJOSeR3I0ic1wNJCJX3edGP7Lbip4E 30djdFqL+Pt0rh5q9JIvPjYapgFxHnpX5+UnwDo0bqGpXzPs9vY7d6Dzxz4MDEdlzG5nimOtxaInb bcnD1XOA==; Received: from [10.9.9.72] (helo=submission01.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vx0MU-00055c-3u; Mon, 02 Mar 2026 11:18:50 +0100 Received: by submission01.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vx0MC-006y7o-TY; Mon, 02 Mar 2026 11:18:32 +0100 From: david.laight.linux@gmail.com To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, Cheng Li Cc: David Laight Subject: [PATCH 17/23] tools/nolibc/printf: Prepend sign to converted number Date: Mon, 2 Mar 2026 10:18:09 +0000 Message-Id: <20260302101815.3043-18-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260302101815.3043-1-david.laight.linux@gmail.com> References: <20260302101815.3043-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: David Laight Instead of appending the converted number to the sign, convert first and then prepend the sign (or "0x"). Use the length returned by u64toh_r() instead of calling strlen(). Needed so that zero padding can be inserted between the sign and digits in an upcoming patch. Signed-off-by: David Laight --- Changes for v4: - Split from the patch that supported modifiers " +#". tools/include/nolibc/stdio.h | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 484432ca87d5..fb310b7d023f 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -345,9 +345,10 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list long long signed_v; int written, width, len; unsigned int flags, ch_flag; - char outbuf[21]; + char outbuf[2 + 22 + 1]; char *out; const char *outstr; + unsigned int sign_prefix; written = 0; while (1) { @@ -446,32 +447,47 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list goto do_strlen_output; } - out = outbuf; + /* The 'sign_prefix' can be zero, one or two ("0x") characters. */ + sign_prefix = 0; if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i')) { /* "%d" and "%i" - signed decimal numbers. */ if (signed_v < 0) { - *out++ = '-'; + sign_prefix = '-'; v = -(signed_v + 1); v++; } } + /* The value is converted offset into the buffer so that + * the sign/prefix can be added in front. + * The longest digit string is 22 + 1 for octal conversions, the + * space is reserved even though octal isn't currently supported. + */ + out = outbuf + 2; + /* Convert the number to ascii in the required base. */ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i', 'u')) { /* Base 10 */ - u64toa_r(v, out); + len = u64toa_r(v, out); } else { /* Base 16 */ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'p')) { - *(out++) = '0'; - *(out++) = 'x'; + /* "%p" needs "0x" prepending. */ + sign_prefix = 'x' | '0' << 8; } - u64toh_r(v, out); + len = u64toh_r(v, out); } - outstr = outbuf; - goto do_strlen_output; + /* Add the 0, 1 or 2 ("0x") sign/prefix characters at the front. */ + for (; sign_prefix; sign_prefix >>= 8) { + /* Force gcc to increment len inside the loop. */ + _NOLIBC_OPTIMIZER_HIDE_VAR(len); + len++; + *--out = sign_prefix; + } + outstr = out; + goto do_output; } if (ch == 'm') { -- 2.39.5