Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 15 Aug 2002 12:26:42 -0700
From:      Brooks Davis <brooks@one-eyed-alien.net>
To:        arch@freebsd.org
Subject:   kernel strlcpy
Message-ID:  <20020815122641.B21334@Odin.AC.HMC.Edu>

next in thread | raw e-mail | index | archive | help

--3lcZGd9BuhuYXNfi
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

I'd like to import strlcpy into libkern.  It's obviously the right
function for setting the if_xname member of struct ifnet from the
device.  Most network drivers would use something of the form:

strlcpy(ifp->if_xname, device_get_nameunit(dev), IFNAMSIZ);

in their attach function.  It's possiable to do this with strncpy, but
it's rather lame to have to write:

strncpy(ifp->if_xname, device_get_nameunit(dev), IFNAMSIZ-1);
ifp->if_xname[IFNAMSIZ-1] =3D '\0';

Due to effiencies created by the if_xname change, GENERIC is actually
483 bytes _smaller_ with my current patch set including strlcpy.  The
ability to create very stripped kernels will be slightly reduced by this
addition, but the stripped object file is only 532 bytes on the i386.

I believe the function will be used enough to justify the bloat as I
have 29 consumers in sys/dev alone and that's less then half of the
interfaces.  At a glance, there also appear to be other places where the
use of strlcpy would be appropriate.  For instance, kern/kern_proc.c
contains a number of cases like:

strncpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm) - 1);

where using strlcpy would eliminate the unsightly -1 and assure the
reader that the string would always be null terminated without having to
think about it too much.

I am not advocating introducing strlcat because strncat is bairly used
(only 9 times in the whole kernel.)

Comments?

-- Brooks

--=20
Any statement of the form "X is the one, true Y" is FALSE.
PGP fingerprint 655D 519C 26A7 82E7 2529  9BF0 5D8E 8BE9 F238 1AD4

--3lcZGd9BuhuYXNfi
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9XABwXY6L6fI4GtQRAuXfAJ4h2AAV2mtR1TQ3MSnftvIs7tX2KACeM70S
fQD+Hx8/Z82bGnOIjUZ5U3Q=
=IyZM
-----END PGP SIGNATURE-----

--3lcZGd9BuhuYXNfi--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020815122641.B21334>