With `strncpy`, the caller assumes the responsibility of buffer size (by specify that size explicitly every single time. I thought that responsibility is not being debated within the current context.
TL;DR - I've reviewed enough code to know that software developers pass the wrong size to strncpy and friends.
I am not saying that is bad. But solutions already exist. One is using the string class, another being transiting to a dynamic language (having size as part of data structure and checking it at every usage is one step into dynamic types).
Alternatively, the compiler can take over this responsibility by statically check every call (the rust solution). That is a different language. On that, I have a question: how can we pass the syntactic buffer size into a static library without embedding the size into the data? If we do embedding the size, then it is still dynamic within the context of the library.
Either way, one should not still be discussing the safety of strncpy here.
Now as long as we are discussing strncpy, I assume it is given that we do not want to have buffer size as permanent part of the simple string type and we are not looking for any solutions that demand an overhaul of the language/libraries. I assume it is given that caller takes the responsibility of buffer size -- exchange with a bit responsibility for simpler system and greater control.
You won't have a good solution (or good discussions) without confining our context first.
* We're going to stick with C and its standard library: lots of room to continue to make errors.
* We're going to stick with C, but it's OK to consider an alternate implementation of strings: the C standard, the standard library, and lots of other C libraries are full of functions that deal in char* strings, so you're stuck with them. Also, runtime overhead increases.
* We're willing to consider other languages: other languages have compiler overhead, semantic overhead, runtime overhead, or all three. Also, you still have to interoperate with C.
So the main issue is how to make C devs actually adopt some kind of safer C dialect and migrate towards it.
I can't quite tell why, but I found it kind of insightful and it made me feel a little better about my own inclination to work on lower levels.
And I also find funny the remark. :)
I bring it up because the article makes programming errors on the part of the caller part of the case for its "safe" solution.
Really, stop using C. To quote Hayao Miyazaki, C was a mistake.
#define BUFSIZE 100
char buf[BUFSIZE+1]
buf[BUFSIZE]=0 // for global var, it is already zeroed, right?
Then one can call strncpy(buf, BUFSIZE, ...) without worrying about null-termination.