Commit bc34a5a7 authored by Chip Turner's avatar Chip Turner Committed by woo

Make strlcpy available in folly

Summary:
strncpy is bad.  strlcpy is somewhat less bad.  We already had
this function, but let's move it somewhere more reasonable.

Test Plan: runtests

Reviewed By: ldbrandy@fb.com

Subscribers: trunkagent, lins, anca, folly-diffs@, yfeldblum, chalfant

FB internal diff: D2062632

Signature: t1:2062632:1431969926:cc7f7283073d0242fe8f361efac2557aa0b0a481
parent 3be65bf0
......@@ -20,6 +20,7 @@
#include <string.h>
#include <folly/Malloc.h>
#include <folly/String.h>
#if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
# include <cxxabi.h>
......@@ -55,21 +56,6 @@ extern "C" int cplus_demangle_v3_callback(
#endif
namespace {
// glibc doesn't have strlcpy
size_t my_strlcpy(char* dest, const char* src, size_t size) {
size_t len = strlen(src);
if (size != 0) {
size_t n = std::min(len, size - 1); // always null terminate!
memcpy(dest, src, n);
dest[n] = '\0';
}
return len;
}
} // namespace
namespace folly {
#if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
......@@ -119,7 +105,7 @@ size_t demangle(const char* name, char* out, size_t outSize) {
demangleCallback,
&dbuf);
if (status == 0) { // failed, return original
return my_strlcpy(out, name, outSize);
return folly::strlcpy(out, name, outSize);
}
if (outSize != 0) {
*dbuf.dest = '\0';
......@@ -134,7 +120,7 @@ fbstring demangle(const char* name) {
}
size_t demangle(const char* name, char* out, size_t outSize) {
return my_strlcpy(out, name, outSize);
return folly::strlcpy(out, name, outSize);
}
#endif
......
......@@ -492,6 +492,16 @@ void toLowerAscii(char* str, size_t length) {
}
}
size_t strlcpy(char* dest, const char* const src, size_t size) {
size_t len = strlen(src);
if (size != 0) {
size_t n = std::min(len, size - 1); // always null terminate!
memcpy(dest, src, n);
dest[n] = '\0';
}
return len;
}
namespace detail {
size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
......
......@@ -569,6 +569,9 @@ inline void toLowerAscii(MutableStringPiece str) {
toLowerAscii(str.begin(), str.size());
}
// glibc doesn't have strlcpy
size_t strlcpy(char* dest, const char* const src, size_t size);
} // namespace folly
// Hook into boost's type traits
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment