public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Li Zefan <lizf@cn.fujitsu.com>
To: "André Goddard Rosa" <andre.goddard@gmail.com>
Cc: linux-kernel@vger.kernel.org, torvalds@linux-foundation.org,
	Alex Riesen <raa.lkml@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH 2/2] string: teach strnstr() to not search past NUL-terminator
Date: Mon, 18 Jan 2010 09:47:49 +0800	[thread overview]
Message-ID: <4B53BDC5.1010201@cn.fujitsu.com> (raw)
In-Reply-To: <30e5fd087ee6fbe992cde8f09d945ed160b0c117.1263675077.git.andre.goddard@gmail.com>

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@gmail.com>
> cc: Li Zefan <lizf@cn.fujitsu.com>
> cc: Alex Riesen <raa.lkml@gmail.com>
> cc: Andrew Morton <akpm@linux-foundation.org>
> ---
>  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))

  parent reply	other threads:[~2010-01-18  1:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-16 20:56 [PATCH 0/2] Fix recently introduced strnstr() to not search past NUL André Goddard Rosa
2010-01-16 20:57 ` [PATCH 1/2] string: simplify stricmp() André Goddard Rosa
2010-01-23  0:27   ` Andrew Morton
2010-01-23  1:47     ` Linus Torvalds
2010-01-16 20:57 ` [PATCH 2/2] string: teach strnstr() to not search past NUL-terminator André Goddard Rosa
2010-01-17 23:47   ` Alex Riesen
2010-01-18  1:47   ` Li Zefan [this message]
2010-01-18 15:10     ` André Goddard Rosa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B53BDC5.1010201@cn.fujitsu.com \
    --to=lizf@cn.fujitsu.com \
    --cc=akpm@linux-foundation.org \
    --cc=andre.goddard@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=raa.lkml@gmail.com \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox