Commit 316fb26f authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #9397 from alalek:memcpy_null_guard

parents a8355170 71e18898
...@@ -598,6 +598,7 @@ String::String(const char* s) ...@@ -598,6 +598,7 @@ String::String(const char* s)
{ {
if (!s) return; if (!s) return;
size_t len = strlen(s); size_t len = strlen(s);
if (!len) return;
memcpy(allocate(len), s, len); memcpy(allocate(len), s, len);
} }
...@@ -665,7 +666,7 @@ String& String::operator=(const char* s) ...@@ -665,7 +666,7 @@ String& String::operator=(const char* s)
deallocate(); deallocate();
if (!s) return *this; if (!s) return *this;
size_t len = strlen(s); size_t len = strlen(s);
memcpy(allocate(len), s, len); if (len) memcpy(allocate(len), s, len);
return *this; return *this;
} }
...@@ -959,8 +960,8 @@ String operator + (const String& lhs, const String& rhs) ...@@ -959,8 +960,8 @@ String operator + (const String& lhs, const String& rhs)
{ {
String s; String s;
s.allocate(lhs.len_ + rhs.len_); s.allocate(lhs.len_ + rhs.len_);
memcpy(s.cstr_, lhs.cstr_, lhs.len_); if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
memcpy(s.cstr_ + lhs.len_, rhs.cstr_, rhs.len_); if (rhs.len_) memcpy(s.cstr_ + lhs.len_, rhs.cstr_, rhs.len_);
return s; return s;
} }
...@@ -970,8 +971,8 @@ String operator + (const String& lhs, const char* rhs) ...@@ -970,8 +971,8 @@ String operator + (const String& lhs, const char* rhs)
String s; String s;
size_t rhslen = strlen(rhs); size_t rhslen = strlen(rhs);
s.allocate(lhs.len_ + rhslen); s.allocate(lhs.len_ + rhslen);
memcpy(s.cstr_, lhs.cstr_, lhs.len_); if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
memcpy(s.cstr_ + lhs.len_, rhs, rhslen); if (rhslen) memcpy(s.cstr_ + lhs.len_, rhs, rhslen);
return s; return s;
} }
...@@ -981,8 +982,8 @@ String operator + (const char* lhs, const String& rhs) ...@@ -981,8 +982,8 @@ String operator + (const char* lhs, const String& rhs)
String s; String s;
size_t lhslen = strlen(lhs); size_t lhslen = strlen(lhs);
s.allocate(lhslen + rhs.len_); s.allocate(lhslen + rhs.len_);
memcpy(s.cstr_, lhs, lhslen); if (lhslen) memcpy(s.cstr_, lhs, lhslen);
memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_); if (rhs.len_) memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
return s; return s;
} }
...@@ -991,7 +992,7 @@ String operator + (const String& lhs, char rhs) ...@@ -991,7 +992,7 @@ String operator + (const String& lhs, char rhs)
{ {
String s; String s;
s.allocate(lhs.len_ + 1); 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; s.cstr_[lhs.len_] = rhs;
return s; return s;
} }
...@@ -1002,7 +1003,7 @@ String operator + (char lhs, const String& rhs) ...@@ -1002,7 +1003,7 @@ String operator + (char lhs, const String& rhs)
String s; String s;
s.allocate(rhs.len_ + 1); s.allocate(rhs.len_ + 1);
s.cstr_[0] = lhs; 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; return s;
} }
......
...@@ -80,7 +80,7 @@ String::String(const std::string& str) ...@@ -80,7 +80,7 @@ String::String(const std::string& str)
if (!str.empty()) if (!str.empty())
{ {
size_t len = str.size(); 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) ...@@ -102,7 +102,7 @@ String& String::operator = (const std::string& str)
if (!str.empty()) if (!str.empty())
{ {
size_t len = str.size(); size_t len = str.size();
memcpy(allocate(len), str.c_str(), len); if (len) memcpy(allocate(len), str.c_str(), len);
} }
return *this; return *this;
} }
...@@ -126,8 +126,8 @@ String operator + (const String& lhs, const std::string& rhs) ...@@ -126,8 +126,8 @@ String operator + (const String& lhs, const std::string& rhs)
String s; String s;
size_t rhslen = rhs.size(); size_t rhslen = rhs.size();
s.allocate(lhs.len_ + rhslen); s.allocate(lhs.len_ + rhslen);
memcpy(s.cstr_, lhs.cstr_, lhs.len_); if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
memcpy(s.cstr_ + lhs.len_, rhs.c_str(), rhslen); if (rhslen) memcpy(s.cstr_ + lhs.len_, rhs.c_str(), rhslen);
return s; return s;
} }
...@@ -137,8 +137,8 @@ String operator + (const std::string& lhs, const String& rhs) ...@@ -137,8 +137,8 @@ String operator + (const std::string& lhs, const String& rhs)
String s; String s;
size_t lhslen = lhs.size(); size_t lhslen = lhs.size();
s.allocate(lhslen + rhs.len_); s.allocate(lhslen + rhs.len_);
memcpy(s.cstr_, lhs.c_str(), lhslen); if (lhslen) memcpy(s.cstr_, lhs.c_str(), lhslen);
memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_); if (rhs.len_) memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
return s; return s;
} }
......
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