From a23d592472b375a92df9c6040ccbe4b9065b5d6d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 3 Nov 2018 16:20:45 -0700 Subject: [PATCH] Fix check_format_string (#925) --- include/fmt/format.h | 6 +++--- test/format-test.cc | 17 +++++++++-------- test/printf-test.cc | 9 +++++++++ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index e95466af..ccb26454 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2243,7 +2243,7 @@ class format_string_checker { }; template -FMT_CONSTEXPR bool check_format_string( +FMT_CONSTEXPR bool do_check_format_string( basic_string_view s, ErrorHandler eh = ErrorHandler()) { format_string_checker checker(s, eh); parse_format_string(s, checker); @@ -2254,7 +2254,7 @@ template typename std::enable_if::value>::type check_format_string(S format_str) { typedef typename S::char_type char_t; - FMT_CONSTEXPR_DECL bool invalid_format = internal::check_format_string< + FMT_CONSTEXPR_DECL bool invalid_format = internal::do_check_format_string< char_t, internal::error_handler, Args...>(to_string_view(format_str)); (void)invalid_format; } @@ -3583,7 +3583,7 @@ class udl_formatter { std::basic_string operator()(const Args &... args) const { FMT_CONSTEXPR_DECL Char s[] = {CHARS..., '\0'}; FMT_CONSTEXPR_DECL bool invalid_format = - check_format_string( + do_check_format_string( basic_string_view(s, sizeof...(CHARS))); (void)invalid_format; return format(s, args...); diff --git a/test/format-test.cc b/test/format-test.cc index c6fe386c..743e44cd 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1426,13 +1426,14 @@ TEST(FormatterTest, FormatFloat) { TEST(FormatterTest, FormatDouble) { check_unknown_types(1.2, "eEfFgGaA", "double"); - EXPECT_EQ("0", format("{0:}", 0.0)); - EXPECT_EQ("0.000000", format("{0:f}", 0.0)); - EXPECT_EQ("392.65", format("{0:}", 392.65)); - EXPECT_EQ("392.65", format("{0:g}", 392.65)); - EXPECT_EQ("392.65", format("{0:G}", 392.65)); - EXPECT_EQ("392.650000", format("{0:f}", 392.65)); - EXPECT_EQ("392.650000", format("{0:F}", 392.65)); + EXPECT_EQ("0", format("{:}", 0.0)); + EXPECT_EQ("0.000000", format("{:f}", 0.0)); + EXPECT_EQ("0", format("{:g}", 0.0)); + EXPECT_EQ("392.65", format("{:}", 392.65)); + EXPECT_EQ("392.65", format("{:g}", 392.65)); + EXPECT_EQ("392.65", format("{:G}", 392.65)); + EXPECT_EQ("392.650000", format("{:f}", 392.65)); + EXPECT_EQ("392.650000", format("{:F}", 392.65)); char buffer[BUFFER_SIZE]; safe_sprintf(buffer, "%e", 392.65); EXPECT_EQ(buffer, format("{0:e}", 392.65)); @@ -2315,7 +2316,7 @@ FMT_CONSTEXPR bool equal(const char *s1, const char *s2) { template FMT_CONSTEXPR bool test_error(const char *fmt, const char *expected_error) { const char *actual_error = FMT_NULL; - fmt::internal::check_format_string( + fmt::internal::do_check_format_string( string_view(fmt, len(fmt)), test_error_handler(actual_error)); return equal(actual_error, expected_error); } diff --git a/test/printf-test.cc b/test/printf-test.cc index 81d949dd..d3e222f2 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -509,3 +509,12 @@ TEST(PrintfTest, VPrintf) { EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42"); EXPECT_WRITE(stdout, fmt::vfprintf(std::cout, "%d", args), "42"); } + +template +void check_format_string_regression(fmt::string_view s, const Args&... args) { + fmt::sprintf(s, args...); +} + +TEST(PrintfTest, CheckFormatStringRegression) { + check_format_string_regression("%c%s", 'x', ""); +}