diff --git a/format.h b/format.h index 5e42646e..31c52bde 100644 --- a/format.h +++ b/format.h @@ -361,7 +361,10 @@ class CharTraits : public BasicCharTraits { // Conversion from wchar_t to char is not allowed. static char ConvertChar(wchar_t); - public: + // Conversion from const wchar_t * to const char * is not allowed. + static const wchar_t *check(const wchar_t *s); + +public: typedef const wchar_t *UnsupportedStrType; static char ConvertChar(char value) { return value; } @@ -386,6 +389,8 @@ class CharTraits : public BasicCharTraits { static StringValue convert(StringValue s) { return s; } + static const wchar_t *check(const wchar_t *s) { return s; } + template static int FormatFloat(wchar_t *buffer, std::size_t size, const wchar_t *format, unsigned width, int precision, T value); @@ -636,7 +641,7 @@ class MakeArg : public Arg { void SetString(WStringRef str) { type = WSTRING; - wstring.value = str.c_str(); + wstring.value = CharTraits::check(str.c_str()); wstring.size = str.size(); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f3b78e40..5c80bd26 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -35,6 +35,7 @@ expect_compile_error("const volatile char s[] = \"test\"; (fmt::internal::MakeAr # MakeArg doesn't accept wchar_t. expect_compile_error("fmt::internal::MakeArg(L'a');") +expect_compile_error("fmt::internal::MakeArg(L\"test\");") # Writing a wide character to a character stream Writer is forbidden. expect_compile_error("fmt::Writer() << L'a';")