On 07/15/2012 03:57 PM, Jim Meyering wrote:
Remember the strncpy prohibition I added to "make syntax-check"
not too long ago? I exempted the uses in pinky.c and who.c
because those programs don't really matter and their uses were
not obviously bad. Plus, I just didn't feel like it.
Well, today I did, so removed them and, along the way, was surprised
to see that while not officially wrong, they were unwarranted: they
would uselessly zero-pad out to the end of each destination buffer.
From reading the man pages of strncpy and stpncpy, stpncpy does the
same. From the stpncpy man page on debian/unstable (date 2011-09-28,
from Linux man-pages 3.40):
DESCRIPTION
The stpncpy() function copies at most n characters from the string
pointed to by src, including the terminating null byte ('\0'), to the
array pointed to by dest. Exactly n characters are written at dest.
If the length strlen(src) is smaller than n, the remaining characters
in the array pointed to by dest are filled with null bytes ('\0'), If
the length strlen(src) is greater or equal to n, the string pointed to
by dest will not be null-terminated.
As written above the copied string may be unterminated after stpncpy
as well as after strncpy. What exactly is won by using stpncpy over
strncpy?