From 8c56919bd281f10ebad3dbf9d7b1b00c2a54b6b6 Mon Sep 17 00:00:00 2001 From: Shawn Zhong Date: Sat, 17 Dec 2022 03:52:20 -0600 Subject: [PATCH] Check chrono spec starts with % --- include/fmt/chrono.h | 2 ++ test/chrono-test.cc | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 9557a377..8a1e5a9b 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -626,6 +626,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 44eccac8..f986c203 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -507,6 +507,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,