]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/nolibc/printf: Add support for length modifiers tzqL and formats iX
authorDavid Laight <david.laight.linux@gmail.com>
Sun, 8 Mar 2026 11:37:34 +0000 (11:37 +0000)
committerThomas Weißschuh <linux@weissschuh.net>
Fri, 20 Mar 2026 16:55:28 +0000 (17:55 +0100)
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 upper case.
Supporting them is mostly just adding the relevant bit to the bit
pattern used for matching 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".

Signed-off-by: David Laight <david.laight.linux@gmail.com>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260308113742.12649-10-david.laight.linux@gmail.com
[Thomas: Fix up testcases for musl libc]
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/include/nolibc/stdio.h
tools/testing/selftests/nolibc/nolibc-test.c

index f5a176bffc936fee7e15caff139f7cd4b84a2882..7382b00707fb89e760c393d9b1d910b3e34ebfda 100644 (file)
@@ -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 always 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 {
index d8949aa1dee3c895272ad81c0ea298bfc8c92a13..6fa2346c655a741997bfd69ea2d9b20fe21c410c 100644 (file)
@@ -1834,11 +1834,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(is_nolibc || is_glibc, "a%d2", "a%-14l%d%d", 2); break;
+               CASE_TEST(invalid);      EXPECT_VFPRINTF(is_nolibc || is_glibc, "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(is_nolibc || is_glibc, "-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;