diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 06a311b3..d3a9ec59 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -706,6 +706,7 @@ FMT_CONSTEXPR const Char* parse_chrono_format(const Char* begin, } if (begin != ptr) handler.on_text(begin, ptr); c = *++ptr; // consume '%' + if (ptr == end) FMT_THROW(format_error("invalid format")); switch (c) { case '_': pad = pad_type::space; diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 17e57bf5..8f02a100 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -921,6 +921,13 @@ TEST(chrono_test, timestamps_sub_seconds) { } TEST(chrono_test, glibc_extensions) { + EXPECT_THROW_MSG((void)fmt::format(runtime("{:%0}"), std::chrono::seconds()), + fmt::format_error, "invalid format"); + EXPECT_THROW_MSG((void)fmt::format(runtime("{:%_}"), std::chrono::seconds()), + fmt::format_error, "invalid format"); + EXPECT_THROW_MSG((void)fmt::format(runtime("{:%-}"), std::chrono::seconds()), + fmt::format_error, "invalid format"); + { const auto d = std::chrono::hours(1) + std::chrono::minutes(2) + std::chrono::seconds(3);