From: David Laight <david.laight.linux@gmail.com>
Extra tests include:
- %%, including ignored modifiers.
- Invalid formats copied to output buffer (matches glibc).
- Left aligned output "%-..."
- Zero padding "%0...".
- "(nil)" for NULL pointers (matches glibc).
- Alternate form "%#x" (prepends 0x in non-zero).
- Field precision as well as width, printf("%.0d", 0) is "".
- Variable length width and precision "%*.*d".
- Length qualifiers L and ll.
- Conversion specifiers i and X.
- More 'corner' cases.
There are no explicit tests of long (l, t or z) because they would
have to differ between 32bit and 64bit.
Set the expected length to zero for all the non-truncating tests.
(Annoying when a test is changed.)
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
tools/testing/selftests/nolibc/nolibc-test.c | 48 +++++++++++++++-----
1 file changed, 36 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 9378a1f26c34..bf92b9046175 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1565,6 +1565,7 @@ int run_stdlib(int min, int max)
#define EXPECT_VFPRINTF(c, expected, fmt, ...) \
ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
+__attribute__((mat(printf, 3, 0)))
static int expect_vfprintf(int llen, int c, const char *expected, const char *fmt, ...)
{
unsigned int i;
@@ -1727,20 +1728,43 @@ static int run_printf(int min, int max)
*/
switch (test + __LINE__ + 1) {
CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break;
- CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break;
- CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
- CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
- CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
- CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
- CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
- CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
- CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
- CASE_TEST(uintmax_t); EXPECT_VFPRINTF(20, "18446744073709551615", "%ju", 0xffffffffffffffffULL); break;
- CASE_TEST(intmax_t); EXPECT_VFPRINTF(20, "-9223372036854775807", "%jd", 0x8000000000000001LL); break;
+ CASE_TEST(simple); EXPECT_VFPRINTF(0, "foo", "foo"); break;
+ CASE_TEST(string); EXPECT_VFPRINTF(0, "foo", "%s", "foo"); break;
+ CASE_TEST(number); EXPECT_VFPRINTF(0, "1234", "%d", 1234); break;
+ CASE_TEST(negnumber); EXPECT_VFPRINTF(0, "-1234", "%d", -1234); break;
+ CASE_TEST(unsigned); EXPECT_VFPRINTF(0, "12345", "%u", 12345); break;
+ CASE_TEST(signed_max); EXPECT_VFPRINTF(0, "2147483647", "%i", ~0u >> 1); break;
+ CASE_TEST(signed_min); EXPECT_VFPRINTF(0, "-2147483648", "%i", (~0u >> 1) + 1); break;
+ CASE_TEST(unsigned_max); EXPECT_VFPRINTF(0, "4294967295", "%u", ~0u); break;
+ CASE_TEST(char); EXPECT_VFPRINTF(0, "|c|d| e|", "|%c|%.0c|%4c|", 'c', 'd', 'e'); break;
+ CASE_TEST(hex); EXPECT_VFPRINTF(0, "|f|d|", "|%x|%X|", 0xf, 0xd); break;
+ CASE_TEST(pointer); EXPECT_VFPRINTF(0, "0x1", "%p", (void *) 0x1); break;
+ CASE_TEST(pointer_NULL); EXPECT_VFPRINTF(0, "|(nil)|(nil)|", "|%p|%.4p|", (void *)0, (void *)0); break;
+ CASE_TEST(string_NULL); EXPECT_VFPRINTF(0, "|(null)||(null)|", "|%s|%.5s|%.6s|", (void *)0, (void *)0, (void *)0); break;
+ CASE_TEST(percent); EXPECT_VFPRINTF(0, "a%d42%69%", "a%%d%d%%%d%%", 42, 69); break;
+ CASE_TEST(perc_qual); EXPECT_VFPRINTF(0, "a%d2", "a%-14l%d%d", 2); break;
+ CASE_TEST(invalid); EXPECT_VFPRINTF(0, "a%12yx3%y42%y", "a%12yx%d%y%d%y", 3, 42); break;
+ CASE_TEST(intmax_max); EXPECT_VFPRINTF(0, "9223372036854775807", "%lld", ~0ULL >> 1); break;
+ CASE_TEST(intmax_min); EXPECT_VFPRINTF(0, "-9223372036854775808", "%Li", (~0ULL >> 1) + 1); break;
+ CASE_TEST(uintmax_max); EXPECT_VFPRINTF(0, "18446744073709551615", "%ju", ~0ULL); break;
CASE_TEST(truncation); EXPECT_VFPRINTF(25, "01234567890123456789", "%s", "0123456789012345678901234"); break;
- CASE_TEST(string_width); EXPECT_VFPRINTF(10, " 1", "%10s", "1"); break;
- CASE_TEST(number_width); EXPECT_VFPRINTF(10, " 1", "%10d", 1); break;
+ CASE_TEST(string_width); EXPECT_VFPRINTF(0, " 1", "%10s", "1"); break;
+ CASE_TEST(string_trunc); EXPECT_VFPRINTF(0, " 12345", "%10.5s", "1234567890"); break;
+ CASE_TEST(number_width); EXPECT_VFPRINTF(0, " 1", "%10d", 1); break;
+ CASE_TEST(number_left); EXPECT_VFPRINTF(0, "|-5 |", "|%-8d|", -5); break;
+ CASE_TEST(string_align); EXPECT_VFPRINTF(0, "|foo |", "|%-8s|", "foo"); break;
CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break;
+ CASE_TEST(width_tr_lft); EXPECT_VFPRINTF(25, "1 ", "%-25d", 1); break;
+ CASE_TEST(number_pad); EXPECT_VFPRINTF(0, "0000000005", "%010d", 5); break;
+ CASE_TEST(num_pad_neg); EXPECT_VFPRINTF(0, "-000000005", "%010d", -5); break;
+ CASE_TEST(num_pad_hex); EXPECT_VFPRINTF(0, "00fffffffb", "%010x", -5); break;
+ CASE_TEST(number_prec); EXPECT_VFPRINTF(0, " 00005", "%10.5d", 5); break;
+ CASE_TEST(num_prec_neg); EXPECT_VFPRINTF(0, " -00005", "%10.5d", -5); break;
+ CASE_TEST(num_prec_var); EXPECT_VFPRINTF(0, " -00005", "%*.*d", 10, 5, -5); break;
+ CASE_TEST(num_0_prec_0); EXPECT_VFPRINTF(0, "|| |+||||", "|%.0d|% .0d|%+.0d|%.0u|%.0x|%#.0x|", 0, 0, 0, 0, 0, 0); break;
+ CASE_TEST(hex_alt); EXPECT_VFPRINTF(0, "|0x1|0x01| 0x02|", "|%#x|%#04x|%#5.2x|", 1, 1, 2); break;
+ CASE_TEST(hex_0_alt); EXPECT_VFPRINTF(0, "|0|0000| 00|", "|%#x|%#04x|%#5.2x|", 0, 0, 0); break;
+ CASE_TEST(dec_alt); EXPECT_VFPRINTF(0, "|1|0001| 02|", "|%#d|%#04d|%#5.2d|", 1, 1, 2); break; /* '#' is ignored */
CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break;
CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break;
CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;
--
2.39.5
Hi,
kernel test robot noticed the following build warnings:
[auto build test WARNING on next-20260202]
url: https://github.com/intel-lab-lkp/linux/commits/david-laight-linux-gmail-com/tools-nolibc-printf-Move-length-check-to-snprintf-callback/20260203-183919
base: next-20260202
patch link: https://lore.kernel.org/r/20260203103000.20206-12-david.laight.linux%40gmail.com
patch subject: [PATCH 11/12] selftests/nolibc: Increase coverage of printf format tests
config: s390-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260204/202602040045.wtyCVh2h-lkp@intel.com/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project f43d6834093b19baf79beda8c0337ab020ac5f17)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260204/202602040045.wtyCVh2h-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202602040045.wtyCVh2h-lkp@intel.com/
All warnings (new ones prefixed by >>):
nolibc-test.c:784:9: warning: 'readdir_r' is deprecated [-Wdeprecated-declarations]
784 | ret = readdir_r(dir, &dirent, &result);
| ^
/usr/include/dirent.h:188:28: note: 'readdir_r' has been explicitly marked deprecated here
188 | __nonnull ((1, 2, 3)) __attribute_deprecated__;
| ^
/usr/include/s390x-linux-gnu/sys/cdefs.h:510:51: note: expanded from macro '__attribute_deprecated__'
510 | # define __attribute_deprecated__ __attribute__ ((__deprecated__))
| ^
nolibc-test.c:982:24: warning: self-comparison always evaluates to false [-Wtautological-compare]
982 | if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000)
| ^
nolibc-test.c:985:24: warning: self-comparison always evaluates to false [-Wtautological-compare]
985 | if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000)
| ^
nolibc-test.c:988:24: warning: self-comparison always evaluates to false [-Wtautological-compare]
988 | if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000)
| ^
nolibc-test.c:1444:54: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
1444 | CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break;
| ^~~~
/opt/cross/clang-f43d683409/lib/clang/22/include/__stddef_null.h:26:14: note: expanded from macro 'NULL'
26 | #define NULL ((void*)0)
| ^~~~~~~~~~
nolibc-test.c:369:22: note: expanded from macro 'EXPECT_SYSER'
369 | EXPECT_SYSER2(cond, expr, expret, experr, 0)
| ^~~~
nolibc-test.c:366:70: note: expanded from macro 'EXPECT_SYSER2'
366 | do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syserr2(expr, expret, experr1, experr2, llen); } while (0)
| ^~~~
>> nolibc-test.c:1652:16: warning: unknown attribute 'mat' ignored [-Wunknown-attributes]
1652 | __attribute__((mat(printf, 3, 0)))
| ^~~~~~~~~~~~~~~~~
6 warnings generated.
--
>> nolibc-test.c:1652:16: warning: unknown attribute 'mat' ignored [-Wunknown-attributes]
1652 | __attribute__((mat(printf, 3, 0)))
| ^~~~~~~~~~~~~~~~~
1 warning generated.
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Hi,
kernel test robot noticed the following build warnings:
[auto build test WARNING on next-20260202]
url: https://github.com/intel-lab-lkp/linux/commits/david-laight-linux-gmail-com/tools-nolibc-printf-Move-length-check-to-snprintf-callback/20260203-183919
base: next-20260202
patch link: https://lore.kernel.org/r/20260203103000.20206-12-david.laight.linux%40gmail.com
patch subject: [PATCH 11/12] selftests/nolibc: Increase coverage of printf format tests
config: x86_64-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260203/202602031944.7j98nfRE-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260203/202602031944.7j98nfRE-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202602031944.7j98nfRE-lkp@intel.com/
All warnings (new ones prefixed by >>):
nolibc-test.c: In function 'test_dirent':
nolibc-test.c:784:17: warning: 'readdir_r' is deprecated [-Wdeprecated-declarations]
784 | ret = readdir_r(dir, &dirent, &result);
| ^~~
In file included from nolibc-test.c:32:
/usr/include/dirent.h:185:12: note: declared here
185 | extern int readdir_r (DIR *__restrict __dirp,
| ^~~~~~~~~
nolibc-test.c: At top level:
>> nolibc-test.c:1654:1: warning: 'mat' attribute directive ignored [-Wattributes]
1654 | {
| ^
--
>> nolibc-test.c:1654:1: warning: 'mat' attribute directive ignored [-Wattributes]
1654 | {
| ^
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
On Tue, 3 Feb 2026 19:22:28 +0100
kernel test robot <lkp@intel.com> wrote:
> Hi,
>
> kernel test robot noticed the following build warnings:
>
> [auto build test WARNING on next-20260202]
>
> url: https://github.com/intel-lab-lkp/linux/commits/david-laight-linux-gmail-com/tools-nolibc-printf-Move-length-check-to-snprintf-callback/20260203-183919
> base: next-20260202
> patch link: https://lore.kernel.org/r/20260203103000.20206-12-david.laight.linux%40gmail.com
> patch subject: [PATCH 11/12] selftests/nolibc: Increase coverage of printf format tests
> config: x86_64-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260203/202602031944.7j98nfRE-lkp@intel.com/config)
> compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260203/202602031944.7j98nfRE-lkp@intel.com/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202602031944.7j98nfRE-lkp@intel.com/
>
> All warnings (new ones prefixed by >>):
>
> nolibc-test.c: In function 'test_dirent':
> nolibc-test.c:784:17: warning: 'readdir_r' is deprecated [-Wdeprecated-declarations]
> 784 | ret = readdir_r(dir, &dirent, &result);
> | ^~~
> In file included from nolibc-test.c:32:
> /usr/include/dirent.h:185:12: note: declared here
> 185 | extern int readdir_r (DIR *__restrict __dirp,
> | ^~~~~~~~~
> nolibc-test.c: At top level:
> >> nolibc-test.c:1654:1: warning: 'mat' attribute directive ignored [-Wattributes]
> 1654 | {
> | ^
> --
> >> nolibc-test.c:1654:1: warning: 'mat' attribute directive ignored [-Wattributes]
> 1654 | {
> | ^
>
Ah, a line I spotted reading the patches that shouldn't have been there.
Changed it expecting an error when I test-built the next 'round'.
For some reason it didn't fail for me (maybe it needs W=1?).
The line is just before expect_vfprintf() and I was looking into why the
tests weren't getting printf format errors and then thinking I could stop
gcc bleating about a missing format attribute - but that doesn't work
because the code is testing invalid formats.
David
© 2016 - 2026 Red Hat, Inc.