Skip to content

Commit

Permalink
Add a few more comments on strlcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
shinh committed Apr 10, 2013
1 parent 54837e8 commit b1d3c52
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions libmac/mac.c
Expand Up @@ -1079,8 +1079,14 @@ size_t strlcpy(char* dst, const char* src, size_t size) {
size_t src_size = strlen(src) + 1; // +1 for '\0'
LOGF("strlcpy: len(src)=%zu\n", src_size);
if (size != 0) {
// As far as I investigated, glibc strncpy is unbelievably slow if |size|
// is much larger than strlen(|src|) e.g. 16K bytes vs 64 bytes.
// We don't use strncpy because strncpy is slow if |size| is
// much larger than strlen(|src|) e.g. 16K bytes vs 64 bytes.
//
// The reason of this is explained by glibc's man:
//
// If the length of src is less than n, strncpy() writes additional
// null bytes to dest to ensure that a total of n bytes are written.
//
// Also, we already know the length of |src|, we can use memcpy.
if (src_size < size)
size = src_size;
Expand Down

0 comments on commit b1d3c52

Please sign in to comment.