Merge pull request #9397 from alalek:memcpy_null_guard

This commit is contained in:
Alexander Alekhin 2017-08-18 08:47:17 +00:00
commit 316fb26f5b
2 changed files with 16 additions and 15 deletions

View File

@ -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;
}

View File

@ -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;
}