From 49f89749862749e4571983cac971ffb798d30c99 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Fri, 2 Dec 2022 16:29:35 -0600 Subject: [Parser] Avoid calling `strtod` on NaNs entirely (#5316) MSVC's implementation of `strtod` doesn't return a negative Nan for "-nan", so we already had a workaround to explicitly handle that case without calling `strtod`. Unfortunately the workaround was not used for negative NaNs with payloads, so there were still bugs. Fix the problem and make the code even more portable by avoiding `strtod` completely for any kind of nan, positive or negative, with or without payload. --- src/wasm/wat-lexer.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/wasm/wat-lexer.cpp b/src/wasm/wat-lexer.cpp index 6d7e9edec..264ffd40c 100644 --- a/src/wasm/wat-lexer.cpp +++ b/src/wasm/wat-lexer.cpp @@ -227,12 +227,13 @@ struct LexFloatCtx : LexCtx { if (!basic) { return {}; } - if (nanPayload) { - double nan = basic->span[0] == '-' ? negNan : posNan; - return LexFloatResult{*basic, nanPayload, nan}; + // strtod does not return NaNs with the expected signs on all platforms. + // TODO: use starts_with once we have C++20. + if (basic->span.substr(0, 3) == "nan"sv || + basic->span.substr(0, 4) == "+nan"sv) { + return LexFloatResult{*basic, nanPayload, posNan}; } - // strtod does not return -NAN for "-nan" on all platforms. - if (basic->span == "-nan"sv) { + if (basic->span.substr(0, 4) == "-nan"sv) { return LexFloatResult{*basic, nanPayload, negNan}; } // Do not try to implement fully general and precise float parsing -- cgit v1.2.3