On Thu, 31 Jul 2008 15:00:57 -0400
Peter Memishian <[EMAIL PROTECTED]> wrote:
[...]
> Fair enough. That said, a lot of the strncpy() users I've seen also feel
> the need to NUL-terminate the interface name as well -- e.g. (from in.mpathd):
>
> (void) strncpy(lifr.lifr_name, pi->pi_name, sizeof (lifr.lifr_name));
> lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
I'm guessing people do it because they are just copying code snippits
or they know that strncpy() isn't guaranteed to null NUL-terminate its
first argument the source string is the same size or longer then the
target buffer.
>
> So regardless of strlcpy(), the interface name has effectively already
> been limited to LIFNAMSIZ-1 characters. So it seems preferable to just
> use strlcpy() all the time -- not to mention that it makes things simpler
> if the application wants to operate on lifr_name without fear that it
> might not be NUL-terminated.
Yes. To use strncpy() well you either have to do as above and risk
truncation or know the length of the source string (and still do
something about too long source strings) in which case you might as
well have use memcpy() or just wrap it all up in strlcpy().
strncpy() always felt like somebody just added a buffer length without
really thinking about the typical use. It would be interesting to know
what the history of the API was.
mph
>
> --
> meem
> _______________________________________________
> nwam-discuss mailing list
> [EMAIL PROTECTED]
> http://mail.opensolaris.org/mailman/listinfo/nwam-discuss
_______________________________________________
networking-discuss mailing list
[email protected]