Disallow formatting of wide strings when using a narrow string formatter.

This commit is contained in:
Victor Zverovich 2014-07-04 06:56:19 -07:00
parent 270ed1cb92
commit 144e1fbb7c
2 changed files with 8 additions and 2 deletions

View File

@ -361,7 +361,10 @@ class CharTraits<char> : public BasicCharTraits<char> {
// Conversion from wchar_t to char is not allowed. // Conversion from wchar_t to char is not allowed.
static char ConvertChar(wchar_t); 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; typedef const wchar_t *UnsupportedStrType;
static char ConvertChar(char value) { return value; } static char ConvertChar(char value) { return value; }
@ -386,6 +389,8 @@ class CharTraits<wchar_t> : public BasicCharTraits<wchar_t> {
static StringValue<wchar_t> convert(StringValue<wchar_t> s) { return s; } static StringValue<wchar_t> convert(StringValue<wchar_t> s) { return s; }
static const wchar_t *check(const wchar_t *s) { return s; }
template <typename T> template <typename T>
static int FormatFloat(wchar_t *buffer, std::size_t size, static int FormatFloat(wchar_t *buffer, std::size_t size,
const wchar_t *format, unsigned width, int precision, T value); const wchar_t *format, unsigned width, int precision, T value);
@ -636,7 +641,7 @@ class MakeArg : public Arg {
void SetString(WStringRef str) { void SetString(WStringRef str) {
type = WSTRING; type = WSTRING;
wstring.value = str.c_str(); wstring.value = CharTraits<Char>::check(str.c_str());
wstring.size = str.size(); wstring.size = str.size();
} }

View File

@ -35,6 +35,7 @@ expect_compile_error("const volatile char s[] = \"test\"; (fmt::internal::MakeAr
# MakeArg<char> doesn't accept wchar_t. # MakeArg<char> doesn't accept wchar_t.
expect_compile_error("fmt::internal::MakeArg<char>(L'a');") expect_compile_error("fmt::internal::MakeArg<char>(L'a');")
expect_compile_error("fmt::internal::MakeArg<char>(L\"test\");")
# Writing a wide character to a character stream Writer is forbidden. # Writing a wide character to a character stream Writer is forbidden.
expect_compile_error("fmt::Writer() << L'a';") expect_compile_error("fmt::Writer() << L'a';")