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) {