diff --git a/docs/changes.txt b/docs/changes.txt index aab70beee2..b14ab8300a 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -567,6 +567,14 @@ Major new features in this release was added. +3.0.0: (released 2013-11-xx) +---------------------------- + +All: + +- Fix crash if wxCharBuffer fails to allocate the requested amount of memory. + + 3.0-RC2: (released 2013-10-28) ------------------------------ diff --git a/include/wx/buffer.h b/include/wx/buffer.h index c88e160270..9891494d97 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -268,9 +268,22 @@ public: wxCharTypeBuffer(size_t len) { - this->m_data = - new Data((CharType *)malloc((len + 1)*sizeof(CharType)), len); - this->m_data->Get()[len] = (CharType)0; + CharType* const str = (CharType *)malloc((len + 1)*sizeof(CharType)); + if ( str ) + { + str[len] = (CharType)0; + + // There is a potential memory leak here if new throws because it + // fails to allocate Data, we ought to use new(nothrow) here, but + // this might fail to compile under some platforms so until this + // can be fully tested, just live with this (rather unlikely, as + // Data is a small object) potential leak. + this->m_data = new Data(str, len); + } + else + { + this->m_data = this->GetNullData(); + } } wxCharTypeBuffer(const wxCharTypeBuffer& src)