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

From: Li Zefan
Date: Sun Jan 17 2010 - 20:48:05 EST


Andrà Goddard Rosa wrote:
> As noticed by Alex Riesen at:
> http://marc.info/?l=linux-kernel&m=126364040821071&w=2
>

Please don't make this change, it's the user's responsibility
to make sure @len won't exceed @s1's length.

All the string functions of "n" version should work when
@s1 is not null-terminated, so don't call strlen() on @s1.

> Signed-off-by: Andrà Goddard Rosa <andre.goddard@xxxxxxxxx>
> cc: Li Zefan <lizf@xxxxxxxxxxxxxx>
> cc: Alex Riesen <raa.lkml@xxxxxxxxx>
> cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> ---
> lib/string.c | 4 +++-
> 1 files changed, 3 insertions(+), 1 deletions(-)
>
> 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;
> while (l1 >= l2) {
> l1--;
> if (!memcmp(s1, s2, l2))
--
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/