Skip to content

Commit

Permalink
Disallow formatting of multibyte strings into a wide buffer (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 10, 2018
1 parent ca93be1 commit daf650c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
14 changes: 8 additions & 6 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ template <typename Context, typename T>
FMT_CONSTEXPR basic_arg<Context> make_arg(const T &value);

#define FMT_MAKE_VALUE(TAG, ArgType, ValueType) \
template <typename C, typename Char = typename C::char_type> \
template <typename C> \
FMT_CONSTEXPR typed_value<C, TAG> make_value(ArgType val) { \
return static_cast<ValueType>(val); \
}
Expand Down Expand Up @@ -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<typename C::char_type>,
basic_string_view<Char>)
basic_string_view<typename C::char_type>)
FMT_MAKE_VALUE(string_type,
typename basic_string_view<typename C::char_type>::type,
basic_string_view<Char>)
basic_string_view<typename C::char_type>)
FMT_MAKE_VALUE(string_type, const std::basic_string<typename C::char_type>&,
basic_string_view<Char>)
basic_string_view<typename C::char_type>)
FMT_MAKE_VALUE(pointer_type, void*, const void*)
FMT_MAKE_VALUE(pointer_type, const void*, const void*)

Expand Down
16 changes: 3 additions & 13 deletions test/compile-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>)(s);")
expect_compile_error("const volatile char s[] = \"test\"; (fmt::internal::MakeArg<char>)(s);")

# MakeArg<char> doesn't accept wchar_t.
expect_compile_error("fmt::internal::MakeValue<char>(L'a');")
expect_compile_error("fmt::internal::MakeValue<char>(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)
Expand Down

0 comments on commit daf650c

Please sign in to comment.