]> Gentwo Git Trees - linux/.git/commitdiff
tools/nolibc: add missing memchr() to string.h
authorWilly Tarreau <w@1wt.eu>
Sun, 2 Nov 2025 10:46:10 +0000 (11:46 +0100)
committerThomas Weißschuh <linux@weissschuh.net>
Sun, 2 Nov 2025 11:11:48 +0000 (12:11 +0100)
Surprisingly we forgot to add this common one. It was added with a
per-arch guard allowing to later implement it in arch-specific asm
code like was done for a few other ones.

The test verifies that we don't search past the indicated length.

Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/include/nolibc/string.h
tools/testing/selftests/nolibc/nolibc-test.c

index 163a17e7dd38b48ba8d9218e10733b3600710477..4000926f44ac40cba06a464baf5d0ddfdde85ec8 100644 (file)
@@ -93,6 +93,21 @@ void *memset(void *dst, int b, size_t len)
 }
 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */
 
+#ifndef NOLIBC_ARCH_HAS_MEMCHR
+static __attribute__((unused))
+void *memchr(const void *s, int c, size_t len)
+{
+       char *p = (char *)s;
+
+       while (len--) {
+               if (*p == (char)c)
+                       return p;
+               p++;
+       }
+       return NULL;
+}
+#endif /* #ifndef NOLIBC_ARCH_HAS_MEMCHR */
+
 static __attribute__((unused))
 char *strchr(const char *s, int c)
 {
index f1fc00d6fef5f59b4bbe4bd952fe01bc3ad26345..4b3c501ae434bed1c107e5de0ccc54936c2cb878 100644 (file)
@@ -1549,6 +1549,8 @@ int run_stdlib(int min, int max)
                CASE_TEST(abs);                     EXPECT_EQ(1, abs(-10), 10); break;
                CASE_TEST(abs_noop);                EXPECT_EQ(1, abs(10), 10); break;
                CASE_TEST(difftime);                EXPECT_ZR(1, test_difftime()); break;
+               CASE_TEST(memchr_foobar6_o);        EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break;
+               CASE_TEST(memchr_foobar3_b);        EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break;
 
                case __LINE__:
                        return ret; /* must be last */