Skip to content

Commit 316fb26

Browse files
committed
Merge pull request opencv#9397 from alalek:memcpy_null_guard
2 parents a835517 + 71e1889 commit 316fb26

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

modules/core/include/opencv2/core/cvstd.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ String::String(const char* s)
598598
{
599599
if (!s) return;
600600
size_t len = strlen(s);
601+
if (!len) return;
601602
memcpy(allocate(len), s, len);
602603
}
603604

@@ -665,7 +666,7 @@ String& String::operator=(const char* s)
665666
deallocate();
666667
if (!s) return *this;
667668
size_t len = strlen(s);
668-
memcpy(allocate(len), s, len);
669+
if (len) memcpy(allocate(len), s, len);
669670
return *this;
670671
}
671672

@@ -959,8 +960,8 @@ String operator + (const String& lhs, const String& rhs)
959960
{
960961
String s;
961962
s.allocate(lhs.len_ + rhs.len_);
962-
memcpy(s.cstr_, lhs.cstr_, lhs.len_);
963-
memcpy(s.cstr_ + lhs.len_, rhs.cstr_, rhs.len_);
963+
if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
964+
if (rhs.len_) memcpy(s.cstr_ + lhs.len_, rhs.cstr_, rhs.len_);
964965
return s;
965966
}
966967

@@ -970,8 +971,8 @@ String operator + (const String& lhs, const char* rhs)
970971
String s;
971972
size_t rhslen = strlen(rhs);
972973
s.allocate(lhs.len_ + rhslen);
973-
memcpy(s.cstr_, lhs.cstr_, lhs.len_);
974-
memcpy(s.cstr_ + lhs.len_, rhs, rhslen);
974+
if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
975+
if (rhslen) memcpy(s.cstr_ + lhs.len_, rhs, rhslen);
975976
return s;
976977
}
977978

@@ -981,8 +982,8 @@ String operator + (const char* lhs, const String& rhs)
981982
String s;
982983
size_t lhslen = strlen(lhs);
983984
s.allocate(lhslen + rhs.len_);
984-
memcpy(s.cstr_, lhs, lhslen);
985-
memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
985+
if (lhslen) memcpy(s.cstr_, lhs, lhslen);
986+
if (rhs.len_) memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
986987
return s;
987988
}
988989

@@ -991,7 +992,7 @@ String operator + (const String& lhs, char rhs)
991992
{
992993
String s;
993994
s.allocate(lhs.len_ + 1);
994-
memcpy(s.cstr_, lhs.cstr_, lhs.len_);
995+
if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
995996
s.cstr_[lhs.len_] = rhs;
996997
return s;
997998
}
@@ -1002,7 +1003,7 @@ String operator + (char lhs, const String& rhs)
10021003
String s;
10031004
s.allocate(rhs.len_ + 1);
10041005
s.cstr_[0] = lhs;
1005-
memcpy(s.cstr_ + 1, rhs.cstr_, rhs.len_);
1006+
if (rhs.len_) memcpy(s.cstr_ + 1, rhs.cstr_, rhs.len_);
10061007
return s;
10071008
}
10081009

modules/core/include/opencv2/core/cvstd.inl.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ String::String(const std::string& str)
8080
if (!str.empty())
8181
{
8282
size_t len = str.size();
83-
memcpy(allocate(len), str.c_str(), len);
83+
if (len) memcpy(allocate(len), str.c_str(), len);
8484
}
8585
}
8686

@@ -102,7 +102,7 @@ String& String::operator = (const std::string& str)
102102
if (!str.empty())
103103
{
104104
size_t len = str.size();
105-
memcpy(allocate(len), str.c_str(), len);
105+
if (len) memcpy(allocate(len), str.c_str(), len);
106106
}
107107
return *this;
108108
}
@@ -126,8 +126,8 @@ String operator + (const String& lhs, const std::string& rhs)
126126
String s;
127127
size_t rhslen = rhs.size();
128128
s.allocate(lhs.len_ + rhslen);
129-
memcpy(s.cstr_, lhs.cstr_, lhs.len_);
130-
memcpy(s.cstr_ + lhs.len_, rhs.c_str(), rhslen);
129+
if (lhs.len_) memcpy(s.cstr_, lhs.cstr_, lhs.len_);
130+
if (rhslen) memcpy(s.cstr_ + lhs.len_, rhs.c_str(), rhslen);
131131
return s;
132132
}
133133

@@ -137,8 +137,8 @@ String operator + (const std::string& lhs, const String& rhs)
137137
String s;
138138
size_t lhslen = lhs.size();
139139
s.allocate(lhslen + rhs.len_);
140-
memcpy(s.cstr_, lhs.c_str(), lhslen);
141-
memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
140+
if (lhslen) memcpy(s.cstr_, lhs.c_str(), lhslen);
141+
if (rhs.len_) memcpy(s.cstr_ + lhslen, rhs.cstr_, rhs.len_);
142142
return s;
143143
}
144144

0 commit comments

Comments
 (0)