git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Implement safe_strncpy() as strlcpy() and use it more. [Take 2]

From
PEPeter Eriksen <s022018@student.dtu.dk>
Date
Jun 11, 2006, 13:05 UTC
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:
Show 16 quoted lines
> 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
Previous: Rocco Rutte
Message 3 of 3 in “Implement safe_strncpy() as strlcpy() and use it more. [Take 2]”
  1. Implement safe_strncpy() as strlcpy() and use it more. [Take 2]Peter Eriksen, Jun 11, 2006
  2. Rocco RutteJun 11, 2006
  3. Peter EriksenJun 11, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.