strncpy() is an odd function that is actually incorrectly named - its original purpose is to make sure that the buffer is fully initialized with the contents of the string (without overflowing the target) and a buffer reminder with zeros. As far as I understand, the original goal was to process the file system directory entries - the target buffer was not really a string in the same sense as the other strxxx() functions in the C library. The main problem with strncpy() is that if the source string is larger than the destination buffer, the result will not be null.
Most of the other "n" functions that deal with strings do end the string, but there are exceptions, such as Microsoft bastardized _snprintf() . The correct C99 snprintf() will always have zero termination of the target string (as long as the destination buffer is larger than 0).
There is a "Technical Report", TR 24731, which offers a set of alternative border checking options for functions related to lines and memory buffers. One of the goals of TR is to make the parameters, results, and error behavior of functions more similar in function. TR seems to have a bit of mixed recognition, and I don't think it is widely implemented except for the Microsoft compiler (I think MS was the main TR driver). You can get more information here:
Even if you are not a fan of these suggestions, I think that they make problems with existing functions for educational reading.
source share