diff --git a/build/cmake/options.cmake b/build/cmake/options.cmake index 2ecc5329dc..7d829eed7c 100644 --- a/build/cmake/options.cmake +++ b/build/cmake/options.cmake @@ -90,7 +90,6 @@ wx_dependent_option(wxUSE_UTF8_LOCALE_ONLY "only support UTF-8 locales in UTF-8 if(NOT WIN32) wx_option(wxUSE_VISIBILITY "use of ELF symbols visibility") endif() -wx_option(wxUSE_CHAR_CONV_IN_WXSTRING "provide implicit conversions to const wchar_t* and const char* in wxString" ON) wx_option(wxUSE_STD_STRING_CONV_IN_WXSTRING "provide implicit conversions to std::wstring and std::string in wxString" OFF) wx_option(wxUSE_UNSAFE_WXSTRING_CONV "provide unsafe implicit conversions in wxString to const char* or std::string") wx_option(wxUSE_REPRODUCIBLE_BUILD "enable reproducable build" OFF) diff --git a/build/cmake/setup.h.in b/build/cmake/setup.h.in index b51553a3c2..d42df8d88e 100644 --- a/build/cmake/setup.h.in +++ b/build/cmake/setup.h.in @@ -159,8 +159,6 @@ #cmakedefine01 wxUSE_UNSAFE_WXSTRING_CONV -#cmakedefine01 wxUSE_CHAR_CONV_IN_WXSTRING - #cmakedefine01 wxUSE_STD_STRING_CONV_IN_WXSTRING diff --git a/configure b/configure index e08aa6c007..ebf498978a 100755 --- a/configure +++ b/configure @@ -1122,7 +1122,6 @@ with_cxx enable_stl enable_std_containers enable_std_iostreams -enable_char_conv_in_wxstring enable_std_string_conv_in_wxstring enable_unsafe_conv_in_wxstring enable_utf8 @@ -2089,7 +2088,6 @@ Optional Features: --enable-stl obsolete option doing nothing --disable-std_containers disable use of standard C++ container classes --disable-std_iostreams disable use of standard C++ stream classes - --disable-char_conv_in_wxstring disable implicit conversion to const wchar_t/char* in wxString --enable-std_string_conv_in_wxstring provide implicit conversion to std::string in wxString --disable-unsafe_conv_in_wxstring disable unsafe implicit conversions in wxString --enable-utf8 use UTF-8 representation for strings @@ -5637,35 +5635,6 @@ fi eval "$wx_cv_use_std_iostreams" - enablestring=disable - defaultval= - if test -z "$defaultval"; then - if test x"$enablestring" = xdisable; then - defaultval=yes - else - defaultval=no - fi - fi - - # Check whether --enable-char_conv_in_wxstring was given. -if test "${enable_char_conv_in_wxstring+set}" = set; then : - enableval=$enable_char_conv_in_wxstring; - if test "$enableval" = yes; then - wx_cv_use_char_conv_in_wxstring='wxUSE_CHAR_CONV_IN_WXSTRING=yes' - else - wx_cv_use_char_conv_in_wxstring='wxUSE_CHAR_CONV_IN_WXSTRING=no' - fi - -else - - wx_cv_use_char_conv_in_wxstring='wxUSE_CHAR_CONV_IN_WXSTRING=${'DEFAULT_wxUSE_CHAR_CONV_IN_WXSTRING":-$defaultval}" - -fi - - - eval "$wx_cv_use_char_conv_in_wxstring" - - enablestring= defaultval= if test -z "$defaultval"; then @@ -37464,11 +37433,6 @@ if test "$wxUSE_STD_IOSTREAM" = "yes"; then fi -if test "$wxUSE_CHAR_CONV_IN_WXSTRING" = "yes"; then - $as_echo "#define wxUSE_CHAR_CONV_IN_WXSTRING 1" >>confdefs.h - -fi - if test "$wxUSE_STD_STRING_CONV_IN_WXSTRING" = "yes"; then $as_echo "#define wxUSE_STD_STRING_CONV_IN_WXSTRING 1" >>confdefs.h diff --git a/configure.in b/configure.in index c1244883aa..0148cfee9d 100644 --- a/configure.in +++ b/configure.in @@ -642,7 +642,6 @@ AC_ARG_WITH(cxx, [ --with-cxx=11|14|17|20 use the given C++ dialect] WX_ARG_ENABLE(stl, [ --enable-stl obsolete option doing nothing], wxUSE_STL) WX_ARG_DISABLE(std_containers,[ --disable-std_containers disable use of standard C++ container classes], wxUSE_STD_CONTAINERS) WX_ARG_DISABLE(std_iostreams,[ --disable-std_iostreams disable use of standard C++ stream classes], wxUSE_STD_IOSTREAM) -WX_ARG_DISABLE(char_conv_in_wxstring, [ --disable-char_conv_in_wxstring disable implicit conversion to const wchar_t/char* in wxString], wxUSE_CHAR_CONV_IN_WXSTRING) WX_ARG_ENABLE(std_string_conv_in_wxstring, [ --enable-std_string_conv_in_wxstring provide implicit conversion to std::string in wxString], wxUSE_STD_STRING_CONV_IN_WXSTRING) WX_ARG_DISABLE(unsafe_conv_in_wxstring, [ --disable-unsafe_conv_in_wxstring disable unsafe implicit conversions in wxString], wxUSE_UNSAFE_WXSTRING_CONV) WX_ARG_ENABLE_PARAM(utf8, [ --enable-utf8 use UTF-8 representation for strings], wxUSE_UNICODE_UTF8) @@ -5391,10 +5390,6 @@ if test "$wxUSE_STD_IOSTREAM" = "yes"; then AC_DEFINE(wxUSE_STD_IOSTREAM) fi -if test "$wxUSE_CHAR_CONV_IN_WXSTRING" = "yes"; then - AC_DEFINE(wxUSE_CHAR_CONV_IN_WXSTRING) -fi - if test "$wxUSE_STD_STRING_CONV_IN_WXSTRING" = "yes"; then AC_DEFINE(wxUSE_STD_STRING_CONV_IN_WXSTRING) fi diff --git a/docs/doxygen/mainpages/const_wxusedef.h b/docs/doxygen/mainpages/const_wxusedef.h index 0353ab916f..6e013cae7f 100644 --- a/docs/doxygen/mainpages/const_wxusedef.h +++ b/docs/doxygen/mainpages/const_wxusedef.h @@ -43,7 +43,7 @@ library: available.} @itemdef{wxUSE_UNSAFE_WXSTRING_CONV, Defined as 1 by default for compatibility, but can be set to 0 to globally disable unsafe implicit conversions in - wxString class. See @ref overview_string_settings for more details.} + wxString class. See @ref string_conv for more details.} @endDefList diff --git a/include/wx/android/setup.h b/include/wx/android/setup.h index 5fc3021542..5a8063ef31 100644 --- a/include/wx/android/setup.h +++ b/include/wx/android/setup.h @@ -284,7 +284,7 @@ // ---------------------------------------------------------------------------- // Provide unsafe implicit conversions in wxString to "const char*" or -// "std::string" (depending on wxUSE_STD_STRING_CONV_IN_WXSTRING value). +// "std::string" (only if implicit conversions are not disabled entirely). // // Default is 1 for compatibility reasons, it is recommended to set // this to 0 because converting wxString to a narrow (non-Unicode) string may @@ -297,16 +297,6 @@ // application code to effectively disallow using these conversions. #define wxUSE_UNSAFE_WXSTRING_CONV 1 -// Define implicit conversions of wxString to "const wchar_t*" and "const -// char*" if wxUSE_UNSAFE_WXSTRING_CONV is also enabled. -// -// Default is 1. -// -// Recommended setting: 1 to remain compatible with the official builds of -// wxWidgets but may be set to 0 to prevent any accidental conversions from -// happening. -#define wxUSE_CHAR_CONV_IN_WXSTRING 1 - // Define implicit conversions of wxString to std::wstring and std::string if // wxUSE_UNSAFE_WXSTRING_CONV is also enabled. // @@ -314,9 +304,9 @@ // this option is turned on or off, it only enables implicit conversion in the // other direction. // -// If this setting is changed to 1, wxUSE_CHAR_CONV_IN_WXSTRING must be set to -// 0 as setting both of them to 1 will result in ambiguities due to having too -// many implicit conversions defined. +// If this setting is changed to 1, implicit conversions to pointer types are +// disabled as defining both kinds of implicit conversions would result in +// ambiguities. // // Default is 0. // diff --git a/include/wx/gtk/setup.h b/include/wx/gtk/setup.h index 0081d674fe..7e7a69ec46 100644 --- a/include/wx/gtk/setup.h +++ b/include/wx/gtk/setup.h @@ -285,7 +285,7 @@ // ---------------------------------------------------------------------------- // Provide unsafe implicit conversions in wxString to "const char*" or -// "std::string" (depending on wxUSE_STD_STRING_CONV_IN_WXSTRING value). +// "std::string" (only if implicit conversions are not disabled entirely). // // Default is 1 for compatibility reasons, it is recommended to set // this to 0 because converting wxString to a narrow (non-Unicode) string may @@ -298,16 +298,6 @@ // application code to effectively disallow using these conversions. #define wxUSE_UNSAFE_WXSTRING_CONV 1 -// Define implicit conversions of wxString to "const wchar_t*" and "const -// char*" if wxUSE_UNSAFE_WXSTRING_CONV is also enabled. -// -// Default is 1. -// -// Recommended setting: 1 to remain compatible with the official builds of -// wxWidgets but may be set to 0 to prevent any accidental conversions from -// happening. -#define wxUSE_CHAR_CONV_IN_WXSTRING 1 - // Define implicit conversions of wxString to std::wstring and std::string if // wxUSE_UNSAFE_WXSTRING_CONV is also enabled. // @@ -315,9 +305,9 @@ // this option is turned on or off, it only enables implicit conversion in the // other direction. // -// If this setting is changed to 1, wxUSE_CHAR_CONV_IN_WXSTRING must be set to -// 0 as setting both of them to 1 will result in ambiguities due to having too -// many implicit conversions defined. +// If this setting is changed to 1, implicit conversions to pointer types are +// disabled as defining both kinds of implicit conversions would result in +// ambiguities. // // Default is 0. // diff --git a/include/wx/msw/setup.h b/include/wx/msw/setup.h index 4757daa268..43e84a82a3 100644 --- a/include/wx/msw/setup.h +++ b/include/wx/msw/setup.h @@ -285,7 +285,7 @@ // ---------------------------------------------------------------------------- // Provide unsafe implicit conversions in wxString to "const char*" or -// "std::string" (depending on wxUSE_STD_STRING_CONV_IN_WXSTRING value). +// "std::string" (only if implicit conversions are not disabled entirely). // // Default is 1 for compatibility reasons, it is recommended to set // this to 0 because converting wxString to a narrow (non-Unicode) string may @@ -298,16 +298,6 @@ // application code to effectively disallow using these conversions. #define wxUSE_UNSAFE_WXSTRING_CONV 1 -// Define implicit conversions of wxString to "const wchar_t*" and "const -// char*" if wxUSE_UNSAFE_WXSTRING_CONV is also enabled. -// -// Default is 1. -// -// Recommended setting: 1 to remain compatible with the official builds of -// wxWidgets but may be set to 0 to prevent any accidental conversions from -// happening. -#define wxUSE_CHAR_CONV_IN_WXSTRING 1 - // Define implicit conversions of wxString to std::wstring and std::string if // wxUSE_UNSAFE_WXSTRING_CONV is also enabled. // @@ -315,9 +305,9 @@ // this option is turned on or off, it only enables implicit conversion in the // other direction. // -// If this setting is changed to 1, wxUSE_CHAR_CONV_IN_WXSTRING must be set to -// 0 as setting both of them to 1 will result in ambiguities due to having too -// many implicit conversions defined. +// If this setting is changed to 1, implicit conversions to pointer types are +// disabled as defining both kinds of implicit conversions would result in +// ambiguities. // // Default is 0. // diff --git a/include/wx/osx/setup.h b/include/wx/osx/setup.h index 7f29b11014..678b8ea182 100644 --- a/include/wx/osx/setup.h +++ b/include/wx/osx/setup.h @@ -291,7 +291,7 @@ // ---------------------------------------------------------------------------- // Provide unsafe implicit conversions in wxString to "const char*" or -// "std::string" (depending on wxUSE_STD_STRING_CONV_IN_WXSTRING value). +// "std::string" (only if implicit conversions are not disabled entirely). // // Default is 1 for compatibility reasons, it is recommended to set // this to 0 because converting wxString to a narrow (non-Unicode) string may @@ -304,16 +304,6 @@ // application code to effectively disallow using these conversions. #define wxUSE_UNSAFE_WXSTRING_CONV 1 -// Define implicit conversions of wxString to "const wchar_t*" and "const -// char*" if wxUSE_UNSAFE_WXSTRING_CONV is also enabled. -// -// Default is 1. -// -// Recommended setting: 1 to remain compatible with the official builds of -// wxWidgets but may be set to 0 to prevent any accidental conversions from -// happening. -#define wxUSE_CHAR_CONV_IN_WXSTRING 1 - // Define implicit conversions of wxString to std::wstring and std::string if // wxUSE_UNSAFE_WXSTRING_CONV is also enabled. // @@ -321,9 +311,9 @@ // this option is turned on or off, it only enables implicit conversion in the // other direction. // -// If this setting is changed to 1, wxUSE_CHAR_CONV_IN_WXSTRING must be set to -// 0 as setting both of them to 1 will result in ambiguities due to having too -// many implicit conversions defined. +// If this setting is changed to 1, implicit conversions to pointer types are +// disabled as defining both kinds of implicit conversions would result in +// ambiguities. // // Default is 0. // diff --git a/include/wx/setup_inc.h b/include/wx/setup_inc.h index a23aa76259..2562857e09 100644 --- a/include/wx/setup_inc.h +++ b/include/wx/setup_inc.h @@ -281,7 +281,7 @@ // ---------------------------------------------------------------------------- // Provide unsafe implicit conversions in wxString to "const char*" or -// "std::string" (depending on wxUSE_STD_STRING_CONV_IN_WXSTRING value). +// "std::string" (only if implicit conversions are not disabled entirely). // // Default is 1 for compatibility reasons, it is recommended to set // this to 0 because converting wxString to a narrow (non-Unicode) string may @@ -294,16 +294,6 @@ // application code to effectively disallow using these conversions. #define wxUSE_UNSAFE_WXSTRING_CONV 1 -// Define implicit conversions of wxString to "const wchar_t*" and "const -// char*" if wxUSE_UNSAFE_WXSTRING_CONV is also enabled. -// -// Default is 1. -// -// Recommended setting: 1 to remain compatible with the official builds of -// wxWidgets but may be set to 0 to prevent any accidental conversions from -// happening. -#define wxUSE_CHAR_CONV_IN_WXSTRING 1 - // Define implicit conversions of wxString to std::wstring and std::string if // wxUSE_UNSAFE_WXSTRING_CONV is also enabled. // @@ -311,9 +301,9 @@ // this option is turned on or off, it only enables implicit conversion in the // other direction. // -// If this setting is changed to 1, wxUSE_CHAR_CONV_IN_WXSTRING must be set to -// 0 as setting both of them to 1 will result in ambiguities due to having too -// many implicit conversions defined. +// If this setting is changed to 1, implicit conversions to pointer types are +// disabled as defining both kinds of implicit conversions would result in +// ambiguities. // // Default is 0. // diff --git a/include/wx/string.h b/include/wx/string.h index 0a4d106c0d..3f04845535 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -98,6 +98,14 @@ class WXDLLIMPEXP_FWD_BASE wxString; #endif #endif // wxUSE_UNSAFE_WXSTRING_CONV +// enabling implicit conversions to std::[w]string is incompatible with having +// implicit conversions to char*/wchar_t*. +#if wxUSE_STD_STRING_CONV_IN_WXSTRING + #ifndef wxNO_IMPLICIT_WXSTRING_CONV_TO_PTR + #define wxNO_IMPLICIT_WXSTRING_CONV_TO_PTR + #endif +#endif // wxUSE_STD_STRING_CONV_IN_WXSTRING + namespace wxPrivate { template struct wxStringAsBufHelper; @@ -1540,7 +1548,7 @@ public: // implicit conversion to wxCStrData operator wxCStrData() const { return c_str(); } -#if wxUSE_CHAR_CONV_IN_WXSTRING +#if !defined(wxNO_IMPLICIT_WXSTRING_CONV_TO_PTR) operator const wchar_t*() const { return c_str(); } #if !defined(wxNO_UNSAFE_WXSTRING_CONV) @@ -1551,7 +1559,7 @@ public: operator const void*() const { return c_str(); } #endif // !defined(wxNO_UNSAFE_WXSTRING_CONV) -#endif // wxUSE_CHAR_CONV_IN_WXSTRING +#endif // !defined(wxNO_IMPLICIT_WXSTRING_CONV_TO_PTR) // identical to c_str(), for MFC compatibility const wxCStrData GetData() const { return c_str(); } diff --git a/include/wx/univ/setup.h b/include/wx/univ/setup.h index 1b4b5b9212..cdbcb73daf 100644 --- a/include/wx/univ/setup.h +++ b/include/wx/univ/setup.h @@ -284,7 +284,7 @@ // ---------------------------------------------------------------------------- // Provide unsafe implicit conversions in wxString to "const char*" or -// "std::string" (depending on wxUSE_STD_STRING_CONV_IN_WXSTRING value). +// "std::string" (only if implicit conversions are not disabled entirely). // // Default is 1 for compatibility reasons, it is recommended to set // this to 0 because converting wxString to a narrow (non-Unicode) string may @@ -297,16 +297,6 @@ // application code to effectively disallow using these conversions. #define wxUSE_UNSAFE_WXSTRING_CONV 1 -// Define implicit conversions of wxString to "const wchar_t*" and "const -// char*" if wxUSE_UNSAFE_WXSTRING_CONV is also enabled. -// -// Default is 1. -// -// Recommended setting: 1 to remain compatible with the official builds of -// wxWidgets but may be set to 0 to prevent any accidental conversions from -// happening. -#define wxUSE_CHAR_CONV_IN_WXSTRING 1 - // Define implicit conversions of wxString to std::wstring and std::string if // wxUSE_UNSAFE_WXSTRING_CONV is also enabled. // @@ -314,9 +304,9 @@ // this option is turned on or off, it only enables implicit conversion in the // other direction. // -// If this setting is changed to 1, wxUSE_CHAR_CONV_IN_WXSTRING must be set to -// 0 as setting both of them to 1 will result in ambiguities due to having too -// many implicit conversions defined. +// If this setting is changed to 1, implicit conversions to pointer types are +// disabled as defining both kinds of implicit conversions would result in +// ambiguities. // // Default is 0. // diff --git a/interface/wx/string.h b/interface/wx/string.h index 6a9c82716b..8c2a424a1f 100644 --- a/interface/wx/string.h +++ b/interface/wx/string.h @@ -139,17 +139,20 @@ becomes unavailable -- but explicit conversions using c_str() and mb_str() still work. - Finally, please note that implicit conversion to both `const char*` and - `const wchar_t*` may be entirely disabled by setting the build option - `wxUSE_CHAR_CONV_IN_WXSTRING` to 0. Unlike with `wxNO_XXX` constants, this - option requires rebuilding the library after changing its value. + Finally, please note that implicit conversion to both `const char*` (which + is unsafe for the reasons explained above) and to `const wchar_t*` (which + is safe from this point of view, but may still be considered dangerous, as + any implicit conversion) may be entirely disabled by defining + `wxNO_IMPLICIT_WXSTRING_CONV_TO_PTR` when building the application. To summarize, the safest way to use wxString is to always define `wxNO_IMPLICIT_WXSTRING_ENCODING` in the application compilation options to disable all implicit uses of encoding and specify it explicitly, typically by using utf8_str() or utf8_string() and FromUTF8() for conversions, for - every operation. + every operation. If this is impossible, for example because it would + require too many changes to the existing code, consider defining + `wxNO_UNSAFE_WXSTRING_CONV` to at least disable implicit unsafe conversions. @section string_gotchas Traps for the unwary diff --git a/setup.h.in b/setup.h.in index 64b88ad6d7..3aea003f43 100644 --- a/setup.h.in +++ b/setup.h.in @@ -162,8 +162,6 @@ #define wxUSE_UNSAFE_WXSTRING_CONV 0 -#define wxUSE_CHAR_CONV_IN_WXSTRING 0 - #define wxUSE_STD_STRING_CONV_IN_WXSTRING 0 diff --git a/setup.h_vms b/setup.h_vms index c3c7ef128b..3f9697626d 100644 --- a/setup.h_vms +++ b/setup.h_vms @@ -195,8 +195,6 @@ typedef pid_t GPid; #define wxUSE_STD_CONTAINERS 0 -#define wxUSE_CHAR_CONV_IN_WXSTRING 1 - #define wxUSE_STD_STRING_CONV_IN_WXSTRING 0 #define wxUSE_LONGLONG 1 diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index ed04009097..33100eae4d 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -1018,7 +1018,7 @@ TEST_CASE("StringCStrDataImplicitConversion", "[wxString]") CHECK( CheckStrConstWChar(s, s.c_str()) ); CHECK( CheckStrConstChar(s, s.c_str()) ); -#if wxUSE_CHAR_CONV_IN_WXSTRING +#ifndef wxNO_IMPLICIT_WXSTRING_CONV_TO_PTR CHECK( CheckStrConstWChar(s, s) ); #ifndef wxNO_UNSAFE_WXSTRING_CONV CHECK( CheckStrConstChar(s, s) );