Re: [PATCH 2/2] string: teach strnstr() to not search past NUL-terminator

From: Alex Riesen
Date: Sun Jan 17 2010 - 18:56:21 EST


On Sat, Jan 16, 2010 at 21:57, Andrà Goddard Rosa
<andre.goddard@xxxxxxxxx> wrote:
> diff --git a/lib/string.c b/lib/string.c
> index 0f86245..94bad34 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -689,11 +689,13 @@ EXPORT_SYMBOL(strstr);
> Â*/
> Âchar *strnstr(const char *s1, const char *s2, size_t len)
> Â{
> - Â Â Â size_t l1 = len, l2;
> + Â Â Â size_t l1, l2;
>
> Â Â Â Âl2 = strlen(s2);
> Â Â Â Âif (!l2)
> Â Â Â Â Â Â Â Âreturn (char *)s1;
> + Â Â Â l1 = strlen(s1);
> + Â Â Â l1 = (l1 <= len) ? l1 : len;

if (l1 > len)
l1 = len;

Less code and easier to read.
--
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/