Index: src/libchcore/TString.cpp =================================================================== diff -u -r9545b48aadb1592a5bf22ac5a227b1666d7710c6 -rd56d66c19fc54562eb0c8e54bc6acff1e7eb4ef8 --- src/libchcore/TString.cpp (.../TString.cpp) (revision 9545b48aadb1592a5bf22ac5a227b1666d7710c6) +++ src/libchcore/TString.cpp (.../TString.cpp) (revision d56d66c19fc54562eb0c8e54bc6acff1e7eb4ef8) @@ -763,21 +763,20 @@ */ void TString::SetString(const wchar_t* pszStr) { - if(!pszStr || pszStr[0] == _T('\0')) - { - // set empty string in internal data, but only if we already have something allocated - // otherwise we already have an "empty" string - if(m_pszStringData) - SetString(_T("")); - } - else - { - size_t stStringLen = wcslen(pszStr); - EnsureWritable(stStringLen + 1); + // when we want to set an empty string to not initialized TString then + // there is no need to allocate buffer just yet + if(!m_pszStringData && (!pszStr || pszStr[0] == _T('\0'))) + return; - wcsncpy_s(m_pszStringData, GetCurrentBufferSize(), pszStr, stStringLen + 1); - GetInternalStringData()->SetStringLength(stStringLen); - } + // if NULL provided - treat it as an empty string + if(!pszStr) + pszStr = _T(""); + + size_t stStringLen = wcslen(pszStr); + EnsureWritable(stStringLen + 1); + + wcsncpy_s(m_pszStringData, GetCurrentBufferSize(), pszStr, stStringLen + 1); + GetInternalStringData()->SetStringLength(stStringLen); } void TString::SetString(const wchar_t* pszStart, size_t stCount) Index: src/libchcore/Tests/TestsTString.cpp =================================================================== diff -u -r9545b48aadb1592a5bf22ac5a227b1666d7710c6 -rd56d66c19fc54562eb0c8e54bc6acff1e7eb4ef8 --- src/libchcore/Tests/TestsTString.cpp (.../TestsTString.cpp) (revision 9545b48aadb1592a5bf22ac5a227b1666d7710c6) +++ src/libchcore/Tests/TestsTString.cpp (.../TestsTString.cpp) (revision d56d66c19fc54562eb0c8e54bc6acff1e7eb4ef8) @@ -597,3 +597,13 @@ EXPECT_EQ(strValue.GetLength(), 11); } + +// corner cases and detected bugs +TEST(TStringTests, AssignEmptyStringToAlreadyInitializedTString) +{ + TString strValue(_T("Some string")); + + // with this we had infinite recurrence + EXPECT_NO_THROW(strValue = _T("")); + EXPECT_EQ(strValue, _T("")); +}