diff --git a/fmt/time.h b/fmt/time.h index e20b2b48..5375fc81 100644 --- a/fmt/time.h +++ b/fmt/time.h @@ -36,6 +36,13 @@ void format(BasicFormatter &f, buffer.resize(start + count); break; } + if (size >= format.size() * 256) { + // If the buffer is 256 times larger than the format string, assume + // that `strftime` gives an empty result. There doesn't seem to be a + // better way to distinguish the two cases: + // https://github.com/fmtlib/fmt/issues/367 + break; + } const std::size_t MIN_GROWTH = 10; buffer.reserve(buffer.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH)); } diff --git a/test/time-test.cc b/test/time-test.cc index f595090e..48df56a0 100644 --- a/test/time-test.cc +++ b/test/time-test.cc @@ -27,3 +27,7 @@ TEST(TimeTest, GrowBuffer) { std::time_t t = std::time(0); fmt::format(s, *std::localtime(&t)); } + +TEST(TimeTest, EmptyResult) { + EXPECT_EQ("", fmt::format("{}", std::tm())); +}