Re: [PATCH 4/4] tools/nolibc: add missing memchr() to string.h

From: Thomas Weißschuh
Date: Sat Jun 21 2025 - 04:27:23 EST


On 2025-06-20 12:02:51+0200, Willy Tarreau wrote:
> 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@xxxxxx>
> ---
> 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 163a17e7dd38b..4000926f44ac4 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

So far we only have added these guards when necessary,
which they aren't here. Can we drop them?

> +static __attribute__((unused))
> +void *memchr(const void *s, int c, size_t len)
> +{
> + char *p = (char *)s;

The docs say that they are interpreted as "unsigned char".
Also, can we keep the const?

> +
> + 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)
> {

<snip>