public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Peng Tao <bergwolf@gmail.com>,
	devel@driverdev.osuosl.org,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] staging: lustre: lustre: libcfs: debug.c: Cleaning up unnecessary use of memset in conjunction with strncpy
Date: Thu, 18 Sep 2014 11:51:05 +0300	[thread overview]
Message-ID: <20140918085105.GB16827@mwanda> (raw)
In-Reply-To: <CAFo99gZUiSB2cgSAAj7EBjZJTs7QxvCDZr6Wms3ud5qQvBWQMw@mail.gmail.com>

On Thu, Sep 18, 2014 at 12:12:42AM +0200, Rickard Strandqvist wrote:
> Hi Dan
> 
> Ok, I have made two suggestions for strncpy function that also
> guarantees a terminating null character.
> 1) retunerar number of characters to be copied, it can be good to
> have, but was not really satisfied.

strlcpy() is more popular the strncpy() in the kernel.

No one uses the return value of strncpy() because what is the point?
There are around 15-20 places which use the return value of strlcpy().
Some of the place which use the return value assume that the copy fits.

I think we should return the number of bytes before the NUL or else
count.

> 
> int strncpyz(char *dest, const char *src, size_t count)
> {
>   size_t len=0;
> 
>   if(0 == count)
>     return 0;
> 
>   --count;
>   while(len < count && src[len])
>    *dest++ = src[len++];
> 
>   do {
>     *dest++ = '\0';
>   }
>   while(len < count--);
> 
>   return len;
> }
> 
> 
> 2) The next version is almost the same code as the regular strncpy,
> but with two extra lines.
> 
> char *strncpyz(char *dest, const char *src, size_t count)
> {
>         char *tmp = dest;
> 
>         while (count) {
>                 if ((*tmp = *src) != 0)
>                         src++;
>                 tmp++;
>                 count--;
>         }
> 
>         if(tmp != dest)
>           *--tmp = '\0';
> 
>         return dest;
> }
> 
> 
> Since I did not got any better solution to variant 1, I prefer variant 2.

I also prefer variant 2.

> 
> Then the next question is of course what it should be called  :-)

I think a lot of people call this function strzcpy().

This sort of patch would go through Andrew Morton.

regards,
dan carpenter


  reply	other threads:[~2014-09-18  8:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-14 16:03 [PATCH] staging: lustre: lustre: libcfs: debug.c: Cleaning up unnecessary use of memset in conjunction with strncpy Rickard Strandqvist
2014-09-15  8:23 ` Dan Carpenter
2014-09-17 22:12   ` Rickard Strandqvist
2014-09-18  8:51     ` Dan Carpenter [this message]
2014-09-18 19:57       ` Rickard Strandqvist
2014-09-18 22:39         ` Dan Carpenter
2014-10-14 21:34           ` Rickard Strandqvist

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=20140918085105.GB16827@mwanda \
    --to=dan.carpenter@oracle.com \
    --cc=bergwolf@gmail.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rickard_strandqvist@spectrumdigital.se \
    /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