News for hackers. Stuff that matters.
Re:C and returning values quickly or safely, but not both by outsomnia on Friday January 19, 2024 @01:42AM (#39052281)
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.

Re:C and returning values quickly or safely, but not both by metadat on Friday January 19, 2024 @04:54AM (#39053599)
Is there an alternate function which should be used instead of strncopy()?
Re:C and returning values quickly or safely, but not both by outsomnia on Friday January 19, 2024 @06:12AM (#39054127)
There 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

Re:C and returning values quickly or safely, but not both by metadat on Friday January 19, 2024 @11:58AM (#39057715)
That's right, I knew this at some point but haven't been doing much C lately. Thank you for the refresh!