From: Peter Eriksen Date: Sun, 11 Jun 2006 13:05:59 GMT Subject: Re: [PATCH] Implement safe_strncpy() as strlcpy() and use it more. [Take 2] Message-ID: <20060611130559.GD10430@bohr.gbar.dtu.dk> In-Reply-To: <20060611123332.GA3832@robert.daprodeges.fqdn.th-h.de> On Sun, Jun 11, 2006 at 12:33:32PM +0000, Rocco Rutte wrote: > Hi, > > * Peter Eriksen [06-06-11 14:03:28 +0200] wrote: > > >-char *safe_strncpy(char *dest, const char *src, size_t n) > >+size_t safe_strncpy(char *dest, const char *src, size_t size) > >{ > >- strncpy(dest, src, n); > >- dest[n - 1] = '\0'; > >+ size_t ret = strlen(src); > > At least FreeBSD's strlen() requires a non-NULL argument, i.e. with > src==NULL, this will segfault. > > If you can ensure that src!=NULL, then it's okay, but the safe_ prefix > implies something different. By eyeballing the source code of strlcpy() from FreeBSD and OpenBSD (which are quite similar), it seems they will segfault if given source string, which is NULL. So, from what I've understood, safe_strncpy() is not more unsafe than strlcpy() or the current safe_strncpy(). It does have different semantics, because the current one pads will NULL, since it uses strncpy(). Peter