Subject: Re: 'Safe' string copy: strlcpy() or strncpy() ?
To: =?iso-8859-1?Q?Jarom=EDr_Dolecek?= <dolecek@ics.muni.cz>
From: Ignatios Souvatzis <is@beverly.kleinbus.org>
List: tech-userlevel
Date: 01/27/2001 12:37:48
On Fri, Jan 26, 2001 at 09:18:22PM +0100, Jaromír Dolecek wrote:
> Separate, though related question: should old code be converted to
> use strlcpy() instead of strncpy()  ?

Not necessarily.

The intended application for strncpy is to copy a zero-terminated string
into a fixed size character array, and be able to use the full array
consistently, or at least to not leak former information therein.

This is different from copying a zero terminated string to a new location,
not overflowing it, and still keeping a zero terminated string. (Although
with the additional xxx[n] = 0 it can simulate it, at a performance penalty.)

So unless you know exactly what you're doing, you'll risk breaking uses, or
at least leak information that was formerly erased.

In summary, you have to be careful.

Regards,
	-is