From: Joe Perches <joe@perches.com>
To: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Cc: Grant Likely <grant.likely@linaro.org>,
Andrew Morton <akpm@linux-foundation.org>,
Andi Kleen <ak@linux.intel.com>,
Dan Carpenter <dan.carpenter@oracle.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/5] lib: string.c: Added a function strzcpy
Date: Sun, 19 Oct 2014 08:05:34 -0700 [thread overview]
Message-ID: <1413731134.14629.13.camel@perches.com> (raw)
In-Reply-To: <CAFo99gaLyzQoLAyBw9uEU=qiDnESH-rP1NL_+w8JLDw6BQMhaw@mail.gmail.com>
On Sun, 2014-10-19 at 14:19 +0200, Rickard Strandqvist wrote:
> 2014-10-19 3:38 GMT+02:00 Joe Perches <joe@perches.com>:
> > On Sun, 2014-10-19 at 00:03 +0200, Rickard Strandqvist wrote:
> >> Added a function strzcpy which works the same as strncpy,
> >> but guaranteed to produce the trailing null character.
> >>
> >> There are many places in the code where strncpy used although it
> >> must be zero terminated, and switching to strlcpy is not an option
> >> because the string must nonetheless be fyld with zero characters.
> > []
> >> diff --git a/lib/string.c b/lib/string.c
> > []
> >> +char *strzcpy(char *dest, const char *src, size_t count)
> >> +{
> >> + char *tmp = dest;
> >> +
> >> + while (count) {
> >> + if ((*tmp = *src) != 0)
> >> + src++;
> >> + tmp++;
> >> + count--;
> >> + }
> >> +
> >> + if (dest != tmp)
> >> + *--tmp = '\0';
> >> +
> >> + return dest;
> >> +}
> >
> > why not
> >
> > char *strzcpy(char *dest, const char *src, size_t count)
> > {
> > strncpy(dest, src, count)
> > if (count)
> > dest[count - 1] = 0; /* or '\0' or whatever */
> >
> > return dest;
> > }
> >
> > maybe use static inline too.
> >
>
> Hi Joe
>
> Yes this solution have also been discussed.
> https://lkml.org/lkml/2014/10/16/682
>
> Very possible that it is a better solution.
> The code that I use in strzcpy is not the way I'd written it, but is
> the same as in strncpy now.
>
> But as I understand it the real strncpy code is normally highly
> optimized for the hardware it runs on.
> Ex: arch/x86/lib/string_32.c
The fact that optimized strncpy variants exist
for any platform is the argument _for_ using it
in strzcpy.
> But missing for x86 64 bit and Arm..?
prev parent reply other threads:[~2014-10-19 15:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-18 22:03 [PATCH 1/5] lib: string.c: Added a function strzcpy Rickard Strandqvist
2014-10-19 1:38 ` Joe Perches
2014-10-19 12:19 ` Rickard Strandqvist
2014-10-19 15:05 ` Joe Perches [this message]
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=1413731134.14629.13.camel@perches.com \
--to=joe@perches.com \
--cc=ak@linux.intel.com \
--cc=akpm@linux-foundation.org \
--cc=dan.carpenter@oracle.com \
--cc=grant.likely@linaro.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