News for hackers. Stuff that matters.
Re:Stop using strncpy already (2013) by jandrese on Friday August 31, 2018 @10:12AM (#17885726)
Sadly, it still has this braindamage:

  Like snprintf(3), the strlcpy() and strlcat() functions return the total
  length of the string they tried to create.  For strlcpy() that means the
  length of src.  For strlcat() that means the initial length of dst plus
  the length of src.
So if you've mmaped in something like the OpenStreetMaps xml dump file and try to strlcpy out the first 100 bytes (because you didn't want to have to null terminate it yourself after a memcpy), you'll notice that your program is running rather slowly.
Re:Stop using strncpy already (2013) by kevindqc on Friday August 31, 2018 @10:50AM (#17886020)
"Let's read all the data you don't want because!" nice
Re:Stop using strncpy already (2013) by needlepont on Friday August 31, 2018 @10:56AM (#17886077)
That is trusting user input. You don't do that, right?
Re:Stop using strncpy already (2013) by quietbritishjim on Friday August 31, 2018 @11:02AM (#17886125)
It's not about trusting user input. It's asking for a function to create a 100 byte substring of a very long string and expecting it to take time~100 not time~len(src).

In fairness, that is probably just not the target "market" for strlcpy. Presumably, it is meant for "please copy this whole string which I expect to fit in the target (but catch me in the rare case that it does not)".

Re:Stop using strncpy already (2013) by jandrese on Friday August 31, 2018 @11:24AM (#17886299)
It does trust the user input. If you are being attacked and the input has lost its null terminator then your strlcpy might core dump even if it doesn't leak data.

They made it this way to make it more of a drop-in replacement for strncpy, but IMHO they should have changed the return value to be the number of characters copied. If your return value is less than n, then your string was copied completely, if it is equal to n then your string was truncated.

There are good reasons to use strlcpy over memcpy. For example you have a text parser where the most common case is each string is only a few bytes long, but you need to be able to handle odd cases where they are much longer. So you have a large buffer that you only use a tiny chunk of most times. With strlcpy it will be quick, but memcpy will be chugging the whole buffer each time.

Re:Stop using strncpy already (2013) by needlepont on Friday August 31, 2018 @11:21AM (#17886271)
As you note: your example is specious.
Re:Stop using strncpy already (2013) by quietbritishjim on Friday August 31, 2018 @11:38AM (#17886422)
What example? Are you confusing me with GP commenter perhaps?
Re:Stop using strncpy already (2013) by tedunangst on Friday August 31, 2018 @11:11AM (#17886195)
Don't treat mmapped files as strings.