linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Alexey Dobriyan' <adobriyan@gmail.com>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-fsdevel@vger.kernel.org" <linux-fsdevel@vger.kernel.org>
Subject: RE: [PATCH] proc: fold kmalloc() + strcpy() into kmemdup()
Date: Mon, 9 Sep 2024 15:13:04 +0000	[thread overview]
Message-ID: <fd47776572944caf8f720e7d429b5e05@AcuMS.aculab.com> (raw)
In-Reply-To: <90af27c1-0b86-47a6-a6c8-61a58b8aa747@p183>

From: Alexey Dobriyan
> Sent: 08 September 2024 10:28
> 
> strcpy() will recalculate string length second time which is
> unnecessary in this case.

There is also definitely scope for the string being changed.
Maybe you can prove it doesn't happen?

Which also means the code would be better explicitly writing
the terminating '\0' rather than relying on the one from the
input buffer.

	David

> 
> Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
> ---
> 
>  fs/proc/generic.c |    4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> --- a/fs/proc/generic.c
> +++ b/fs/proc/generic.c
> @@ -464,9 +464,9 @@ struct proc_dir_entry *proc_symlink(const char *name,
>  			  (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
> 
>  	if (ent) {
> -		ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
> +		ent->size = strlen(dest);
> +		ent->data = kmemdup(dest, ent->size + 1, GFP_KERNEL);
>  		if (ent->data) {
> -			strcpy((char*)ent->data,dest);
>  			ent->proc_iops = &proc_link_inode_operations;
>  			ent = proc_register(parent, ent);
>  		} else {

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

  parent reply	other threads:[~2024-09-09 15:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-08  9:27 [PATCH] proc: fold kmalloc() + strcpy() into kmemdup() Alexey Dobriyan
2024-09-08 11:24 ` Markus Elfring
2024-09-09  8:52 ` Christian Brauner
2024-09-09 15:13 ` David Laight [this message]
2024-09-11 10:42   ` Alexey Dobriyan

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=fd47776572944caf8f720e7d429b5e05@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).