From 2869e1ccd6a4048407bc95f65efd79e59c340a0a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 9 Jan 2021 17:22:04 +0100 Subject: [PATCH] Implement wxSysErrorMsg() in terms of wxSysErrorMsgStr() Instead of doing it the other way round, as it has several advantages: 1. wxSysErrorMsgStr() is not limited by the static buffer size any longer, i.e. doesn't truncate errors even longer than 1KiB. 2. Code is much simpler and more obviously correct. 3. We avoid an extra and absolutely unnecessary buffer copy. No real changes. --- src/common/log.cpp | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/common/log.cpp b/src/common/log.cpp index 968f871ea9..8c99a88277 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -1056,7 +1056,7 @@ unsigned long wxSysErrorCode() #endif //Win/Unix } -static const wxChar* GetSysErrorMsg(wxChar* szBuf, size_t sizeBuf, unsigned long nErrCode) +wxString wxSysErrorMsgStr(unsigned long nErrCode) { if ( nErrCode == 0 ) nErrCode = wxSysErrorCode(); @@ -1080,33 +1080,29 @@ static const wxChar* GetSysErrorMsg(wxChar* szBuf, size_t sizeBuf, unsigned long // if this happens, something is seriously wrong, so don't use _() here // for safety - wxSprintf(szBuf, wxS("unknown error 0x%lx"), nErrCode); - return szBuf; + return wxString::Format(wxS("unknown error 0x%lx"), nErrCode); } + wxString str; // copy it to our buffer and free memory // Crashes on SmartPhone (FIXME) if( lpMsgBuf != 0 ) { - wxStrlcpy(szBuf, (const wxChar *)lpMsgBuf, sizeBuf); + str = static_cast(lpMsgBuf); LocalFree(lpMsgBuf); // returned string is ended with '\r\n' - bad - size_t len = wxStrlen(szBuf); + size_t len = str.length(); if ( len >= 2 ) { // truncate string - if ( szBuf[len - 2] == wxS('\r') ) - szBuf[len - 2] = wxS('\0'); + if ( str[len - 2] == wxS('\r') ) + str.Truncate(len - 2); } } - else - { - szBuf[0] = wxS('\0'); - } - return szBuf; + return str; #else // !__WINDOWS__ char buffer[1024]; char *errorMsg = buffer; @@ -1123,29 +1119,18 @@ static const wxChar* GetSysErrorMsg(wxChar* szBuf, size_t sizeBuf, unsigned long #endif // at this point errorMsg might not point to buffer anymore - szBuf[0] = wxS('\0'); - #if wxUSE_UNICODE - wxConvCurrent->MB2WC(szBuf, errorMsg, sizeBuf - 1); - szBuf[sizeBuf - 1] = wxS('\0'); - #else - wxStrlcpy(szBuf, errorMsg, sizeBuf); - #endif - return szBuf; + return errorMsg; #endif // __WINDOWS__/!__WINDOWS__ } -// get error message from system +// get error message from system as a char pointer: this function has to use a +// static buffer of fixed size, so should be avoided in favour of the function +// returning wxString const wxChar *wxSysErrorMsg(unsigned long nErrCode) { static wxChar s_szBuf[1024]; - return GetSysErrorMsg(s_szBuf, WXSIZEOF(s_szBuf), nErrCode); -} - -// get error message from system as wxString -wxString wxSysErrorMsgStr(unsigned long nErrCode) -{ - wxChar szBuf[1024]; - return GetSysErrorMsg(szBuf, WXSIZEOF(szBuf), nErrCode); + wxStrlcpy(s_szBuf, wxSysErrorMsgStr(nErrCode), WXSIZEOF(s_szBuf)); + return s_szBuf; } #endif // wxUSE_LOG