Re:Strlcpy and strlcat added to glibc 2.38
by saagarjha on Tuesday July 18, 2023 @01:24AM
(#36768146)
People typically do not realize that it has a return value that is expensive to compute.
[ Parent
| on HN ]
It's not any slower in the typical case where destination buffer is large enough to fit the source thing. And if that's not the case then we are most likely in a error case (either caller notices the truncation and decides to abort, or ignores the truncation and things may soon go boom), and not many people care about optimizing error paths.
Furthermore, when coders dont't have strlcpy() the alternatives are often even worse than strlcpy(): 1) They use strcpy() and have buffer overflows. 2) They use strncpy() which is slower than strlcpy() in the common (non truncating) case, and in the truncating case leave the string unterminated (thus segfault potential) 3) They use snprintf(dst, len, "%s", src); which is strictly slower than strlcpy()
Re:Strlcpy and strlcat added to glibc 2.38
by saagarjha on Tuesday July 18, 2023 @09:26AM
(#36771793)
Since the error path is the largest one (the string doesn’t fit…) it makes sense to bound its execution. I would not recommend the others FWIW for exactly the reasons you mentioned.
Re:Strlcpy and strlcat added to glibc 2.38
by tedunangst on Tuesday July 18, 2023 @10:34AM
(#36773016)
Why would you optimize for the error case and not the common case? You've already done an unbounded amount of work copying the string in from the network or wherever. If anybody cared that much, they wouldn't let the string get that long in the first place.
Re:Strlcpy and strlcat added to glibc 2.38
by saagarjha on Sunday July 23, 2023 @12:15AM
(#36832518)
It can be appropriate to bound the runtime of certain components of a system while allowing looser constraints elsewhere. For example I would perhaps not want to do an O(n) string operation on a collection of strings even though the user would be pretty upset if they can’t paste infinite input into my app.
Re:Strlcpy and strlcat added to glibc 2.38
by jstimpfle on Tuesday July 18, 2023 @08:47AM
(#36771324)
It's only as expensive as what you pass in. Joke's on you.
Re:Strlcpy and strlcat added to glibc 2.38
by saagarjha on Tuesday July 18, 2023 @09:24AM
(#36771773)
qsort is also only as expensive as what I pass in. If it did a bubble sort internally I would be pretty upset though.