Re: Usage of strncpy in the kernel

"Greg A. Woods" <[email protected]>
Newsgroups gmane.os.netbsd.devel.kernel
Organization Planix, Inc.
Message-ID <[email protected]>
At Mon, 6 Jan 2025 15:23:45 -0500 (EST), Mouse <[email protected]> wrote:
Subject: Re: Usage of strncpy in the kernel
>
> > All strlcpy() does extra is guarantee NUL-termination,
>
> Extra?  Over strcpy and strncpy?

Yes, it's what strlcpy() does over and above what strnpy() does.

> No, that's not all.  It also fills the rest, if any, of the destination
> space with more NULs.  (This can be useful to, for example, prevent
> leaking kernel stack trash to userland.)

That's what strncpy() does over and above what strlcpy() does.

Each has an advantage (depending on the specific circumstances it is
used in) over the other.

Personally though I'll never use strlcpy(), either in-kernel or not.  I
prefer to be explicit about doing the NUL-termination when necessary
after a call to strncpy() as that explicitly demonstrates what code is
intending to do, and it accomplishes this using a well known standard
function that's not system specific.  Your idea about a wrapper to do
the termination is interesting, but it's not one defined by any existing
standard, and as such in some sense it actually hides the intention too
much.

--
					Greg A. Woods <[email protected]>

Kelowna, BC     +1 250 762-7675           RoboHack <[email protected]>
Planix, Inc. <[email protected]>     Avoncote Farms <[email protected]>
signature.asc (application/pgp-signature, 195 B)
-----BEGIN PGP SIGNATURE-----

iF0EABECAB0WIQRuK6dmwVAucmRxuh9mfXG3eL/0fwUCZ3xFHgAKCRBmfXG3eL/0
fy/lAJ4o/7lri2OYvqWyDopyt9PDcnZL4gCbBAn55L8xaRThptllel3Cv13m8/c=
=fVFb
-----END PGP SIGNATURE-----
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.