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 DA968396B85 for ; Mon, 2 Mar 2026 10:18:53 +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=1772446741; cv=none; b=uvi0FzaRHgRbY9FZIJh+Ep3E6GXuZzvCJBSDGA9zT7CPXailjoyz44NjxbwQOu8A3T3EVinbiKDtzTN2QcsBelc8+/RieFlAVeFrOkViSizM2ZNW8yBnWOyV9TunOyHNp8k3BePuOwzZekwGmTjFxkhPCkzTGuG8fhpkd+C/Fi0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772446741; c=relaxed/simple; bh=QZcNFuzhE6cuFx0DsYKprPH5ZBYEDDfrMhyPdeZRuXY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PZnQJuW/4TCwxLYu3j/rY0QAV8W0MrZtzYwEKRvreN0iECeTyNjenq4DaAiqNmk1+FzF1E1FUJcTarw2r8ynA3H5Wg34t046ZvK/FIm0Gp3vrbu6c3Rtk4soW0l3NbbcpExyzlS7u/HAmwnjFYWcvg4eWO9I+Zbaqv1tWHLtWPM= 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=LkiAu21s; 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="LkiAu21s" 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-003Bbf-QO; 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=TwHJ2KlZpg0cbG3t4PlbVej/YSrEwJyR73TCnhv4BqA=; b=LkiAu21sxWPrhbUyRpa57dH1VB AxpmjH90ByqP/dAZN+Lf+mqqXjUnfcoVmUDY7W8ss7/hZKNY/OFgMcmJ9EyNAiWULyaX5Cs9FJjSo rN9D/U+ogJ4OdUWmVsfqUn7aUSQhXbJjFJN01lt1a2zd/EiAZ7WmM2AtZaAVJO3mlXbUNg/UIugO2 UcTRyhpFYtP4gsqGfjMjuZ7LQyr/pCo1dD9k6f/7o3ncKwMLVvBUKrqVEld4Fp3OxxBxTfDAvMZlV 5bEepyDu0sQokYA4JeLTl3As4gRbCj1pL07EAvEVFO0+VZPCbVcTPIaFv4kk0qMt3bSJ0CHhKTxbV mMwzpNVw==; Received: from [10.9.9.72] (helo=submission01.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vx0MU-00055i-GJ; 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-Cr; 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 v4 next 15/23] tools/nolibc/printf: Add support for length modifiers tzqL and formats iX Date: Mon, 2 Mar 2026 10:18:07 +0000 Message-Id: <20260302101815.3043-16-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 Length modifiers t (ptrdiff_t) and z (size_t) are aliases for l (long), q and L are 64bit the same as j (intmax). Format i is an alias for d and X similar to x but uppper case. Supporting them is mostly just adding the relavant bit to the bit pattern used for maching characters. Although %X is detected the output will be lower case. Change/add tests to use conversions i and X, and length modifiers L and ll. Use the correct minimum value for "%Li". Acked-by: Willy Tarreau Signed-off-by: David Laight --- v4: Split from the previous patch. tools/include/nolibc/stdio.h | 27 ++++++++++++-------- tools/testing/selftests/nolibc/nolibc-test.c | 14 +++++++--- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index ea1288d87eea..4c7626dbd63f 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -291,10 +291,11 @@ int fseek(FILE *stream, long offset, int whence) } -/* printf(). Supports the following integer and string formats. - * - %[#-+ 0][width][{l,ll,j}]{c,d,u,x,p,s,m,%} +/* printf(). Supports most of the normal integer and string formats. + * - %[#-+ 0][width][{l,t,z,ll,L,j,q}]{c,d,i,u,x,X,p,s,m,%} * - %% generates a single % * - %m outputs strerror(errno). + * - %X outputs a..f the same as %x. * - The modifiers [#-+ 0] are currently ignored. * - No support for precision or variable widths. * - No support for floating point or wide characters. @@ -386,9 +387,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list /* Length modifier. * They miss the conversion flags characters " #+-0" so can go into flags. - * Change ll to j (both always 64bits). + * Change both L and ll to j (all alwys 64bit). */ - ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'l', 'j'); + if (ch == 'L') + ch = 'j'; + ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'l', 't', 'z', 'j', 'q'); if (ch_flag != 0) { if (ch == 'l' && fmt[0] == 'l') { fmt++; @@ -403,20 +406,22 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list /* Numeric and pointer conversion specifiers. * * Use an explicit bound check (rather than _NOLIBC_PF_CHAR_IS_ONE_OF()) - * so ch_flag can be used later. + * so that 'X' can be allowed through. + * 'X' gets treated and 'x' because _NOLIBC_PF_FLAG() returns the same + * value for both. */ ch_flag = _NOLIBC_PF_FLAG(ch); - if ((ch >= 'a' && ch <= 'z') && - _NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'c', 'd', 'u', 'x', 'p')) { + if (((ch >= 'a' && ch <= 'z') || ch == 'X') && + _NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'c', 'd', 'i', 'u', 'x', 'p')) { /* 'long' is needed for pointer conversions and ltz lengths. * A single test can be used provided 'p' (the same bit as '0') * is masked from flags. */ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag | (flags & ~_NOLIBC_PF_FLAG('p')), - 'p', 'l')) { + 'p', 'l', 't', 'z')) { v = va_arg(args, unsigned long); signed_v = (long)v; - } else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, 'j')) { + } else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, 'j', 'q')) { v = va_arg(args, unsigned long long); signed_v = v; } else { @@ -434,7 +439,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list out = outbuf; - if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd')) { + if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i')) { /* "%d" and "%i" - signed decimal numbers. */ if (signed_v < 0) { *out++ = '-'; @@ -444,7 +449,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list } /* Convert the number to ascii in the required base. */ - if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'u')) { + if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i', 'u')) { /* Base 10 */ u64toa_r(v, out); } else { diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 638f18fc5123..a140c54e4d72 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1826,11 +1826,19 @@ static int run_printf(int min, int max) CASE_TEST(number); EXPECT_VFPRINTF(1, "1234", "%d", 1234); break; CASE_TEST(negnumber); EXPECT_VFPRINTF(1, "-1234", "%d", -1234); break; CASE_TEST(unsigned); EXPECT_VFPRINTF(1, "12345", "%u", 12345); break; + CASE_TEST(signed_max); EXPECT_VFPRINTF(1, "2147483647", "%i", ~0u >> 1); break; + CASE_TEST(signed_min); EXPECT_VFPRINTF(1, "-2147483648", "%i", (~0u >> 1) + 1); break; + CASE_TEST(unsigned_max); EXPECT_VFPRINTF(1, "4294967295", "%u", ~0u); break; CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break; - CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break; + CASE_TEST(hex_nolibc); EXPECT_VFPRINTF(is_nolibc, "|f|d|", "|%x|%X|", 0xf, 0xd); break; + CASE_TEST(hex_libc); EXPECT_VFPRINTF(!is_nolibc, "|f|D|", "|%x|%X|", 0xf, 0xd); break; CASE_TEST(pointer); EXPECT_VFPRINTF(1, "0x1", "%p", (void *) 0x1); break; - CASE_TEST(uintmax_t); EXPECT_VFPRINTF(1, "18446744073709551615", "%ju", 0xffffffffffffffffULL); break; - CASE_TEST(intmax_t); EXPECT_VFPRINTF(1, "-9223372036854775807", "%jd", 0x8000000000000001LL); break; + CASE_TEST(percent); EXPECT_VFPRINTF(1, "a%d42%69%", "a%%d%d%%%d%%", 42, 69); break; + CASE_TEST(perc_qual); EXPECT_VFPRINTF(1, "a%d2", "a%-14l%d%d", 2); break; + CASE_TEST(invalid); EXPECT_VFPRINTF(1, "a%12yx3%y42%P", "a%12yx%d%y%d%P", 3, 42); break; + CASE_TEST(intmax_max); EXPECT_VFPRINTF(1, "9223372036854775807", "%lld", ~0ULL >> 1); break; + CASE_TEST(intmax_min); EXPECT_VFPRINTF(1, "-9223372036854775808", "%Li", (~0ULL >> 1) + 1); break; + CASE_TEST(uintmax_max); EXPECT_VFPRINTF(1, "18446744073709551615", "%ju", ~0ULL); break; CASE_TEST(truncation); EXPECT_VFPRINTF(1, "0123456789012345678901234", "%s", "0123456789012345678901234"); break; CASE_TEST(string_width); EXPECT_VFPRINTF(1, " 1", "%10s", "1"); break; CASE_TEST(number_width); EXPECT_VFPRINTF(1, " 1", "%10d", 1); break; -- 2.39.5