From 0e555c213ddf1fa155a170f56bc13bc0d38fa412 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 30 Sep 2010 14:30:41 +0000 Subject: [PATCH] Correctly convert wxPrintf() to a buffer even when it doesn't fit. ConvertStringToBuf() helper function was defined incorrectly for converting wxString to a char* buffer as it didn't fill the buffer at all if the string didn't fit into it entirely instead of putting as much of the string into it as possible as was already done for the conversion to wchar_t* buffer. This broke wxSprintf()-related functions in when the ASCII output buffer was not big enough as it was not filled at all. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65691 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/wxcrt.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/common/wxcrt.cpp b/src/common/wxcrt.cpp index 55ad0812a8..dc51b48e41 100644 --- a/src/common/wxcrt.cpp +++ b/src/common/wxcrt.cpp @@ -586,13 +586,20 @@ namespace #if !wxUSE_UTF8_LOCALE_ONLY int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) { - const wxWX2WCbuf buf = s.wc_str(); + const wxCharBuffer buf(s.mb_str()); - size_t len = wxConvLibc.FromWChar(out, outsize, buf); - if ( len != wxCONV_FAILED ) - return len-1; - else - return wxConvLibc.FromWChar(NULL, 0, buf); + const size_t len = buf.length(); + if ( outsize > len ) + { + memcpy(out, buf, (len+1) * sizeof(char)); + } + else // not enough space + { + memcpy(out, buf, (outsize-1) * sizeof(char)); + out[outsize-1] = '\0'; + } + + return len; } #endif // !wxUSE_UTF8_LOCALE_ONLY -- 2.45.2