Re: Usage of strncpy in the kernel

Martin Husemann <[email protected]>
Newsgroups gmane.os.netbsd.devel.kernel
Message-ID <[email protected]>
On Fri, Jan 03, 2025 at 03:45:07PM +0100, Roland Illig wrote:
> No, strlcpy is fine for string handling, as it expects (null-terminated)
> strings and also produces them.
> 
> It's only strncpy (and probably strncat) that are problematic, as these
> two are hard to use correctly for handling strings.

The question is wether the source strings are 0 terminated or not.

If they are, strlcpy() is good. If they are not guaranteed to be (like
sometimes when copying from fixed size descriptors) you need to use
strncpy() and manually terminate the result.

You can not swap them randomly, they serve different purposes.
See the last WARNING: section in strlcpy(3).

Martin
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.