From e8e006f4e7702f192ebab1214439843c673a19c4 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 30 Mar 2018 09:29:47 -1000 Subject: [PATCH] Fix compile checks for mixing narrow and wide strings (#690) --- include/fmt/core.h | 4 ++-- test/compile-test/CMakeLists.txt | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 0d5b9c2c..f7a5da68 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -619,8 +619,8 @@ FMT_MAKE_VALUE(pointer_type, std::nullptr_t, const void*) // pointer cast it to "void *" or "const void *". In particular, this forbids // formatting of "[const] volatile char *" which is printed as bool by // iostreams. -template -void make_value(const T *) { +template +typed_value make_value(const T *) { static_assert(!sizeof(T), "formatting of non-void pointers is disallowed"); } diff --git a/test/compile-test/CMakeLists.txt b/test/compile-test/CMakeLists.txt index ede0c3ce..38038ede 100644 --- a/test/compile-test/CMakeLists.txt +++ b/test/compile-test/CMakeLists.txt @@ -10,6 +10,7 @@ function (generate_source result fragment) set(${result} " #define FMT_HEADER_ONLY 1 #include \"fmt/posix.h\" + #include \"fmt/ostream.h\" int main() { ${fragment} } @@ -53,6 +54,10 @@ 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\");") +# Formatting a narrow string with a wide format string is forbidden because +# mixing UTF-8 with UTF-16/32 can result in an invalid output. +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)