Add wxNO_UNSAFE_WXSTRING_CONV2 macro

The macro disallows implicit conversions between wxString and const
char*
This commit is contained in:
Arrigo Marchiori
2019-10-21 11:05:58 +02:00
committed by Vadim Zeitlin
parent 15a4375f93
commit 65cbf40b7e
16 changed files with 502 additions and 33 deletions

View File

@@ -133,16 +133,20 @@ class WXDLLIMPEXP_FWD_BASE wxString;
class WXDLLIMPEXP_BASE wxFormatString
{
public:
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
wxFormatString(const char *str)
: m_char(wxScopedCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
#endif
wxFormatString(const wchar_t *str)
: m_wchar(wxScopedWCharBuffer::CreateNonOwned(str)), m_str(NULL), m_cstr(NULL) {}
wxFormatString(const wxString& str)
: m_str(&str), m_cstr(NULL) {}
wxFormatString(const wxCStrData& str)
: m_str(NULL), m_cstr(&str) {}
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
wxFormatString(const wxScopedCharBuffer& str)
: m_char(str), m_str(NULL), m_cstr(NULL) {}
#endif
wxFormatString(const wxScopedWCharBuffer& str)
: m_wchar(str), m_str(NULL), m_cstr(NULL) {}
@@ -201,7 +205,7 @@ public:
// to other InputAsXXX() methods
wxString InputAsString() const;
#if !wxUSE_UNICODE_WCHAR
#if !wxUSE_UNICODE_WCHAR && !defined wxNO_IMPLICIT_WXSTRING_ENCODING
operator const char*() const
{ return const_cast<wxFormatString*>(this)->AsChar(); }
private:
@@ -212,7 +216,7 @@ private:
const char* InputAsChar();
const char* AsChar();
wxScopedCharBuffer m_convertedChar;
#endif // !wxUSE_UNICODE_WCHAR
#endif // !wxUSE_UNICODE_WCHAR && !defined wx_NO_IMPLICIT_WXSTRING_ENCODING
#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
public:
@@ -284,7 +288,12 @@ struct wxFormatStringArgumentFinder<wxString>
template<>
struct wxFormatStringArgumentFinder<wxScopedCharBuffer>
: public wxFormatStringArgumentFinder<const wxScopedCharBuffer&> {};
: public wxFormatStringArgumentFinder<const wxScopedCharBuffer&> {
#ifdef wxNO_IMPLICIT_WXSTRING_ENCODING
private:
wxFormatStringArgumentFinder<wxScopedCharBuffer>(); // Disabled
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING
};
template<>
struct wxFormatStringArgumentFinder<wxScopedWCharBuffer>
@@ -292,7 +301,12 @@ struct wxFormatStringArgumentFinder<wxScopedWCharBuffer>
template<>
struct wxFormatStringArgumentFinder<wxCharBuffer>
: public wxFormatStringArgumentFinder<const wxCharBuffer&> {};
: public wxFormatStringArgumentFinder<const wxCharBuffer&> {
#ifdef wxNO_IMPLICIT_WXSTRING_ENCODING
private:
wxFormatStringArgumentFinder<wxCharBuffer>(); // Disabled
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING
};
template<>
struct wxFormatStringArgumentFinder<wxWCharBuffer>
@@ -387,6 +401,13 @@ struct wxFormatStringSpecifier<const T*>
enum { value = arg }; \
};
#define wxDISABLED_FORMAT_STRING_SPECIFIER(T) \
template<> struct wxFormatStringSpecifier<T> \
{ \
private: \
wxFormatStringSpecifier<T>(); /* Disabled */ \
};
wxFORMAT_STRING_SPECIFIER(bool, wxFormatString::Arg_Int)
wxFORMAT_STRING_SPECIFIER(int, wxFormatString::Arg_Int)
wxFORMAT_STRING_SPECIFIER(unsigned int, wxFormatString::Arg_Int)
@@ -406,18 +427,27 @@ wxFORMAT_STRING_SPECIFIER(long double, wxFormatString::Arg_LongDouble)
wxFORMAT_STRING_SPECIFIER(wchar_t, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
#endif
#if !wxUSE_UNICODE
#if !wxUSE_UNICODE && !defined wxNO_IMPLICIT_WXSTRING_ENCODING
wxFORMAT_STRING_SPECIFIER(char, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
wxFORMAT_STRING_SPECIFIER(signed char, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
wxFORMAT_STRING_SPECIFIER(unsigned char, wxFormatString::Arg_Char | wxFormatString::Arg_Int)
#endif
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
wxFORMAT_STRING_SPECIFIER(char*, wxFormatString::Arg_String)
wxFORMAT_STRING_SPECIFIER(unsigned char*, wxFormatString::Arg_String)
wxFORMAT_STRING_SPECIFIER(signed char*, wxFormatString::Arg_String)
wxFORMAT_STRING_SPECIFIER(const char*, wxFormatString::Arg_String)
wxFORMAT_STRING_SPECIFIER(const unsigned char*, wxFormatString::Arg_String)
wxFORMAT_STRING_SPECIFIER(const signed char*, wxFormatString::Arg_String)
#else // wxNO_IMPLICIT_WXSTRING_ENCODING
wxDISABLED_FORMAT_STRING_SPECIFIER(char*)
wxDISABLED_FORMAT_STRING_SPECIFIER(unsigned char*)
wxDISABLED_FORMAT_STRING_SPECIFIER(signed char*)
wxDISABLED_FORMAT_STRING_SPECIFIER(const char*)
wxDISABLED_FORMAT_STRING_SPECIFIER(const unsigned char*)
wxDISABLED_FORMAT_STRING_SPECIFIER(const signed char*)
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING
wxFORMAT_STRING_SPECIFIER(wchar_t*, wxFormatString::Arg_String)
wxFORMAT_STRING_SPECIFIER(const wchar_t*, wxFormatString::Arg_String)
@@ -430,6 +460,7 @@ wxFORMAT_STRING_SPECIFIER(std::nullptr_t, wxFormatString::Arg_Pointer)
#endif
#undef wxFORMAT_STRING_SPECIFIER
#undef wxDISABLED_FORMAT_STRING_SPECIFIER
// Converts an argument passed to wxPrint etc. into standard form expected,
@@ -571,6 +602,7 @@ struct WXDLLIMPEXP_BASE wxArgNormalizerWchar<const wxCStrData&>
// char* for wchar_t Unicode build or UTF8):
#if wxUSE_UNICODE_WCHAR
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
template<>
struct wxArgNormalizerWchar<const char*>
: public wxArgNormalizerWithBuffer<wchar_t>
@@ -579,6 +611,7 @@ struct wxArgNormalizerWchar<const char*>
const wxFormatString *fmt, unsigned index)
: wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s), fmt, index) {}
};
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING
#elif wxUSE_UNICODE_UTF8
@@ -591,6 +624,7 @@ struct wxArgNormalizerUtf8<const wchar_t*>
: wxArgNormalizerWithBuffer<char>(wxConvUTF8.cWC2MB(s), fmt, index) {}
};
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
template<>
struct wxArgNormalizerUtf8<const char*>
: public wxArgNormalizerWithBuffer<char>
@@ -616,9 +650,10 @@ struct wxArgNormalizerUtf8<const char*>
}
}
};
#endif
// UTF-8 build needs conversion to wchar_t* too:
#if !wxUSE_UTF8_LOCALE_ONLY
#if !wxUSE_UTF8_LOCALE_ONLY && !defined wxNO_IMPLICIT_WXSTRING_ENCODING
template<>
struct wxArgNormalizerWchar<const char*>
: public wxArgNormalizerWithBuffer<wchar_t>
@@ -627,10 +662,11 @@ struct wxArgNormalizerWchar<const char*>
const wxFormatString *fmt, unsigned index)
: wxArgNormalizerWithBuffer<wchar_t>(wxConvLibc.cMB2WC(s), fmt, index) {}
};
#endif // !wxUSE_UTF8_LOCALE_ONLY
#endif // !wxUSE_UTF8_LOCALE_ONLY && !defined wxNO_IMPLICIT_WXSTRING_ENCODING
#else // ANSI - FIXME-UTF8
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
template<>
struct wxArgNormalizerWchar<const wchar_t*>
: public wxArgNormalizerWithBuffer<char>
@@ -639,10 +675,56 @@ struct wxArgNormalizerWchar<const wchar_t*>
const wxFormatString *fmt, unsigned index)
: wxArgNormalizerWithBuffer<char>(wxConvLibc.cWC2MB(s), fmt, index) {}
};
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING
#endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8/ANSI
#ifdef wxNO_IMPLICIT_WXSTRING_ENCODING
// wxArgNormalizer specializations that cannot be instanced
template<>
struct wxArgNormalizer<const char*> {
private:
wxArgNormalizer<const char*>(const char*, const wxFormatString *,
unsigned);
const char *get() const;
};
template<>
struct wxArgNormalizer<char*> {
private:
wxArgNormalizer<char*>(const char*, const wxFormatString *, unsigned);
char *get() const;
};
template<>
struct wxArgNormalizer<const std::string> {
private:
wxArgNormalizer<const std::string>(const std::string&,
const wxFormatString *, unsigned);
std::string get() const;
};
template<>
struct wxArgNormalizer<std::string> {
private:
wxArgNormalizer<std::string>(std::string&,
const wxFormatString *, unsigned);
std::string get() const;
};
template<>
struct wxArgNormalizer<wxCharBuffer> {
private:
wxArgNormalizer<wxCharBuffer>(wxCharBuffer&,
const wxFormatString *, unsigned);
std::string get() const;
};
template<>
struct wxArgNormalizer<wxScopedCharBuffer> {
private:
wxArgNormalizer<wxScopedCharBuffer>(wxScopedCharBuffer&,
const wxFormatString *, unsigned);
std::string get() const;
};
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING
// this macro is used to implement specialization that are exactly same as
// some other specialization, i.e. to "forward" the implementation (e.g. for
// T=wxString and T=const wxString&). Note that the ctor takes BaseT argument,
@@ -675,16 +757,22 @@ WX_ARG_NORMALIZER_FORWARD(wxString, const wxString&);
WX_ARG_NORMALIZER_FORWARD(wxCStrData, const wxCStrData&);
// versions for passing non-const pointers:
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
WX_ARG_NORMALIZER_FORWARD(char*, const char*);
#endif
WX_ARG_NORMALIZER_FORWARD(wchar_t*, const wchar_t*);
// versions for passing wx[W]CharBuffer:
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
WX_ARG_NORMALIZER_FORWARD(wxScopedCharBuffer, const char*);
WX_ARG_NORMALIZER_FORWARD(const wxScopedCharBuffer&, const char*);
#endif
WX_ARG_NORMALIZER_FORWARD(wxScopedWCharBuffer, const wchar_t*);
WX_ARG_NORMALIZER_FORWARD(const wxScopedWCharBuffer&, const wchar_t*);
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
WX_ARG_NORMALIZER_FORWARD(wxCharBuffer, const char*);
WX_ARG_NORMALIZER_FORWARD(const wxCharBuffer&, const char*);
#endif
WX_ARG_NORMALIZER_FORWARD(wxWCharBuffer, const wchar_t*);
WX_ARG_NORMALIZER_FORWARD(const wxWCharBuffer&, const wchar_t*);
@@ -694,6 +782,7 @@ WX_ARG_NORMALIZER_FORWARD(const wxWCharBuffer&, const wchar_t*);
#include "wx/stringimpl.h"
#if !wxUSE_UTF8_LOCALE_ONLY
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
template<>
struct wxArgNormalizerWchar<const std::string&>
: public wxArgNormalizerWchar<const char*>
@@ -702,6 +791,7 @@ struct wxArgNormalizerWchar<const std::string&>
const wxFormatString *fmt, unsigned index)
: wxArgNormalizerWchar<const char*>(s.c_str(), fmt, index) {}
};
#endif // NO_IMPLICIT_WXSTRING_ENCODING
template<>
struct wxArgNormalizerWchar<const wxStdWideString&>
@@ -714,6 +804,7 @@ struct wxArgNormalizerWchar<const wxStdWideString&>
#endif // !wxUSE_UTF8_LOCALE_ONLY
#if wxUSE_UNICODE_UTF8
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
template<>
struct wxArgNormalizerUtf8<const std::string&>
: public wxArgNormalizerUtf8<const char*>
@@ -722,6 +813,7 @@ struct wxArgNormalizerUtf8<const std::string&>
const wxFormatString *fmt, unsigned index)
: wxArgNormalizerUtf8<const char*>(s.c_str(), fmt, index) {}
};
#endif // wxNO_IMPLICIT_WXSTRING_ENCODING
template<>
struct wxArgNormalizerUtf8<const wxStdWideString&>
@@ -733,7 +825,9 @@ struct wxArgNormalizerUtf8<const wxStdWideString&>
};
#endif // wxUSE_UNICODE_UTF8
#ifndef wxNO_IMPLICIT_WXSTRING_ENCODING
WX_ARG_NORMALIZER_FORWARD(std::string, const std::string&);
#endif
WX_ARG_NORMALIZER_FORWARD(wxStdWideString, const wxStdWideString&);
#endif // wxUSE_STD_STRING