From 1d7384530e1e60f30e207f2a4b293082ace99ac3 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 2 Jul 2021 07:51:04 -0700 Subject: [PATCH] Add missing presentation type checks for std::string (#2402) --- include/fmt/core.h | 4 ++-- include/fmt/format.h | 1 + test/format-test.cc | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index f0426093..22768ff8 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -2577,8 +2577,8 @@ FMT_CONSTEXPR auto check_cstring_type_spec(Char spec, ErrorHandler&& eh = {}) return false; } -template -FMT_CONSTEXPR void check_string_type_spec(Char spec, ErrorHandler&& eh) { +template +FMT_CONSTEXPR void check_string_type_spec(Char spec, ErrorHandler&& eh = {}) { if (spec != 0 && spec != 's') eh.on_error("invalid type specifier"); } diff --git a/include/fmt/format.h b/include/fmt/format.h index 7de8e98f..0024cf7c 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1569,6 +1569,7 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view> s, const basic_format_specs& specs, locale_ref) -> OutputIt { + check_string_type_spec(specs.type); return write(out, s, specs); } template diff --git a/test/format-test.cc b/test/format-test.cc index 892556f1..ec59a5fc 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1390,6 +1390,8 @@ TEST(format_test, format_pointer) { TEST(format_test, format_string) { EXPECT_EQ("test", fmt::format("{0}", std::string("test"))); + EXPECT_THROW(fmt::format(fmt::runtime("{:x}"), std::string("test")), + fmt::format_error); } TEST(format_test, format_string_view) {