From 26378b412c51fe424c06ef38a61a644e72f63a74 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 2 Jan 2005 23:54:40 +0000 Subject: [PATCH] don't crash in wxStrftime() if conversion of strftime() result to Unicode fails (modified patch 1094100) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@31216 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/wxchar.cpp | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/common/wxchar.cpp b/src/common/wxchar.cpp index 9db633db07..793b0b3204 100644 --- a/src/common/wxchar.cpp +++ b/src/common/wxchar.cpp @@ -1492,24 +1492,28 @@ int WXDLLEXPORT wxSystem(const wxChar *psz) #endif // wxNEED_WX_STDLIB_H #ifdef wxNEED_WX_TIME_H -WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm) +WXDLLEXPORT size_t +wxStrftime(wxChar *s, size_t maxsize, const wxChar *fmt, const struct tm *tm) { - if (!max) return 0; + if ( !maxsize ) + return 0; - char *buf = (char *)malloc(max); - size_t ret = strftime(buf, max, wxConvLocal.cWX2MB(fmt), tm); - if (ret) - { - wxStrcpy(s, wxConvLocal.cMB2WX(buf)); - free(buf); - return wxStrlen(s); - } - else - { - free(buf); - *s = 0; + wxCharBuffer buf(maxsize); + + wxCharBuffer bufFmt(wxConvLocal.cWX2MB(fmt)); + if ( !bufFmt ) return 0; - } + + size_t ret = strftime(buf.data(), maxsize, bufFmt, tm); + if ( !ret ) + return 0; + + wxWCharBuffer wbuf = wxConvLocal.cMB2WX(buf); + if ( !wbuf ) + return 0; + + wxStrncpy(s, wbuf, maxsize); + return wxStrlen(s); } #endif // wxNEED_WX_TIME_H -- 2.45.2