Stop using strncpy already (2013)
randomascii.wordpress.com
Stop using strncpy already (2013)
1–10 of 83 posts
Re: Stop using strncpy already (2013)
#2Re: Stop using strncpy already (2013)
#3Re: Stop using strncpy already (2013)
#4Two years after this post, the Linux kernel added strscpy, the api of which is equivalent to the safe strncpy in this post. Internally, it stops copying once it reaches the null terminator. https://lwn.net/Articles/659214/
Re: Stop using strncpy already (2013)
#5Let's not forget that it's also not natively available in glibc. Though you'll find it in every BSD you can think of.
Re: Stop using strncpy already (2013)
#6Re: Stop using strncpy already (2013)
#7If the standard libs are that bad, the community should really lobby for the inclusion of a "safe" string copy into the C standard. Failing that, use a battle-tested third-party library like bstrlib[0].
Re: Stop using strncpy already (2013)
#8Re: Stop using strncpy already (2013)
#9Re: Stop using strncpy already (2013)
#10> strlcpy is designed to solve the null-termination problems – it always null-terminates. It’s certainly an improvement over strncpy, however it isn’t natively available in VC++. Let's not forget that it's also not natively available in glibc. Though you'll find it in every BSD you can think of.
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.