From 5f9996dc9c4c057a6d54d4332d4e4baf2ea904a1 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Mon, 3 Oct 2022 15:05:54 +0000 Subject: [PATCH] Replace std::stof with std::strtof. std::stof can throw std::out_of_range if the input is not actually representable. We had similar code in Skia which was using stof to test that a stringized float would round-trip successfully, and it would throw an exception on some older versions of libc++ for edge- case inputs like FLT_MIN. std::stof is documented as using strtof to do its conversion, so this shouldn't change your results in practice; it just removes the part where it could potentially throw for some inputs. Tangentially, have you ever seen a case where the scientific-notation path gets used? According to brucedawson@, nine digits should always safely round-trip (in 2013, testing gcc and MSVC). See https://randomascii.wordpress.com/2013/02/07/float-precision-revisited-nine-digit-float-portability/ Change-Id: Ie215fb8502dd8c554020c6f73432f91e3d756563 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/104500 Reviewed-by: Dan Sinclair Auto-Submit: John Stiles Commit-Queue: Dan Sinclair Commit-Queue: John Stiles --- src/tint/writer/float_to_string.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tint/writer/float_to_string.cc b/src/tint/writer/float_to_string.cc index e0c3260d54..b8ae313e58 100644 --- a/src/tint/writer/float_to_string.cc +++ b/src/tint/writer/float_to_string.cc @@ -15,6 +15,7 @@ #include "src/tint/writer/float_to_string.h" #include +#include #include #include #include @@ -35,7 +36,7 @@ std::string FloatToString(float f) { // If this string can be parsed without loss of information, use it auto float_equal_no_warning = std::equal_to(); - if (float_equal_no_warning(std::stof(fixed.str()), f)) { + if (float_equal_no_warning(std::strtof(fixed.str().c_str(), nullptr), f)) { auto str = fixed.str(); while (str.length() >= 2 && str[str.size() - 1] == '0' && str[str.size() - 2] != '.') { str.pop_back();