Fix bug found by fuzzing

This commit is contained in:
Shawn Zhong 2023-01-13 05:47:34 -06:00
parent 6a0f6ff708
commit 5dc570cc0e
2 changed files with 8 additions and 0 deletions

View File

@ -706,6 +706,7 @@ FMT_CONSTEXPR const Char* parse_chrono_format(const Char* begin,
} }
if (begin != ptr) handler.on_text(begin, ptr); if (begin != ptr) handler.on_text(begin, ptr);
c = *++ptr; // consume '%' c = *++ptr; // consume '%'
if (ptr == end) FMT_THROW(format_error("invalid format"));
switch (c) { switch (c) {
case '_': case '_':
pad = pad_type::space; pad = pad_type::space;

View File

@ -921,6 +921,13 @@ TEST(chrono_test, timestamps_sub_seconds) {
} }
TEST(chrono_test, glibc_extensions) { 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) + const auto d = std::chrono::hours(1) + std::chrono::minutes(2) +
std::chrono::seconds(3); std::chrono::seconds(3);