[PATCH 3/5] libstdc++: Adjust fast_float's over/underflow behavior for conformnace

Patrick Palka ppalka@redhat.com
Tue Nov 16 00:25:03 GMT 2021


This makes fast_float handle the situation where std::from_chars is
specified to return result_out_of_range, i.e. when the parsed value
is outside the representable range of the floating-point type.

libstdc++-v3/ChangeLog:

	* src/c++17/fast_float/LOCAL_PATCHES: Update.
	* src/c++17/fast_float/parse_number.h (from_chars_advanced): In
	case of over/underflow, return errc::result_out_of_range and don't
	modify 'value'.
---
 libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES  |  1 +
 libstdc++-v3/src/c++17/fast_float/parse_number.h | 10 ++++++++++
 2 files changed, 11 insertions(+)

diff --git a/libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES b/libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES
index e9d7bba6195..1f90f9d1d85 100644
--- a/libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES
+++ b/libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES
@@ -1 +1,2 @@
 r12-????
+r12-????
diff --git a/libstdc++-v3/src/c++17/fast_float/parse_number.h b/libstdc++-v3/src/c++17/fast_float/parse_number.h
index 86dea2287b4..57b3585c2fe 100644
--- a/libstdc++-v3/src/c++17/fast_float/parse_number.h
+++ b/libstdc++-v3/src/c++17/fast_float/parse_number.h
@@ -99,6 +99,16 @@ from_chars_result from_chars_advanced(const char *first, const char *last,
   // If we called compute_float<binary_format<T>>(pns.exponent, pns.mantissa) and we have an invalid power (am.power2 < 0),
   // then we need to go the long way around again. This is very uncommon.
   if(am.power2 < 0) { am = digit_comp<T>(pns, am); }
+
+  if((pns.mantissa != 0 && am.mantissa == 0 && am.power2 == 0) || am.power2 == binary_format<T>::infinite_power()) {
+    // In case of over/underflow, return result_out_of_range and don't modify value,
+    // as per [charconv.from.chars]/1.
+    //
+    // If LWG 3081 gets adopted, then we'll need to call to_float in this case too.
+    answer.ec = std::errc::result_out_of_range;
+    return answer;
+  }
+
   to_float(pns.negative, am, value);
   return answer;
 }
-- 
2.34.0



More information about the Gcc-patches mailing list