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.
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()