]> git.saurik.com Git - wxWidgets.git/commitdiff
Ensure wxCharTypeBuffer data is NUL-terminated after extend() call.
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 20 Jan 2012 22:11:51 +0000 (22:11 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 20 Jan 2012 22:11:51 +0000 (22:11 +0000)
As wxCharTypeBuffer ctor taking the length NUL-terminates the buffer, it may
be expected that extend() does the same but it did not. Do add the NUL at the
end for consistency, even though it's not really needed for the existing code
using extend() in wxWidgets itself.

Closes #13885.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70417 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/buffer.h
tests/strings/strings.cpp

index 800c41b71431183f652977a56ab2411c3e7facbe..ec0e1fc7651e541c116a88a402f6c2e2621de06a 100644 (file)
@@ -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);
index af18513045d5d9e0db16715c1e28dad2baa0e7db..39e1904fd6dd2189cbc709744728b5e3f5adf002 100644 (file)
@@ -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]);
 }