Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/nolibc: add missing memchr() to string.h

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>

authored by

Willy Tarreau and committed by
Thomas Weißschuh
db75042e 09c873c9

+17
+15
tools/include/nolibc/string.h
··· 93 93 } 94 94 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */ 95 95 96 + #ifndef NOLIBC_ARCH_HAS_MEMCHR 97 + static __attribute__((unused)) 98 + void *memchr(const void *s, int c, size_t len) 99 + { 100 + char *p = (char *)s; 101 + 102 + while (len--) { 103 + if (*p == (char)c) 104 + return p; 105 + p++; 106 + } 107 + return NULL; 108 + } 109 + #endif /* #ifndef NOLIBC_ARCH_HAS_MEMCHR */ 110 + 96 111 static __attribute__((unused)) 97 112 char *strchr(const char *s, int c) 98 113 {
+2
tools/testing/selftests/nolibc/nolibc-test.c
··· 1549 1549 CASE_TEST(abs); EXPECT_EQ(1, abs(-10), 10); break; 1550 1550 CASE_TEST(abs_noop); EXPECT_EQ(1, abs(10), 10); break; 1551 1551 CASE_TEST(difftime); EXPECT_ZR(1, test_difftime()); break; 1552 + CASE_TEST(memchr_foobar6_o); EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break; 1553 + CASE_TEST(memchr_foobar3_b); EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break; 1552 1554 1553 1555 case __LINE__: 1554 1556 return ret; /* must be last */