From: Willy Tarreau <w@1wt.eu>
To: linux@weissschuh.net
Cc: shuah@kernel.org, linux-kernel@vger.kernel.org, Willy Tarreau <w@1wt.eu>
Subject: [PATCH v2 3/4] tools/nolibc: add missing memchr() to string.h
Date: Sun, 2 Nov 2025 11:46:10 +0100 [thread overview]
Message-ID: <20251102104611.24454-4-w@1wt.eu> (raw)
In-Reply-To: <20251102104611.24454-1-w@1wt.eu>
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>
---
tools/include/nolibc/string.h | 15 +++++++++++++++
tools/testing/selftests/nolibc/nolibc-test.c | 2 ++
2 files changed, 17 insertions(+)
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
index 163a17e7dd38..4000926f44ac 100644
--- a/tools/include/nolibc/string.h
+++ b/tools/include/nolibc/string.h
@@ -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)
{
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 2b1fcfaaa60e..7fc937e9a4df 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1558,6 +1558,8 @@ int run_stdlib(int min, int max)
CASE_TEST(abs_noop); EXPECT_EQ(1, abs(10), 10); break;
CASE_TEST(difftime); EXPECT_ZR(1, test_difftime()); break;
CASE_TEST(test_timespec); EXPECT_ZR(1, test_timespec()); 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 */
--
2.17.5
next prev parent reply other threads:[~2025-11-02 10:46 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-02 10:46 [PATCH v2 0/4] tools/nolibc: assorted fixes and small updates Willy Tarreau
2025-11-02 10:46 ` [PATCH v2 1/4] tools/nolibc: fix misleading help message regarding installation path Willy Tarreau
2025-11-02 11:03 ` Thomas Weißschuh
2025-11-02 10:46 ` [PATCH v2 2/4] tools/nolibc: add the more portable inttypes.h Willy Tarreau
2025-11-02 11:04 ` Thomas Weißschuh
2025-11-02 11:18 ` Willy Tarreau
2025-11-02 11:41 ` Thomas Weißschuh
2025-11-02 10:46 ` Willy Tarreau [this message]
2025-11-02 11:10 ` [PATCH v2 3/4] tools/nolibc: add missing memchr() to string.h Thomas Weißschuh
2025-11-02 10:46 ` [PATCH v2 4/4] tools/nolibc: provide the portable sys/select.h Willy Tarreau
2025-11-02 12:51 ` Thomas Weißschuh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251102104611.24454-4-w@1wt.eu \
--to=w@1wt.eu \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®