Detect overflow on large precision

This commit is contained in:
Victor Zverovich 2021-12-05 07:12:10 -08:00
parent c240d98ffd
commit 215f21a038
3 changed files with 10 additions and 2 deletions

View File

@ -704,7 +704,12 @@ FMT_INLINE FMT_CONSTEXPR20 digits::result grisu_gen_digits(
if (handler.fixed) {
// Adjust fixed precision by exponent because it is relative to decimal
// point.
handler.precision += exp + handler.exp10;
int precision_offset = exp + handler.exp10;
if (precision_offset > 0 &&
handler.precision > max_value<int>() - precision_offset) {
throw format_error("number is too big");
}
handler.precision += precision_offset;
// Check if precision is satisfied just by leading zeros, e.g.
// format("{:.2f}", 0.001) gives "0.00" without generating any digits.
if (handler.precision <= 0) {

View File

@ -934,6 +934,9 @@ TEST(format_test, precision) {
EXPECT_THROW_MSG((void)fmt::format(runtime("{:.{}e}"), 42.0,
fmt::detail::max_value<int>()),
format_error, "number is too big");
EXPECT_THROW_MSG(
(void)fmt::format("{:.2147483646f}", -2.2121295195081227E+304),
format_error, "number is too big");
EXPECT_EQ("st", fmt::format("{0:.2}", "str"));
}

View File

@ -12,7 +12,7 @@
void check_round_trip(fmt::string_view format_str, double value) {
auto buffer = fmt::memory_buffer();
fmt::format_to(buffer, format_str, value);
fmt::format_to(std::back_inserter(buffer), format_str, value);
if (std::isnan(value)) {
auto nan = std::signbit(value) ? "-nan" : "nan";