Fix formatting string_view in UTF-8 build

Don't use string_view data directly, as this doesn't respect its length
and would use the entire rest of the string this view is based on.

Instead, make a copy of just the part corresponding in the view to
ensure that it is NUL-terminated and also use a temporary buffer to hold
it to ensure that it lives long enough.
This commit is contained in:
Vadim Zeitlin 2023-03-27 21:26:24 +02:00
parent 2b5dbd1ec5
commit 92649ca2e6

View file

@ -824,11 +824,19 @@ struct wxArgNormalizerUtf8<const std::string&>
#ifdef __cpp_lib_string_view
template<>
struct wxArgNormalizerUtf8<const std::string_view&>
: public wxArgNormalizerUtf8<const char*>
{
wxArgNormalizerUtf8(const std::string_view& v,
const wxFormatString *fmt, unsigned index)
: wxArgNormalizerUtf8<const char*>(v.data(), fmt, index) {}
: m_str{v}
{
wxASSERT_ARG_TYPE( fmt, index, wxFormatString::Arg_String );
}
const char* get() const { return m_str.c_str(); }
// We need to store this string to ensure that we use a NUL-terminated
// buffer, i.e. we can't use string_view data directly.
const std::string m_str;
};
#endif // __cpp_lib_string_view