diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 53f861fa8ce1..e242dc6e6996 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -614,6 +614,8 @@ template FMT_CONSTEXPR const Char* parse_chrono_format(const Char* begin, const Char* end, Handler&& handler) { + if (begin == end || *begin == '}') return begin; + if (*begin != '%') FMT_THROW(format_error("invalid format")); auto ptr = begin; while (ptr != end) { auto c = *ptr; diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 2321c64c17a1..8d69971581dd 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -435,6 +435,10 @@ TEST(chrono_test, invalid_specs) { "invalid format"); EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Oq}"), sec), fmt::format_error, "invalid format"); + EXPECT_THROW_MSG((void)fmt::format(runtime("{:abc}"), sec), fmt::format_error, + "invalid format"); + EXPECT_THROW_MSG((void)fmt::format(runtime("{:.2f}"), sec), fmt::format_error, + "invalid format"); } auto format_tm(const std::tm& time, fmt::string_view spec,