All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: Phil Sutter <phil@nwl.cc>
Cc: David Laight <David.Laight@ACULAB.COM>,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>
Subject: Re: [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat()
Date: Mon, 4 Sep 2017 11:25:25 -0700	[thread overview]
Message-ID: <20170904112525.04bc66d5@xeon-e3> (raw)
In-Reply-To: <20170904150015.GB30364@orbyte.nwl.cc>

On Mon, 4 Sep 2017 17:00:15 +0200
Phil Sutter <phil@nwl.cc> wrote:

> On Mon, Sep 04, 2017 at 02:49:20PM +0000, David Laight wrote:
> > From: Phil Sutter  
> > > Sent: 01 September 2017 17:53
> > > By making use of strncpy(), both implementations are really simple so
> > > there is no need to add libbsd as additional dependency.
> > >   
> > ...  
> > > +
> > > +size_t strlcpy(char *dst, const char *src, size_t size)
> > > +{
> > > +	if (size) {
> > > +		strncpy(dst, src, size - 1);
> > > +		dst[size - 1] = '\0';
> > > +	}
> > > +	return strlen(src);
> > > +}  
> > 
> > Except that isn't really strlcpy().
> > Better would be:
> > 	len = strlen(src) + 1;
> > 	if (len <= size)
> > 		memcpy(dst, src, len);
> > 	else if (size) {
> > 		dst[size - 1] = 0;
> > 		memcpy(dst, src, size - 1);
> > 	}
> > 	return len - 1;  
> 
> Please elaborate: Why isn't my version "really" strlcpy()? Why is your
> proposed version better?
> 
> Thanks, Phil

Linux kernel:
size_t strlcpy(char *dest, const char *src, size_t size)
{
	size_t ret = strlen(src);

	if (size) {
		size_t len = (ret >= size) ? size - 1 : ret;
		memcpy(dest, src, len);
		dest[len] = '\0';
	}
	return ret;
}

FreeBSD:
size_t
strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize)
{
	const char *osrc = src;
	size_t nleft = dsize;

	/* Copy as many bytes as will fit. */
	if (nleft != 0) {
		while (--nleft != 0) {
			if ((*dst++ = *src++) == '\0')
				break;
		}
	}

	/* Not enough room in dst, add NUL and traverse rest of src. */
	if (nleft == 0) {
		if (dsize != 0)
			*dst = '\0';		/* NUL-terminate dst */
		while (*src++)
			;
	}

	return(src - osrc - 1);	/* count does not include NUL */
}


They all give the same results for some basic tests.
Test			FreeBSD		Linux		Iproute2
"",0:           	0 "JUNK"      	0 "JUNK"      	0 "JUNK"      
"",1:           	0 ""          	0 ""          	0 ""          
"",8:           	0 ""          	0 ""          	0 ""          
"foo",0:        	3 "JUNK"      	3 "JUNK"      	3 "JUNK"      
"foo",3:        	3 "fo"        	3 "fo"        	3 "fo"        
"foo",4:        	3 "foo"       	3 "foo"       	3 "foo"       
"foo",8:        	3 "foo"       	3 "foo"       	3 "foo"       
"longstring",0: 	10 "JUNK"     	10 "JUNK"     	10 "JUNK"     
"longstring",8: 	10 "longstr"  	10 "longstr"  	10 "longstr"  

  reply	other threads:[~2017-09-04 18:25 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-01 16:52 [iproute PATCH 0/6] strlcpy() and strlcat() for iproute2 Phil Sutter
2017-09-01 16:52 ` [iproute PATCH 1/6] utils: Implement strlcpy() and strlcat() Phil Sutter
2017-09-04 14:49   ` David Laight
2017-09-04 15:00     ` Phil Sutter
2017-09-04 18:25       ` Stephen Hemminger [this message]
2017-09-06 13:59         ` David Laight
2017-09-06 15:25           ` Stephen Hemminger
2017-09-06 16:51           ` [iproute PATCH] utils: Review " Phil Sutter
2017-09-01 16:52 ` [iproute PATCH 2/6] Convert the obvious cases to strlcpy() Phil Sutter
2017-09-01 19:13   ` Daniel Borkmann
2017-09-01 16:52 ` [iproute PATCH 3/6] Convert harmful calls to strncpy() " Phil Sutter
2017-09-01 16:52 ` [iproute PATCH 4/6] ipxfrm: Replace STRBUF_CAT macro with strlcat() Phil Sutter
2017-09-01 16:52 ` [iproute PATCH 5/6] tc_util: No need to terminate an snprintf'ed buffer Phil Sutter
2017-09-01 16:52 ` [iproute PATCH 6/6] lnstat_util: Make sure buffer is NUL-terminated Phil Sutter
2017-09-01 19:12 ` [iproute PATCH 0/6] strlcpy() and strlcat() for iproute2 Stephen Hemminger

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=20170904112525.04bc66d5@xeon-e3 \
    --to=stephen@networkplumber.org \
    --cc=David.Laight@ACULAB.COM \
    --cc=netdev@vger.kernel.org \
    --cc=phil@nwl.cc \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.