Subject: Re: 'Safe' string copy: strlcpy() or strncpy() ?
To: Matthias Buelow <mkb@mukappabeta.de>
From: Todd Vierling <tv@wasabisystems.com>
List: tech-userlevel
Date: 01/26/2001 16:54:38
On Fri, 26 Jan 2001, Matthias Buelow wrote:
: >Separate, though related question: should old code be converted to
: >use strlcpy() instead of strncpy() ?
:
: strncpy() is portable and standardized (ANSI/ISO C), while strlcpy()
: is not (it only seems to exist in newer OpenBSD, FreeBSD and NetBSD
: versions, newer being post-OpenBSD 2.4, where it made its appearance,
: obviously). So you chose -- more safety for inexperienced programmers
: versus portability. I usually go for the latter.
FWIW, strlcpy() has a less well known speed advantage. From strncpy(3):
The strncpy() copies not more than len characters into dst, appending
`\0' characters if src is less than len characters long,... ^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
strlcpy() doesn't have this speed-impeding "feature" (dictated by ANSI in
the definition of strncpy()).
--
-- Todd Vierling <tv@wasabisystems.com> * Wasabi NetBSD: Run with it.
-- NetBSD 1.5 now available on CD-ROM -- http://www.wasabisystems.com/