netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Felipe Alfaro Solana <felipe_alfaro@linuxmail.org>
To: "YOSHIFUJI Hideaki / 吉藤英明" <yoshfuji@linux-ipv6.org>
Cc: rmk+lkml@arm.linux.org.uk, davem@redhat.com,
	Linux Kernel Mailinglist <linux-kernel@vger.kernel.org>,
	netdev@oss.sgi.com
Subject: Re: [PATCH 2.6]: IPv6: strcpy -> strlcpy
Date: Fri, 28 Nov 2003 10:54:33 +0100	[thread overview]
Message-ID: <1070013272.2144.4.camel@teapot.felipe-alfaro.com> (raw)
In-Reply-To: <20031128.094022.106776635.yoshfuji@linux-ipv6.org>

On Fri, 2003-11-28 at 01:40, YOSHIFUJI Hideaki / 吉藤英明 wrote:
> In article <20031128.092642.47232575.yoshfuji@linux-ipv6.org> (at Fri, 28 Nov 2003 09:26:42 +0900 (JST)), YOSHIFUJI Hideaki / 吉藤英明 <yoshfuji@linux-ipv6.org> says:
> 
> > >  3)   if (len)
> > >          strncpy(dst, src, len - 1);
> > >       dst[len] = 0;
> 
> grr, another mistake...:
> 
>           if (len) {
>              strncpy(dst, src, len - 1);
>              dst[len - 1];
>           }
> 
> ----------------
> 1) use strlcpy0(dst, src, len)
> 
> size_t strlcpy0(char *dst, const char *src, size_t maxlen)
> {
>   size_t len = strlcpy(dst, src, maxlen);
>   if (likely(maxlen != 0) && len < maxlen - 1)
>     memset(dst + len + 1, 0, maxlen - len - 1);
> }
> 
> 2a) use strncpy0(dst, src, len)
> 
> char *strncpy0(char *dst, const char *src, size_t maxlen)
> {
>   memset(dst, 0, maxlen);
>   if (likely(maxlen != 0))
>     strncpy(dst, src, maxlen - 1);
> }
> 
> 2b) fix strncpy() to zero-out rest of destination buffer 
>     and use strncpy0(dst, src, len)
> 
> char *strncpy0(char *dst, const char *src, size_t maxlen)
> {
>   if (likely(maxlen != 0)) {
>     strncpy(dst, src, maxlen - 1);
>     dst[maxlen - 1] = 0;
>   }
> }
> 
> 
> I prefer 1 > 2b >> 2a.

What about coding a new function str0cpy()?

/**
 * str0cpy - Copy a length-limited, %NUL-terminated string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 * @count: The maximum number of bytes to copy
 */
char * str0cpy(char * dest,const char *src,size_t count)
{
        if (count) {
                char *tmp = dest;

                count--;
                while (count) {
                        if ((*tmp = *src) != 0) src++;
                        tmp++;
                        count--;
                }
                *tmp = 0;
        }
        return dest;
}

It behaves like strncpy() but correctly null-terminates the string in
every case.

  reply	other threads:[~2003-11-28  9:54 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1069920883.2476.1.camel@teapot.felipe-alfaro.com>
2003-11-27  8:33 ` [PATCH 2.6]: IPv6: strcpy -> strlcpy YOSHIFUJI Hideaki / 吉藤英明
2003-11-27 10:59   ` David S. Miller
2003-11-27 12:04     ` Felipe Alfaro Solana
2003-11-27 12:09       ` YOSHIFUJI Hideaki / 吉藤英明
2003-11-27 19:46         ` Russell King
2003-11-27 19:54           ` YOSHIFUJI Hideaki / 吉藤英明
2003-11-27 20:00             ` Russell King
2003-11-27 20:47               ` YOSHIFUJI Hideaki / 吉藤英明
2003-11-27 22:06               ` Felipe Alfaro Solana
2003-11-27 22:19                 ` Russell King
2003-11-27 22:33                   ` Russell King
2003-11-28  1:34                     ` Mitchell Blank Jr
2003-11-27 23:03                   ` Felipe Alfaro Solana
2003-11-28  0:23                     ` YOSHIFUJI Hideaki / 吉藤英明
2003-11-28  0:26                       ` YOSHIFUJI Hideaki / 吉藤英明
2003-11-28  0:40                         ` YOSHIFUJI Hideaki / 吉藤英明
2003-11-28  9:54                           ` Felipe Alfaro Solana [this message]
2003-11-28 11:22                           ` Jörn Engel

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=1070013272.2144.4.camel@teapot.felipe-alfaro.com \
    --to=felipe_alfaro@linuxmail.org \
    --cc=davem@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@oss.sgi.com \
    --cc=rmk+lkml@arm.linux.org.uk \
    --cc=yoshfuji@linux-ipv6.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).