From: Václav Slavík Date: Fri, 29 Jun 2007 19:28:54 +0000 (+0000) Subject: fixed wxVsnprintf() to write as much as it can if the output buffer is too short X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/ec873c943d71f0d5f13e3398557071448cda6c23 fixed wxVsnprintf() to write as much as it can if the output buffer is too short git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47029 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/common/wxcrt.cpp b/src/common/wxcrt.cpp index 2b862a043a..e4e5426cf3 100644 --- a/src/common/wxcrt.cpp +++ b/src/common/wxcrt.cpp @@ -552,7 +552,7 @@ namespace { #if !wxUSE_UTF8_LOCALE_ONLY -int wxInternalConvertStringToBuf(const wxString& s, char *out, size_t outsize) +int ConvertStringToBuf(const wxString& s, char *out, size_t outsize) { const wxWX2WCbuf buf = s.wc_str(); @@ -565,13 +565,19 @@ int wxInternalConvertStringToBuf(const wxString& s, char *out, size_t outsize) #endif // !wxUSE_UTF8_LOCALE_ONLY #if wxUSE_UNICODE_UTF8 -int wxInternalConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize) +int ConvertStringToBuf(const wxString& s, wchar_t *out, size_t outsize) { const wxWX2WCbuf buf(s.wc_str()); - size_t len = wxWcslen(buf); + size_t len = s.length(); // same as buf length for wchar_t* if ( outsize > len ) + { memcpy(out, buf, (len+1) * sizeof(wchar_t)); - // else: not enough space + } + else // not enough space + { + memcpy(out, buf, (outsize-1) * sizeof(wchar_t)); + out[outsize-1] = 0; + } return len; } #endif // wxUSE_UNICODE_UTF8 @@ -585,7 +591,7 @@ static size_t PrintfViaString(T *out, size_t outsize, wxString s; s.PrintfV(format, argptr); - return wxInternalConvertStringToBuf(s, out, outsize); + return ConvertStringToBuf(s, out, outsize); } #endif // wxUSE_UNICODE