[PATCH 1/3] lib: implement strnstr()

Jerome Forissier jerome.forissier at linaro.org
Mon Jan 6 08:44:24 CET 2025



On 1/4/25 00:21, Heinrich Schuchardt wrote:
> Implement library function strnstr().
> Implement strstr() using strnstr().
> Sort the includes.
> 
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
> ---
>  include/linux/string.h |  3 +++
>  lib/string.c           | 49 +++++++++++++++++++++++++++++-------------
>  2 files changed, 37 insertions(+), 15 deletions(-)
> 
> diff --git a/include/linux/string.h b/include/linux/string.h
> index 27b2beb9ddb..8a377a8176a 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -72,6 +72,9 @@ extern char * strrchr(const char *,int);
>  #ifndef __HAVE_ARCH_STRSTR
>  extern char * strstr(const char *,const char *);
>  #endif
> +#ifndef __HAVE_ARCH_STRNSTR
> +extern char *strnstr(const char *, const char *, size_t);
> +#endif
>  #ifndef __HAVE_ARCH_STRLEN
>  extern __kernel_size_t strlen(const char *);
>  #endif
> diff --git a/lib/string.c b/lib/string.c
> index feae9519f2f..d5a486fb89d 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -16,11 +16,12 @@
>   */
>  
>  #include <config.h>
> +#include <malloc.h>
> +#include <stdint.h>
>  #include <linux/compiler.h>
> +#include <linux/ctype.h>
>  #include <linux/types.h>
>  #include <linux/string.h>
> -#include <linux/ctype.h>
> -#include <malloc.h>
>  
>  /**
>   * strncasecmp - Case insensitive, length-limited string comparison
> @@ -678,30 +679,48 @@ char *memdup(const void *src, size_t len)
>  	return p;
>  }
>  
> -#ifndef __HAVE_ARCH_STRSTR
> +#ifndef __HAVE_ARCH_STRNSTR
>  /**
> - * strstr - Find the first substring in a %NUL terminated string
> - * @s1: The string to be searched
> - * @s2: The string to search for
> + * strnstr() - find the first substring occurrence in a NUL terminated string
> + *
> + * @s1:		string to be searched
> + * @s2:		string to search for
> + * @len:	maximum number of characters in s2 to consider

You mean s1?

> + *
> + * Return:	pointer to the first occurrence or NULL
>   */
> -char * strstr(const char * s1,const char * s2)
> +char *strnstr(const char *s1, const char *s2, size_t len)
>  {
> -	int l1, l2;
> +	size_t l1, l2;
>  
> +	l1 = strnlen(s1, len);
>  	l2 = strlen(s2);
> -	if (!l2)
> -		return (char *) s1;
> -	l1 = strlen(s1);
> -	while (l1 >= l2) {
> -		l1--;
> -		if (!memcmp(s1,s2,l2))
> +
> +	for (; l1 >= l2; --l1, ++s1) {
> +		if (!memcmp(s1, s2, l2))
>  			return (char *) s1;
> -		s1++;
>  	}
> +
>  	return NULL;
>  }
>  #endif

This won't return s1 when s2 == NULL, will it?
Why not use a known good implementation such as [1]?

[1] https://github.com/freebsd/freebsd-src/blob/main/lib/libc/string/strnstr.c

>  
> +#ifndef __HAVE_ARCH_STRSTR
> +/**
> + * strstr() - find the first substring occurrence in a NUL terminated string
> + *
> + * @s1:		string to be searched
> + * @s2:		string to search for
> + * @len:	maximum number of characters in s2 to consider

There is no len parameter

> + *
> + * Return:	pointer to the first occurrence or NULL
> + */
> +char *strstr(const char *s1, const char *s2)
> +{
> +	return strnstr(s1, s2, SIZE_MAX);
> +}
> +#endif
> +
>  #ifndef __HAVE_ARCH_MEMCHR
>  /**
>   * memchr - Find a character in an area of memory.

Thanks,
-- 
Jerome


More information about the U-Boot mailing list