[PATCH v3 next 03/17] selftests/nolibc: Fix build with host headers and libc

From: david . laight . linux

Date: Mon Feb 23 2026 - 05:39:01 EST


From: David Laight <david.laight.linux@xxxxxxxxx>

Many systems don't have strlcpy() or strlcat() and readdir_r() is deprecated.
This makes the tests fail to build with the host headers.
Disable the 'directories' test and define strlcpy(), strlcat() and
readdir_r() using #defines so that the code compiles.

Fixes: 6fe8360b16acb ("selftests/nolibc: also test libc-test through regular selftest framework")
Signed-off-by: David Laight <david.laight.linux@xxxxxxxxx>
---

For v3 (no v2):
- Now part of the patch series that enhances _nolibc_printf().
- Disable the test that uses readdir_r() and #defines readdir_r()
instead of disabling compiler warnings.
- No need to change is_nolibc, doesn't help.

Basically the nolibc-test program makefiles don't work without this change.

tools/testing/selftests/nolibc/nolibc-test.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 1b9d3b2e2491..0e8b3b9a86ef 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -74,6 +74,20 @@ static const int is_nolibc =
#endif
;

+#if !defined(NOLIBC)
+/* Some disabled tests may not compile. */
+
+/* strlcat() and strlcpy() may not be in the system headers. */
+#undef strlcat
+#undef strlcpy
+#define strlcat(d, s, l) 0
+#define strlcpy(d, s, l) 0
+
+/* readdir_r() is likely to be marked deprecated */
+#undef readdir_r
+#define readdir_r(dir, dirent, result) ((errno = EINVAL), -1)
+#endif
+
/* definition of a series of tests */
struct test {
const char *name; /* test name */
@@ -1408,7 +1422,7 @@ int run_syscall(int min, int max)
CASE_TEST(fork); EXPECT_SYSZR(1, test_fork(FORK_STANDARD)); break;
CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
- CASE_TEST(directories); EXPECT_SYSZR(proc, test_dirent()); break;
+ CASE_TEST(directories); EXPECT_SYSZR(is_nolibc && proc, test_dirent()); break;
CASE_TEST(getrandom); EXPECT_SYSZR(1, test_getrandom()); break;
CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
--
2.39.5