]> git.ipfire.org Git - thirdparty/man-pages.git/commit
strcpy.3: Fix example code for strncpy, which could pass an incorrect length
authorMatthew Kilgore <mattkilgore12@gmail.com>
Thu, 7 Jun 2018 04:10:51 +0000 (00:10 -0400)
committerMichael Kerrisk <mtk.manpages@gmail.com>
Sun, 10 Jun 2018 11:36:46 +0000 (13:36 +0200)
commit85bbb2a25391db590bd2d40f2526325e6ca6863a
treeed88925dc2d06464fab7aedccb0da0b1a14a9063
parent4c974cbac65b07ad4badda787e5b70588e1b67b1
strcpy.3: Fix example code for strncpy, which could pass an incorrect length

The example code currently passes `buflen - 1` to `strncpy`,
however the length parameter to `strncpy` is `size_t`, which is
unsigned.  This means that when `buflen` is zero, the cast of `-1`
to unsigned will result in passing `UINT_MAX` as the length.
Obviously, that would be incorrect and could cause `strncpy` to
write well beyond the buffer passed.

The easy solution is to wrap the whole code in the `buflen > 0`
check, rather then just the part of the code that applies the null
terminator.

Signed-off-by: Matthew Kilgore <mattkilgore12@gmail.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
man3/strcpy.3