|
|
Log in / Subscribe / Register

Ushering out strlcpy()

Ushering out strlcpy()

Posted Aug 25, 2022 22:12 UTC (Thu) by kees (subscriber, #27264)
Parent article: Ushering out strlcpy()

Replacing strlcpy is pretty straightforward. Here the issue tracker item for it: https://github.com/KSPP/linux/issues/89

Replacing strncpy has some special corner cases, but I really hope we can get it purged too. I wrote up the per-caller conversion flow chart in the issue for it:
https://github.com/KSPP/linux/issues/90


to post comments

Ushering out strlcpy()

Posted Aug 26, 2022 6:53 UTC (Fri) by wtarreau (subscriber, #51152) [Link] (3 responses)

Agreed, I too would like to see strncpy() disappear, as it tends to lie to the reader. I've seen it be entirely responsible for major slowdowns a few times in different projects by the way, where users did not realize that it was padding the whole target buffer. And the number of times you see it called in series to concatenate path elements, you can't avoid thinking "OK I have an overflow after the first one". The only valid case I know for it in userland is to write a UNIX socket path because sun_path is of fixed length and not necessarily 0-terminated :-/ But that clearly demonstrates that str_anything_pad() would be a more suitable name for this thing (though memset(end, 0, size-len)
based on strscpy()'s return would work too).

Ushering out strlcpy()

Posted Aug 30, 2022 7:10 UTC (Tue) by milesrout (subscriber, #126894) [Link] (2 responses)

>Agreed, I too would like to see strncpy() disappear, as it tends to lie to the reader. I've seen it be entirely responsible for major slowdowns a few times in different projects by the way, where users did not realize that it was padding the whole target buffer.

These people should not be programming in C.

"If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written."

Right there in the man page. If you can't read the most basic documentation, don't use the language!

Ushering out strlcpy()

Posted Aug 30, 2022 14:58 UTC (Tue) by NYKevin (subscriber, #129325) [Link] (1 responses)

IMHO it is not totally unreasonable to expect strncpy(src) to be equivalent to snprintf("%s", size, src), because strcpy(src) is actually equivalent to sprintf("%s", src).

(Incidentally, snprintf is just about the only portable function that DTRT in this context.)

Ushering out strlcpy()

Posted Aug 30, 2022 14:59 UTC (Tue) by NYKevin (subscriber, #129325) [Link]

(Oops, I put the arguments in the wrong order, but you know what I mean.)

Ushering out strlcpy()

Posted Aug 27, 2022 6:42 UTC (Sat) by ruscur (guest, #104891) [Link] (1 responses)

Replacing stray strlcpy() cases is some great low-hanging fruit for anyone looking to get their first commits into the kernel.

Ushering out strlcpy()

Posted Aug 28, 2022 11:39 UTC (Sun) by willy (subscriber, #9762) [Link]

Did you follow kees's link? It's literally tagged "Good first issue"


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds