3 ms·
I mostly agree with what you wrote, but then take a look at the man pages for the two functions you mentioned: https://linux.die.net/man/3/strlcpy https://linu
by TeMPOraL 6y ago
I mostly agree with what you wrote, but then take a look at the man pages for the two functions you mentioned:
https://linux.die.net/man/3/strlcpy https://linux.die.net/man/3/strlcpy
https://linux.die.net/man/3/strncpy https://linux.die.net/man/3/strncpy
Note how most of the text there is focused on the "How" and "What", because both functions have a bunch of requirements for their arguments that are not expressed in their respective signatures.
Some languages have better tools for expressing these requirements in code. But when they can't be expressed in a way that can be enforced by the compiler, IMO they absolutely need to be mentioned in an interface-level comment (i.e. above function signature), to give users a fighting chance of avoiding bugs.
Also worth noting that the particular constraints around strncpy() and strlcpy() will not be obvious in the implementation either - the programmer trying to make use of these functions would have to study the implementation to notice potential issues. A well-placed comment can save them an expensive context switch here.