mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Give an error on precision overflow
This commit is contained in:
@ -1715,7 +1715,12 @@ template <typename Range> class basic_writer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int precision = specs.precision >= 0 || !specs.type ? specs.precision : 6;
|
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<int>())
|
||||||
|
FMT_THROW(format_error("number is too big"));
|
||||||
|
else
|
||||||
|
++precision;
|
||||||
|
}
|
||||||
if (const_check(std::is_same<T, float>())) fspecs.binary32 = true;
|
if (const_check(std::is_same<T, float>())) fspecs.binary32 = true;
|
||||||
fspecs.use_grisu = use_grisu<T>();
|
fspecs.use_grisu = use_grisu<T>();
|
||||||
if (const_check(FMT_DEPRECATED_PERCENT) && fspecs.percent) value *= 100;
|
if (const_check(FMT_DEPRECATED_PERCENT) && fspecs.percent) value *= 100;
|
||||||
|
@ -1236,6 +1236,9 @@ TEST(FormatterTest, Precision) {
|
|||||||
EXPECT_THROW_MSG(format("{0:.2f}", reinterpret_cast<void*>(0xcafe)),
|
EXPECT_THROW_MSG(format("{0:.2f}", reinterpret_cast<void*>(0xcafe)),
|
||||||
format_error,
|
format_error,
|
||||||
"precision not allowed for this argument type");
|
"precision not allowed for this argument type");
|
||||||
|
EXPECT_THROW_MSG(format("{:.{}e}", 42.0, fmt::internal::max_value<int>()),
|
||||||
|
format_error,
|
||||||
|
"number is too big");
|
||||||
|
|
||||||
EXPECT_EQ("st", format("{0:.2}", "str"));
|
EXPECT_EQ("st", format("{0:.2}", "str"));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user