summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2022-12-02 16:29:35 -0600
committerGitHub <noreply@github.com>2022-12-02 14:29:35 -0800
commit49f89749862749e4571983cac971ffb798d30c99 (patch)
tree1e013cf884ad479deae6ef05abea3c7013726fcc
parent99cd20964345f6280cca463b7bca98804a6a24c8 (diff)
downloadbinaryen-49f89749862749e4571983cac971ffb798d30c99.tar.gz
binaryen-49f89749862749e4571983cac971ffb798d30c99.tar.bz2
binaryen-49f89749862749e4571983cac971ffb798d30c99.zip
[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.
-rw-r--r--src/wasm/wat-lexer.cpp11
1 files changed, 6 insertions, 5 deletions
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