forked from fmtlib/fmt
Detect overflow on large precision
This commit is contained in:
@@ -704,7 +704,12 @@ FMT_INLINE FMT_CONSTEXPR20 digits::result grisu_gen_digits(
|
|||||||
if (handler.fixed) {
|
if (handler.fixed) {
|
||||||
// Adjust fixed precision by exponent because it is relative to decimal
|
// Adjust fixed precision by exponent because it is relative to decimal
|
||||||
// point.
|
// 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.
|
// Check if precision is satisfied just by leading zeros, e.g.
|
||||||
// format("{:.2f}", 0.001) gives "0.00" without generating any digits.
|
// format("{:.2f}", 0.001) gives "0.00" without generating any digits.
|
||||||
if (handler.precision <= 0) {
|
if (handler.precision <= 0) {
|
||||||
|
@@ -934,6 +934,9 @@ TEST(format_test, precision) {
|
|||||||
EXPECT_THROW_MSG((void)fmt::format(runtime("{:.{}e}"), 42.0,
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:.{}e}"), 42.0,
|
||||||
fmt::detail::max_value<int>()),
|
fmt::detail::max_value<int>()),
|
||||||
format_error, "number is too big");
|
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"));
|
EXPECT_EQ("st", fmt::format("{0:.2}", "str"));
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
void check_round_trip(fmt::string_view format_str, double value) {
|
void check_round_trip(fmt::string_view format_str, double value) {
|
||||||
auto buffer = fmt::memory_buffer();
|
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)) {
|
if (std::isnan(value)) {
|
||||||
auto nan = std::signbit(value) ? "-nan" : "nan";
|
auto nan = std::signbit(value) ? "-nan" : "nan";
|
||||||
|
Reference in New Issue
Block a user