diff --git a/include/fmt/format.h b/include/fmt/format.h index e6c649d2..8a4ab7fb 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1715,7 +1715,12 @@ template class basic_writer { return; } int precision = specs.precision >= 0 || !specs.type ? specs.precision : 6; - if (fspecs.format == float_format::exp) ++precision; + if (fspecs.format == float_format::exp) { + if (precision == max_value()) + FMT_THROW(format_error("number is too big")); + else + ++precision; + } if (const_check(std::is_same())) fspecs.binary32 = true; fspecs.use_grisu = use_grisu(); if (const_check(FMT_DEPRECATED_PERCENT) && fspecs.percent) value *= 100; diff --git a/test/format-test.cc b/test/format-test.cc index 42ba77ba..7f718e7a 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1236,6 +1236,9 @@ TEST(FormatterTest, Precision) { EXPECT_THROW_MSG(format("{0:.2f}", reinterpret_cast(0xcafe)), format_error, "precision not allowed for this argument type"); + EXPECT_THROW_MSG(format("{:.{}e}", 42.0, fmt::internal::max_value()), + format_error, + "number is too big"); EXPECT_EQ("st", format("{0:.2}", "str")); }