]> git.saurik.com Git - wxWidgets.git/commitdiff
don't crash if (char*)c_str() is used twice in a row on the same string and both...
authorVáclav Slavík <vslavik@fastmail.fm>
Tue, 10 Apr 2007 19:13:52 +0000 (19:13 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Tue, 10 Apr 2007 19:13:52 +0000 (19:13 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45389 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/string.h
src/common/string.cpp

index 3cb68a0df4ae5b22893537a6d9f8f1cdf31e06dc..5f3801ef5126193e7eac677cd47f2614b8b128ce 100644 (file)
@@ -2010,7 +2010,7 @@ private:
       ~ConvertedBuffer()
           { free(m_buf); }
 
-      operator const T*() const { return m_buf; }
+      operator T*() const { return m_buf; }
 
       ConvertedBuffer& operator=(T *str)
       {
index 564f3ea4f12048a05af9405824f5da24a0479a03..737ae2441c9727812d9d0e12372f3f1474dc845c 100644 (file)
@@ -198,8 +198,24 @@ wxString::~wxString()
 const char* wxCStrData::AsChar() const
 {
     wxString *str = wxConstCast(m_str, wxString);
-    // convert the string and keep it:
-    str->m_convertedToChar = str->mb_str().release();
+
+    // convert the string:
+    wxCharBuffer buf(str->mb_str());
+
+    // FIXME-UTF8: do the conversion in-place in the existing buffer
+    if ( str->m_convertedToChar &&
+         strlen(buf) == strlen(str->m_convertedToChar) )
+    {
+        // keep the same buffer for as long as possible, so that several calls
+        // to c_str() in a row still work:
+        strcpy(str->m_convertedToChar, buf);
+    }
+    else
+    {
+        str->m_convertedToChar = buf.release();
+    }
+
+    // and keep it:
     return str->m_convertedToChar + m_offset;
 }
 #endif // wxUSE_UNICODE
@@ -208,8 +224,24 @@ const char* wxCStrData::AsChar() const
 const wchar_t* wxCStrData::AsWChar() const
 {
     wxString *str = wxConstCast(m_str, wxString);
-    // convert the string and keep it:
-    str->m_convertedToWChar = str->wc_str().release();
+
+    // convert the string:
+    wxWCharBuffer buf(str->wc_str());
+
+    // FIXME-UTF8: do the conversion in-place in the existing buffer
+    if ( str->m_convertedToWChar &&
+         wxWcslen(buf) == wxWcslen(str->m_convertedToWChar) )
+    {
+        // keep the same buffer for as long as possible, so that several calls
+        // to c_str() in a row still work:
+        memcpy(str->m_convertedToWChar, buf, sizeof(wchar_t) * wxWcslen(buf));
+    }
+    else
+    {
+        str->m_convertedToWChar = buf.release();
+    }
+
+    // and keep it:
     return str->m_convertedToWChar + m_offset;
 }
 #endif // !wxUSE_UNICODE_WCHAR