diff --git a/include/wx/buffer.h b/include/wx/buffer.h index 800c41b714..ec0e1fc765 100644 --- a/include/wx/buffer.h +++ b/include/wx/buffer.h @@ -312,6 +312,10 @@ public: if ( !str ) return false; + // For consistency with the ctor taking just the length, NUL-terminate + // the buffer. + str[len] = (CharType)0; + if ( this->m_data == this->GetNullData() ) { this->m_data = new Data(str, len); diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index af18513045..39e1904fd6 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -1039,4 +1039,18 @@ void StringTestCase::ScopedBuffers() wxCharBuffer buf2 = sbuf; CPPUNIT_ASSERT( buf2.data() != literal ); CPPUNIT_ASSERT_EQUAL( literal, buf.data() ); + + // Check that extending the buffer keeps it NUL-terminated. + size_t len = 10; + + wxCharBuffer buf3(len); + CPPUNIT_ASSERT_EQUAL('\0', buf3.data()[len]); + + wxCharBuffer buf4; + buf4.extend(len); + CPPUNIT_ASSERT_EQUAL('\0', buf4.data()[len]); + + wxCharBuffer buf5(5); + buf5.extend(len); + CPPUNIT_ASSERT_EQUAL('\0', buf5.data()[len]); }