diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h --- a/libc/src/__support/str_to_float.h +++ b/libc/src/__support/str_to_float.h @@ -783,14 +783,28 @@ src += 3; BitsType NaNMantissa = 0; if (*src == '(') { - char *tempSrc = 0; - if (isdigit(*(src + 1)) || *(src + 1) == ')') { - NaNMantissa = strtointeger(src + 1, &tempSrc, 0); - if (*tempSrc != ')') { - NaNMantissa = 0; - } else { - src = tempSrc + 1; + const char *parenStart = src; + ++src; + while (isalnum(*src)) { + ++src; + } + if (*src == ')') { + ++src; + char *tempSrc = 0; + if (isdigit(*(parenStart + 1))) { + // This is to prevent errors when BitsType is larger than 64 bits, + // since strtointeger only supports up to 64 bits. This is actually + // more than is required by the specification, which says for the + // input type "NAN(n-char-sequence)" that "the meaning of + // the n-char sequence is implementation-defined." + NaNMantissa = static_cast( + strtointeger(parenStart + 1, &tempSrc, 0)); + if (*tempSrc != ')') { + NaNMantissa = 0; + } } + } else { + src = parenStart; } } NaNMantissa |= fputil::FloatProperties::quietNaNMask; diff --git a/libc/test/src/stdlib/strtof_test.cpp b/libc/test/src/stdlib/strtof_test.cpp --- a/libc/test/src/stdlib/strtof_test.cpp +++ b/libc/test/src/stdlib/strtof_test.cpp @@ -160,4 +160,5 @@ runTest("NaN()", 5, 0x7fc00000); runTest("NaN(1234)", 9, 0x7fc004d2); runTest("NaN( 1234)", 3, 0x7fc00000); + runTest("NaN(asdf)", 9, 0x7fc00000); }