[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
|
|
Subscribe / Log in / New account

Ushering out strlcpy()

Ushering out strlcpy()

Posted Aug 25, 2022 16:23 UTC (Thu) by IanKelling (subscriber, #89418)
Parent article: Ushering out strlcpy()

> strncpy() is still holding string despite its known hazards

Funny.


to post comments

Ushering out strlcpy()

Posted Aug 25, 2022 18:08 UTC (Thu) by adobriyan (subscriber, #30858) [Link] (3 responses)

"n" in "strncpy" stands for "never".

Ushering out strlcpy()

Posted Aug 25, 2022 19:47 UTC (Thu) by Sesse (subscriber, #53779) [Link] (2 responses)

I think it's just catastrophically named. It should be something like strtofixed() or strpad() or something; what it does is fundamentally to copy a null-terminated string into a fixed-size, non-null-terminated, buffer. Like if you have a struct of fixed-width string fields, ISAM-style. So the destination isn't a C-style string, and shouldn't be treated as such.

Of course, this makes the function much less useful than the average C programmer assumes.

strncpy

Posted Aug 25, 2022 22:03 UTC (Thu) by tialaramex (subscriber, #21167) [Link] (1 responses)

As I understand it, this function exists because it's how you do exactly this operation (copy a C-style string into some fixed width buffer) somewhere in early Unix, maybe the filesystem. Its authors knew exactly what they were doing, and this function makes sense for their intended application, but as you say the name suggests somebody might find it useful in very different circumstances.

But, on the other hand notice the newer APIs are still awful. The language just doesn't give us much to work with by having this terrible string type. I think we might have been better off if C hadn't admitted to a string type at all (ie there are no string functions, and no "" literal syntax) and then presumably C89 might have introduced one but there would have been a war to decide how that works (since C89 mostly codifies popular real world C implementations) and I can't believe that 0-terminated wins such a war, even if Pascal-style (length-prefixed) wins that's a less awful world than our world.

strncpy

Posted Aug 26, 2022 0:46 UTC (Fri) by NYKevin (subscriber, #129325) [Link]

Raymond Chen reports that this was the case at least as far back as System V (and yes, it was in the filesystem): https://devblogs.microsoft.com/oldnewthing/20050107-00/?p...


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