From af27d29d29d50faffd290e7d54d9f1eca84bae7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Tue, 25 Oct 2016 15:05:40 +0200 Subject: [PATCH] fixed a bug in String::operator+ and String::operator+= there was enough space allocated for both strings & the null-terminator --- String.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/String.cpp b/String.cpp index 3ec4b01..c460e43 100644 --- a/String.cpp +++ b/String.cpp @@ -51,7 +51,7 @@ String::String(String&& other) noexcept : String() // init via default ctor so 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 std::strcpy(data, this->c_str()); // copy data of this String ret; @@ -62,7 +62,7 @@ String String::operator+(const String& rhs) const 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]; std::strcpy(data, this->c_str()); // copy our data std::strcat(data, other.c_str()); // append data of the other string