3 ms·
Guy seems a bit new to C. pointers-to-structs ARE pointers-to-objects. That's the default way to operate on objects in a huge amount of code. Don't use strnc
by outsomnia 3y ago
Guy seems a bit new to C.
pointers-to-structs ARE pointers-to-objects. That's the default way to operate on objects in a huge amount of code.
Don't use strncpy(), if it hits the limit it doesn't end the copy with NUL, causing random crashes.
Quite a few people have concluded that generally, despite the standard library using them, NUL-terminated C string were not the Right Way and it's better to have a struct with the pointer and a size_t length (and perhaps a size_t representing the possibly larger allocated length). This eliminates NUL handling, and strlen() as well and since everything can cheaply know the length, can be the basis of more secure code.
- metadat 3y agoIs there an alternate function which should be used instead of strncopy()?
- outsomnia 3y agoThere are various 'safer' versions of it, a common one is strlcpy() from bsd (which is in glibc etc) https://linux.die.net/man/3/strlcpy https://linux.die.net/man/3/strlcpy
- metadat 3y agoThat's right, I knew this at some point but haven't been doing much C lately. Thank you for the refresh!