Re: [PATCH 4/7] lib: Introduce strnstr()

From: Alex Riesen
Date: Sat Jan 16 2010 - 06:13:17 EST


On Thu, Jan 14, 2010 at 03:53, Li Zefan <lizf@xxxxxxxxxxxxxx> wrote:
> @@ -667,7 +667,7 @@ EXPORT_SYMBOL(memscan);
> Â*/
> Âchar *strstr(const char *s1, const char *s2)
> Â{
> - Â Â Â int l1, l2;
> + Â Â Â size_t l1, l2;
>

This chunk is not related, is it?

> @@ -684,6 +684,31 @@ char *strstr(const char *s1, const char *s2)
> ÂEXPORT_SYMBOL(strstr);
> Â#endif
>
> +#ifndef __HAVE_ARCH_STRNSTR
> +/**
> + * strnstr - Find the first substring in a length-limited string
> + * @s1: The string to be searched
> + * @s2: The string to search for
> + * @len: the maximum number of characters to search
> + */
> +char *strnstr(const char *s1, const char *s2, size_t len)
> +{
> + Â Â Â size_t l1 = len, l2;

Are you sure you want to search _past_ the NUL-terminator
of s1?

> + Â Â Â l2 = strlen(s2);
> + Â Â Â if (!l2)
> + Â Â Â Â Â Â Â return (char *)s1;
> + Â Â Â while (l1 >= l2) {
> + Â Â Â Â Â Â Â l1--;
> + Â Â Â Â Â Â Â if (!memcmp(s1, s2, l2))
> + Â Â Â Â Â Â Â Â Â Â Â return (char *)s1;
> + Â Â Â Â Â Â Â s1++;
> + Â Â Â }
> + Â Â Â return NULL;
> +}
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/