From a5d0adf3953f0ae3e698d5b5702d1e5a7bf3d87e Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 5 Aug 2016 07:27:56 -0700 Subject: [PATCH] Use a heuristic to detect empty strftime result (#367) --- fmt/time.h | 7 +++++++ test/time-test.cc | 4 ++++ 2 files changed, 11 insertions(+) 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())); +}