3 ms·
Yep, that's intended use case for strncpy(). It's not really suitable for general purpose programming like the OP is doing. It won't null terminate the string
by masternight 1y ago
Yep, that's intended use case for strncpy().
It's not really suitable for general purpose programming like the OP is doing. It won't null terminate the string if the buffer is filled, which will cause you all sorts of problems. If the buffer is not filled, it will write extra null bytes to fill the buffer (not a problem, but unnecessary).
On freebsd you have strlcpy(), Windows has strcpy_s() which will do what the OP needs. I remember someone trying to import strlcpy() into Linux, but Ulrich Drepper had a fit and said no.
You just never assume a string is null terminated when reading, using strnlen or strncpy when reading as well.
Not really possible when dealing with operating system level APIs that expect and require null-terminated strings. It's safer and less error-prone to keep everything null terminated at all times.
Or just write in C++ and use std::string, or literally any other language. C is terrible when it comes to text strings.
- fsckboy 1y ago>It won't null terminate the string if the buffer is filled, which will cause you all sorts of problems. if you don't know how to solve/avoid a problem like that, you will have all sorts of other problems pound-define strncopy to a compile fail, write the function you want instead, correct all the compile errors, and then, not only move on with your life, never speak of it again, for that is the waste of time. C++ std:string is trash, java strings are trash, duplicate what you want from those in your C string library and sail ahead. no language has better defined behaviors than C, that's why so many other languages, interpreters, etc. have been implemented in C.
- donnachangstein 1y ago> On freebsd you have strlcpy() strlcpy() came from OpenBSD and was later ported to FreeBSD, Solaris, etc.
- masternight 1y agoYup. Lots of good security & safety innovations came from OpenBSD.
- sirwhinesalot 1y agoYou shouldn't use any of those garbage functions. Just ignore \0 entirely, manage your lengths, and use memcpy.
- masternight 1y agoYeah, Drepper said the same thing.
- hkpack 1y agoI am not writing in C, but always wondered, why pascal-like strings wrappers are not popular, i. e. when you have first 2 bytes represent the length of the string following by \0 terminated string for compatibility.
- sirwhinesalot 1y ago2 bytes is not enough, usually you'll see whole "size_t" worth of bytes for the length. But you could do something utf-8 inspired I suppose where some bit pattern in the first byte of the length tells you how many bytes are actually used for the length.
- renewedrebecca 1y agoPascal originally required you to specify the length of the string before you did anything with it. This is a totally good idea, but was considered to be too much of a pain to use at the time.
- hnfong 1y agoIn C you have to do that too, like... malloc()?
- int_19h 1y agoYou still need a 0-terminated string to pass to API of most libraries (including ones included with the OS - in this case, Win32).