summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Mittendrein <maxmitti@maxmitti.tk>2022-01-09 20:36:40 +0100
committerMarkus Mittendrein <maxmitti@maxmitti.tk>2022-01-09 20:36:40 +0100
commit6debbb4a54e98b23d0cf07a90cccdf9aaa36e05c (patch)
treec5033ccb8173b81f83a78eda64308c5c41459a35
parentbf4c0d7089d262a1ea61550ff4cd951f52dad4da (diff)
downloadcxxformat-6debbb4a54e98b23d0cf07a90cccdf9aaa36e05c.tar.gz
cxxformat-6debbb4a54e98b23d0cf07a90cccdf9aaa36e05c.zip
Throw on truncation for the precision value in the floating point formatter
-rw-r--r--include/cxxformat/formatters.hpp17
1 files changed, 15 insertions, 2 deletions
diff --git a/include/cxxformat/formatters.hpp b/include/cxxformat/formatters.hpp
index c32d2e8..b248b17 100644
--- a/include/cxxformat/formatters.hpp
+++ b/include/cxxformat/formatters.hpp
@@ -312,12 +312,25 @@ namespace format {
assert(!"Unsupported conversion; should have been caught by formatter<T>::conversionSupported");
}
+ optional_int<int> intPrecision;
+
+ if (precision)
+ {
+ const auto val = *precision;
+ constexpr auto max = std::numeric_limits<int>::max();
+ if (val > max)
+ {
+ throw std::out_of_range{"Precision is too big; maximum supported for floating point types is " + std::to_string(max)};
+ }
+ intPrecision = static_cast<int>(val);
+ }
+
char* data, *end;
std::errc ec;
- const auto call_to_chars = [format, precision, t, &data, &end, &ec](char* data_, std::size_t size)
+ const auto call_to_chars = [format, intPrecision, t, &data, &end, &ec](char* data_, std::size_t size)
{
data = data_;
- const auto [end_, ec_] = precision ? std::to_chars(data, data + size, t, format, *precision) : std::to_chars(data, data + size, t, format);
+ const auto [end_, ec_] = intPrecision ? std::to_chars(data, data + size, t, format, *intPrecision) : std::to_chars(data, data + size, t, format);
end = end_;
ec = ec_;
};