Ushering out strlcpy()
Ushering out strlcpy()
Posted Sep 2, 2022 13:38 UTC (Fri) by mathstuf (subscriber, #69389)In reply to: Ushering out strlcpy() by mtodorov
Parent article: Ushering out strlcpy()
> strlen (buf) should thereof always be done as strnlen (buf, sizeof (buf)). Beyond the end of the buffer there can be beasts, nasty animals and hidden pits.
The problem is that `sizeof (buf)` is not known in a lot of contexts (e.g., filenames passed in from userspace). At that point, you're starting to carry around the allocated size around and you may as well amortize the length calculation as well…and oh, what do you know, you have a C++ std::string with no relevant established API.
