From 536025791ac8ea532b952529e88913c5a1ab56fd Mon Sep 17 00:00:00 2001 From: Paul Cornett Date: Sun, 25 Aug 2019 23:08:59 -0700 Subject: [PATCH] Avoid converting empty strings to NULL pointers in non-Unicode build ...when using utf8_str() or wxGTK_CONV as argument to const char* function parameter --- include/wx/string.h | 6 +++++- src/gtk/utilsgtk.cpp | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/wx/string.h b/include/wx/string.h index a5a3b3108a..6e579ed616 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -1709,7 +1709,11 @@ public: { return FromUTF8Unchecked(utf8.c_str(), utf8.length()); } #endif const wxScopedCharBuffer utf8_str() const - { return wxMBConvUTF8().cWC2MB(wc_str()); } + { + if (empty()) + return wxScopedCharBuffer::CreateNonOwned("", 0); + return wxMBConvUTF8().cWC2MB(wc_str()); + } #endif const wxScopedCharBuffer ToUTF8() const { return utf8_str(); } diff --git a/src/gtk/utilsgtk.cpp b/src/gtk/utilsgtk.cpp index 137fb2cf16..4a7afc9edc 100644 --- a/src/gtk/utilsgtk.cpp +++ b/src/gtk/utilsgtk.cpp @@ -86,6 +86,9 @@ wxWindow* wxFindWindowAtPoint(const wxPoint& pt) WXDLLIMPEXP_CORE wxCharBuffer wxConvertToGTK(const wxString& s, wxFontEncoding enc) { + if (s.empty()) + return wxCharBuffer(""); + wxWCharBuffer wbuf; if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT ) { @@ -96,7 +99,7 @@ wxConvertToGTK(const wxString& s, wxFontEncoding enc) wbuf = wxCSConv(enc).cMB2WC(s.c_str()); } - if ( !wbuf && !s.empty() ) + if (wbuf.length() == 0) { // conversion failed, but we still want to show something to the user // even if it's going to be wrong it is better than nothing