[libc-commits] [PATCH] D113790: [libc] fix strtof/d/ld NaN parsing
Michael Jones via Phabricator via libc-commits
libc-commits at lists.llvm.org
Fri Nov 12 12:05:56 PST 2021
michaelrj created this revision.
michaelrj added reviewers: sivachandra, lntue.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added a project: libc-project.
michaelrj requested review of this revision.
Fix the fact that previously strtof/d/ld would only accept a NaN as
having parentheses if the thing in the parentheses was a valid number,
now it will accept any combination of letters and numbers, but will only
put valid numbers in the mantissa.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D113790
Files:
libc/src/__support/str_to_float.h
libc/test/src/stdlib/strtof_test.cpp
Index: libc/test/src/stdlib/strtof_test.cpp
===================================================================
--- libc/test/src/stdlib/strtof_test.cpp
+++ 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);
}
Index: libc/src/__support/str_to_float.h
===================================================================
--- libc/src/__support/str_to_float.h
+++ 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<BitsType>(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<BitsType>(
+ strtointeger<uint64_t>(parenStart + 1, &tempSrc, 0));
+ if (*tempSrc != ')') {
+ NaNMantissa = 0;
+ }
}
+ } else {
+ src = parenStart;
}
}
NaNMantissa |= fputil::FloatProperties<T>::quietNaNMask;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113790.386909.patch
Type: text/x-patch
Size: 1890 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20211112/90ee2583/attachment.bin>
More information about the libc-commits
mailing list