-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,28 @@ void StringUtil::rtrim(std::string& source) { | |
} | ||
} | ||
|
||
size_t StringUtil::strlcpy(char* dst, const char* src, size_t siz) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,12 @@ class StringUtil { | |
*/ | ||
static void rtrim(std::string& source); | ||
|
||
/** | ||
* size-bounded string copying and concatenation | ||
* Adapted from https://github.com/freebsd/freebsd/blob/20c3c08/sys/libkern/strlcpy.c | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: move this comment (where adapted from) into the implementation file. |
||
*/ | ||
static size_t strlcpy(char* dst, const char* src, size_t siz); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#include "stats_impl.h" | ||
#include "common/common/utility.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: newline before this line |
||
|
||
namespace Stats { | ||
|
||
|
@@ -17,7 +18,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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#include "gtest/gtest.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete |
||
|
||
#include "common/common/utility.h" | ||
|
||
TEST(StringUtil, atoul) { | ||
|
@@ -41,6 +43,13 @@ TEST(StringUtil, rtrim) { | |
} | ||
} | ||
|
||
TEST(StringUtil, strlcpy) { | ||
// TODO: more tests | ||
char dest[6]; | ||
StringUtil::strlcpy(dest, std::string{"hello"}.c_str(), sizeof(dest)); | ||
ASSERT_THAT("hello", testing::ContainerEq(dest)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EXPECT_STREQ |
||
} | ||
|
||
TEST(StringUtil, split) { | ||
EXPECT_EQ(std::vector<std::string>{}, StringUtil::split("", ',')); | ||
EXPECT_EQ(std::vector<std::string>{"a"}, StringUtil::split("a", ',')); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:size
There was a problem hiding this comment.
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?