From daf650c49a61339e540c21d9ea1de78a426f39a8 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 10 Mar 2018 06:46:41 -0800 Subject: [PATCH] Disallow formatting of multibyte strings into a wide buffer (#606) --- include/fmt/core.h | 14 ++++++++------ test/compile-test/CMakeLists.txt | 16 +++------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index ea3bf969e8d3..2e8335ed6467 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -546,7 +546,7 @@ template FMT_CONSTEXPR basic_arg make_arg(const T &value); #define FMT_MAKE_VALUE(TAG, ArgType, ValueType) \ - template \ + template \ FMT_CONSTEXPR typed_value make_value(ArgType val) { \ return static_cast(val); \ } @@ -589,20 +589,22 @@ FMT_MAKE_VALUE(long_double_type, long double, long double) // Formatting of wide strings into a narrow buffer and multibyte strings // into a wide buffer is disallowed (https://github.com/fmtlib/fmt/pull/606). -FMT_MAKE_VALUE(cstring_type, Char*, const Char*) -FMT_MAKE_VALUE(cstring_type, const Char*, const Char*) +FMT_MAKE_VALUE(cstring_type, typename C::char_type*, + const typename C::char_type*) +FMT_MAKE_VALUE(cstring_type, const typename C::char_type*, + const typename C::char_type*) FMT_MAKE_VALUE(cstring_type, signed char*, const signed char*) FMT_MAKE_VALUE(cstring_type, const signed char*, const signed char*) FMT_MAKE_VALUE(cstring_type, unsigned char*, const unsigned char*) FMT_MAKE_VALUE(cstring_type, const unsigned char*, const unsigned char*) FMT_MAKE_VALUE(string_type, basic_string_view, - basic_string_view) + basic_string_view) FMT_MAKE_VALUE(string_type, typename basic_string_view::type, - basic_string_view) + basic_string_view) FMT_MAKE_VALUE(string_type, const std::basic_string&, - basic_string_view) + basic_string_view) FMT_MAKE_VALUE(pointer_type, void*, const void*) FMT_MAKE_VALUE(pointer_type, const void*, const void*) diff --git a/test/compile-test/CMakeLists.txt b/test/compile-test/CMakeLists.txt index ded517ad25d5..cebcee015825 100644 --- a/test/compile-test/CMakeLists.txt +++ b/test/compile-test/CMakeLists.txt @@ -47,22 +47,12 @@ endfunction () # check if the source file skeleton compiles expect_compile("") -# MakeArg doesn't accept [const] volatile char *. -expect_compile_error("volatile char s[] = \"test\"; (fmt::internal::MakeArg)(s);") -expect_compile_error("const volatile char s[] = \"test\"; (fmt::internal::MakeArg)(s);") - -# MakeArg doesn't accept wchar_t. -expect_compile_error("fmt::internal::MakeValue(L'a');") -expect_compile_error("fmt::internal::MakeValue(L\"test\");") - -# Writing a wide character to a character stream Writer is forbidden. -expect_compile_error("fmt::MemoryWriter() << L'a';") -expect_compile_error("fmt::MemoryWriter() << fmt::pad(\"abc\", 5, L' ');") -expect_compile_error("fmt::MemoryWriter() << fmt::pad(42, 5, L' ');") - # Formatting a wide character with a narrow format string is forbidden. expect_compile_error("fmt::format(\"{}\", L'a';") +# Formatting a wide string with a narrow format string is forbidden. +expect_compile_error("fmt::format(\"{}\", L\"foo\";") + # Make sure that compiler features detected in the header # match the features detected in CMake. if (SUPPORTS_USER_DEFINED_LITERALS)