fixed a bug in String::operator+ and String::operator+=

there was enough space allocated for both strings & the null-terminator
This commit is contained in:
Kjistóf 2016-10-25 15:05:40 +02:00
parent b74a35d1ec
commit af27d29d29

View File

@ -51,7 +51,7 @@ String::String(String&& other) noexcept : String() // init via default ctor so
String String::operator+(const String& rhs) const String String::operator+(const String& rhs) const
{ {
auto size = this->size() + rhs.size() - 1; // minus one null-terminator, we need only one auto size = this->size() + rhs.size() + 1; // plus a null-terminator
auto data = new char[size]; // make some space auto data = new char[size]; // make some space
std::strcpy(data, this->c_str()); // copy data of this std::strcpy(data, this->c_str()); // copy data of this
String ret; String ret;
@ -62,7 +62,7 @@ String String::operator+(const String& rhs) const
String& String::operator+=(const String& other) String& String::operator+=(const String& other)
{ {
auto size = this->size() + other.size() - 1; // minus one null-terminator, we need only one auto size = this->size() + other.size() + 1; // plus a null-terminator
auto data = new char[size]; auto data = new char[size];
std::strcpy(data, this->c_str()); // copy our data std::strcpy(data, this->c_str()); // copy our data
std::strcat(data, other.c_str()); // append data of the other string std::strcat(data, other.c_str()); // append data of the other string