From 92649ca2e6f982d80314eb8dd758941a6894910a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 27 Mar 2023 21:26:24 +0200 Subject: [PATCH] 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. --- include/wx/strvararg.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/wx/strvararg.h b/include/wx/strvararg.h index 9161e87d59..4193079847 100644 --- a/include/wx/strvararg.h +++ b/include/wx/strvararg.h @@ -824,11 +824,19 @@ struct wxArgNormalizerUtf8 #ifdef __cpp_lib_string_view template<> struct wxArgNormalizerUtf8 - : public wxArgNormalizerUtf8 { wxArgNormalizerUtf8(const std::string_view& v, const wxFormatString *fmt, unsigned index) - : wxArgNormalizerUtf8(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