tile gxio: use better string copy primitive
authorChris Metcalf <cmetcalf@tilera.com>
Tue, 2 Sep 2014 20:25:22 +0000 (16:25 -0400)
committerChris Metcalf <cmetcalf@tilera.com>
Thu, 2 Oct 2014 14:19:34 +0000 (10:19 -0400)
commitbceb7efa6a7e656bfaa67b6f54925e7db75bcd52
treeb43b312e9f61c5e9b5fc0ad91fc522fd372f10b0
parent514b82a52b7aaa413ab8f0e7db93a5ccd0540d09
tile gxio: use better string copy primitive

Both strncpy and strlcpy suffer from the fact that they do
partial copies of strings into the destination when the target
buffer is too small.  This is frequently pointless since an
overflow of the target buffer may make the result invalid.

strncpy() makes it relatively hard to even detect the error
condition, and with strlcpy() you have to duplicate the buffer
size parameter to test to see if the result exceeds it.
By returning zero in the failure case, we both make testing
for it easy, and by simply not copying anything in that case,
we make it mandatory for callers to test the error code.

To catch lazy programmers who don't check, we also place a NUL at
the start of the destination buffer (if there is space) to
ensure that the result is an invalid string.

At some point it may make sense to promote strscpy() to
a global platform-independent function, but other than the
reviewers, no one was interested on LKML, so for now leave
the strscpy() function as file-static.

Reviewed-by: Randy Dunlap <rdunlap@infradead.org>
Reviewed-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
arch/tile/gxio/mpipe.c