Merge pull request #1886 from donaldsharp/strlcpy

lib: Fixup strlcat and strlcpy to be a bit more descriptive
This commit is contained in:
Rafael Zalamena 2018-03-14 11:59:31 -03:00 committed by GitHub
commit 2b9a295256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 21 deletions

View File

@ -28,23 +28,25 @@
#ifndef HAVE_STRLCAT #ifndef HAVE_STRLCAT
#undef strlcat #undef strlcat
size_t strlcat(char *__restrict dest, const char *__restrict src, size_t size); size_t strlcat(char *__restrict dest,
const char *__restrict src, size_t destsize);
size_t strlcat(char *__restrict dest, const char *__restrict src, size_t size) size_t strlcat(char *__restrict dest,
const char *__restrict src, size_t destsize)
{ {
size_t src_length = strlen(src); size_t src_length = strlen(src);
/* Our implementation strlcat supports dest == NULL if size == 0 /* Our implementation strlcat supports dest == NULL if size == 0
(for consistency with snprintf and strlcpy), but strnlen does (for consistency with snprintf and strlcpy), but strnlen does
not, so we have to cover this case explicitly. */ not, so we have to cover this case explicitly. */
if (size == 0) if (destsize == 0)
return src_length; return src_length;
size_t dest_length = strnlen(dest, size); size_t dest_length = strnlen(dest, destsize);
if (dest_length != size) { if (dest_length != destsize) {
/* Copy at most the remaining number of characters in the /* Copy at most the remaining number of characters in the
destination buffer. Leave for the NUL terminator. */ destination buffer. Leave for the NUL terminator. */
size_t to_copy = size - dest_length - 1; size_t to_copy = destsize - dest_length - 1;
/* But not more than what is available in the source string. */ /* But not more than what is available in the source string. */
if (to_copy > src_length) if (to_copy > src_length)
to_copy = src_length; to_copy = src_length;

View File

@ -27,23 +27,26 @@
#ifndef HAVE_STRLCPY #ifndef HAVE_STRLCPY
#undef strlcpy #undef strlcpy
size_t strlcpy(char *__restrict dest, const char *__restrict src, size_t size); size_t strlcpy(char *__restrict dest,
const char *__restrict src, size_t destsize);
size_t strlcpy(char *__restrict dest, const char *__restrict src, size_t size) size_t strlcpy(char *__restrict dest,
const char *__restrict src, size_t destsize)
{ {
size_t src_length = strlen(src); size_t src_length = strlen(src);
if (__builtin_expect(src_length >= size, 0)) { if (__builtin_expect(src_length >= destsize, 0)) {
if (size > 0) { if (destsize > 0) {
/* Copy the leading portion of the string. The last /*
character is subsequently overwritten with the NUL * Copy the leading portion of the string. The last
terminator, but the destination size is usually a * character is subsequently overwritten with the NUL
multiple of a small power of two, so writing it twice * terminator, but the destination destsize is usually
should be more efficient than copying an odd number * a multiple of a small power of two, so writing it
of * twice should be more efficient than copying an odd
bytes. */ * number of bytes.
memcpy(dest, src, size); */
dest[size - 1] = '\0'; memcpy(dest, src, destsize);
dest[destsize - 1] = '\0';
} }
} else } else
/* Copy the string and its terminating NUL character. */ /* Copy the string and its terminating NUL character. */

View File

@ -232,10 +232,12 @@ typedef unsigned char u_int8_t;
#include "zassert.h" #include "zassert.h"
#ifndef HAVE_STRLCAT #ifndef HAVE_STRLCAT
size_t strlcat(char *__restrict dest, const char *__restrict src, size_t size); size_t strlcat(char *__restrict dest,
const char *__restrict src, size_t destsize);
#endif #endif
#ifndef HAVE_STRLCPY #ifndef HAVE_STRLCPY
size_t strlcpy(char *__restrict dest, const char *__restrict src, size_t size); size_t strlcpy(char *__restrict dest,
const char *__restrict src, size_t destsize);
#endif #endif
#ifdef HAVE_BROKEN_CMSG_FIRSTHDR #ifdef HAVE_BROKEN_CMSG_FIRSTHDR