Allow disabling unsafe implicit conversions in wxString

While we have to keep these conversions enabled by default, they are very
dangerous as they can result in silent data loss on any system not using a
locale with UTF-8 encoding, i.e. always under MSW.

Allow mitigating this by defining wxNO_UNSAFE_WXSTRING_CONV when compiling the
application code using the library, which makes these conversions invisible to
the user code, and so can be used without recompiling the library.

Also add wxUSE_UNSAFE_WXSTRING_CONV which can be set to 0 when compiling the
library to disable these conversions globally for all applications using it.

Closes #11830.
This commit is contained in:
Vadim Zeitlin
2017-02-12 00:47:35 +01:00
parent 4cc45797a1
commit e125c3b657
14 changed files with 159 additions and 1 deletions

View File

@@ -1263,7 +1263,9 @@ public:
// they conflict with the implicit conversions to "const char/wchar_t *"
// which we use for backwards compatibility but do provide them if
// explicitly requested.
#if wxUSE_UNSAFE_WXSTRING_CONV && !defined(wxNO_UNSAFE_WXSTRING_CONV)
operator wxStringToStdStringRetType() const { return ToStdString(); }
#endif // wxUSE_UNSAFE_WXSTRING_CONV
operator wxStringToStdWstringRetType() const { return ToStdWstring(); }
#endif // wxUSE_STD_STRING_CONV_IN_WXSTRING
@@ -1517,13 +1519,16 @@ public:
// messages for the code which relies on implicit conversion to char* in
// STL build
#if !wxUSE_STD_STRING_CONV_IN_WXSTRING
operator const char*() const { return c_str(); }
operator const wchar_t*() const { return c_str(); }
#if wxUSE_UNSAFE_WXSTRING_CONV && !defined(wxNO_UNSAFE_WXSTRING_CONV)
operator const char*() const { return c_str(); }
// implicit conversion to untyped pointer for compatibility with previous
// wxWidgets versions: this is the same as conversion to const char * so it
// may fail!
operator const void*() const { return c_str(); }
#endif // wxUSE_UNSAFE_WXSTRING_CONV && !defined(wxNO_UNSAFE_WXSTRING_CONV)
#endif // !wxUSE_STD_STRING_CONV_IN_WXSTRING
// identical to c_str(), for MFC compatibility