From 144e1fbb7c272e3ec4f0ec580daeb8f104fbcb22 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 4 Jul 2014 06:56:19 -0700 Subject: [PATCH] Disallow formatting of wide strings when using a narrow string formatter. --- format.h | 9 +++++++-- test/CMakeLists.txt | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) 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';")