提交 316fb26f 编写于 作者: A Alexander Alekhin

Merge pull request #9397 from alalek:memcpy_null_guard

......@@ -598,6 +598,7 @@ String::String(const char* s)
{
if (!s) return;
size_t len = strlen(s);
if (!len) return;
memcpy(allocate(len), s, len);
}
......@@ -665,7 +666,7 @@ String& String::operator=(const char* s)
deallocate();
if (!s) return *this;
size_t len = strlen(s);
memcpy(allocate(len), s, len);
if (len) memcpy(allocate(len), s, len);
return *this;
}
......@@ -959,8 +960,8 @@ String operator + (const String& lhs, const String& rhs)
{
String s;
s.allocate(lhs.len_ + rhs.len_);
memcpy(s.cstr_, lhs.cstr_, lhs.len_);
memcpy(s.cstr_ + lhs.len_, rhs.cstr_, rhs.len_);
if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
if (rhs.len_) memcpy(s.cstr_ + lhs.len_, rhs.cstr_, rhs.len_);
return s;
}
......@@ -970,8 +971,8 @@ String operator + (const String& lhs, const char* rhs)
String s;
size_t rhslen = strlen(rhs);
s.allocate(lhs.len_ + rhslen);
memcpy(s.cstr_, lhs.cstr_, lhs.len_);
memcpy(s.cstr_ + lhs.len_, rhs, rhslen);
if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
if (rhslen) memcpy(s.cstr_ + lhs.len_, rhs, rhslen);
return s;
}
......@@ -981,8 +982,8 @@ String operator + (const char* lhs, const String& rhs)
String s;
size_t lhslen = strlen(lhs);
s.allocate(lhslen + rhs.len_);
memcpy(s.cstr_, lhs, lhslen);
memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
if (lhslen) memcpy(s.cstr_, lhs, lhslen);
if (rhs.len_) memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
return s;
}
......@@ -991,7 +992,7 @@ String operator + (const String& lhs, char rhs)
{
String s;
s.allocate(lhs.len_ + 1);
memcpy(s.cstr_, lhs.cstr_, lhs.len_);
if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
s.cstr_[lhs.len_] = rhs;
return s;
}
......@@ -1002,7 +1003,7 @@ String operator + (char lhs, const String& rhs)
String s;
s.allocate(rhs.len_ + 1);
s.cstr_[0] = lhs;
memcpy(s.cstr_ + 1, rhs.cstr_, rhs.len_);
if (rhs.len_) memcpy(s.cstr_ + 1, rhs.cstr_, rhs.len_);
return s;
}
......
......@@ -80,7 +80,7 @@ String::String(const std::string& str)
if (!str.empty())
{
size_t len = str.size();
memcpy(allocate(len), str.c_str(), len);
if (len) memcpy(allocate(len), str.c_str(), len);
}
}
......@@ -102,7 +102,7 @@ String& String::operator = (const std::string& str)
if (!str.empty())
{
size_t len = str.size();
memcpy(allocate(len), str.c_str(), len);
if (len) memcpy(allocate(len), str.c_str(), len);
}
return *this;
}
......@@ -126,8 +126,8 @@ String operator + (const String& lhs, const std::string& rhs)
String s;
size_t rhslen = rhs.size();
s.allocate(lhs.len_ + rhslen);
memcpy(s.cstr_, lhs.cstr_, lhs.len_);
memcpy(s.cstr_ + lhs.len_, rhs.c_str(), rhslen);
if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
if (rhslen) memcpy(s.cstr_ + lhs.len_, rhs.c_str(), rhslen);
return s;
}
......@@ -137,8 +137,8 @@ String operator + (const std::string& lhs, const String& rhs)
String s;
size_t lhslen = lhs.size();
s.allocate(lhslen + rhs.len_);
memcpy(s.cstr_, lhs.c_str(), lhslen);
memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
if (lhslen) memcpy(s.cstr_, lhs.c_str(), lhslen);
if (rhs.len_) memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
return s;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册