Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace strncpy with strlcpy #207

Merged
merged 5 commits into from
Nov 29, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions source/common/common/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ void StringUtil::rtrim(std::string& source) {
}
}

/**
* strlcpy adapted from https://github.com/freebsd/freebsd/blob/20c3c08/sys/libkern/strlcpy.c
*/
size_t StringUtil::strlcpy(char* dst, const char* src, size_t siz) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:size

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you use full words for variables?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: siz -> size

char* d = dst;
const char* s = src;
size_t n = siz;

if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}

if (n == 0) {
if (siz != 0)
*d = '\0';
while (*s++)
;
}

return (s - src - 1);
}

std::vector<std::string> StringUtil::split(const std::string& source, char split) {
std::vector<std::string> ret;
size_t last_index = 0;
Expand Down
5 changes: 5 additions & 0 deletions source/common/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class StringUtil {
*/
static void rtrim(std::string& source);

/**
* Size-bounded string copying and concatenation
*/
static size_t strlcpy(char* dst, const char* src, size_t siz);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: siz -> size


/**
* Split a string.
* @param source supplies the string to split.
Expand Down
2 changes: 1 addition & 1 deletion source/common/network/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ sockaddr_un Utility::resolveUnixDomainSocket(const std::string& path) {
sockaddr_un address;
memset(&address, 0, sizeof(address));
address.sun_family = AF_UNIX;
strncpy(&address.sun_path[0], path.c_str(), sizeof(address.sun_path));
StringUtil::strlcpy(&address.sun_path[0], path.c_str(), sizeof(address.sun_path));
return address;
}

Expand Down
4 changes: 3 additions & 1 deletion source/common/stats/stats_impl.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "stats_impl.h"

#include "common/common/utility.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline before this line


namespace Stats {

void TimerImpl::TimespanImpl::complete(const std::string& dynamic_name) {
Expand All @@ -17,7 +19,7 @@ RawStatData* HeapRawStatDataAllocator::alloc(const std::string& name) {
void RawStatData::initialize(const std::string& name) {
ASSERT(!initialized());
ASSERT(name.size() <= MAX_NAME_SIZE);
strncpy(name_, name.substr(0, MAX_NAME_SIZE).c_str(), MAX_NAME_SIZE + 1);
StringUtil::strlcpy(name_, name.substr(0, MAX_NAME_SIZE).c_str(), MAX_NAME_SIZE + 1);
}

bool RawStatData::matches(const std::string& name) {
Expand Down
8 changes: 5 additions & 3 deletions source/exe/hot_restart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "envoy/server/instance.h"
#include "envoy/server/options.h"

#include "common/common/utility.h"

#include <sys/mman.h>
#include <sys/prctl.h>

Expand Down Expand Up @@ -119,9 +121,9 @@ sockaddr_un HotRestartImpl::createDomainSocketAddress(uint64_t id) {
sockaddr_un address;
memset(&address, 0, sizeof(address));
address.sun_family = AF_UNIX;
strncpy(&address.sun_path[1],
fmt::format("envoy_domain_socket_{}", options_.baseId() + id).c_str(),
sizeof(address.sun_path) - 1);
StringUtil::strlcpy(&address.sun_path[1],
fmt::format("envoy_domain_socket_{}", options_.baseId() + id).c_str(),
sizeof(address.sun_path) - 1);
address.sun_path[0] = 0;
return address;
}
Expand Down
7 changes: 7 additions & 0 deletions test/common/common/utility_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ TEST(StringUtil, rtrim) {
}
}

TEST(StringUtil, strlcpy) {
// TODO: more tests
char dest[6];
StringUtil::strlcpy(dest, std::string{"hello"}.c_str(), sizeof(dest));
EXPECT_STREQ("hello", dest);
}

TEST(StringUtil, split) {
EXPECT_EQ(std::vector<std::string>{}, StringUtil::split("", ','));
EXPECT_EQ(std::vector<std::string>{"a"}, StringUtil::split("a", ','));
Expand Down