Repository navigation
fix(libc): return the default quiet NaN from the strtod family - #4616
Merged
adriadam10 merged 3 commits intoOct 13, 2026
Merged
Conversation
C17 7.22.1.3 and 7.29.4.1.1 interpret NAN(n-char-sequence) as a quiet
NaN. With MinGW-w64, std::strtod, strtof and strtold are mingw-w64's
gdtoa functions, which copy the hex digits into the significand
without the quiet bit, and for long double without the x87 integer
bit: strtod("nan(0x123)") returned the signaling NaN
0x7ff0000000000123 and strtold a pseudo-NaN. A payload whose only set
bit is the implicit or x87 integer bit gave infinity:
strtof("nan(0x800000)"), strtod("nan(0x10000000000000)") and
strtold("nan(0x8000000000000000)") returned +inf. glibc returns quiet
NaNs that keep the payload.
C17 7.12.11.2p2 makes nan("n-char-sequence") equivalent to
strtod("NAN(n-char-sequence)", NULL), and nan and nanf already return
the console's fixed quiet NaNs, ignoring the tag. ParseFloat, which
strtod, strtof, strtold, atof and the wide variants share, now returns
the same values, 0x7ff8000000000000 and 0x7fc00000, negated after a
minus sign, on Windows and Linux. strtold returns the x87 default
quiet NaN, since no console nanl was observed.
This was referenced Oct 12, 2026
…ighbouring changes
…-nan-windows # Conflicts: # docs/dev/TechnicalDebt.md
Collaborator
|
Checked the diff, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
C17 7.22.1.3p4, and 7.29.4.1.1p4 for the wide functions, read
NAN(n-char-sequence)as a quiet NaN and leave the meaning of the sequence to the implementation. 7.12.11.2p2 makesnan("n-char-sequence")equivalent tostrtod("NAN(n-char-sequence)", NULL), andnanandnanfonmainreturn the console's fixed quiet NaNs and ignore the tag (31906bf, from the console'slibc.prx). Thestrtodfamily did not match them. On Windows it returned signaling NaNs, x87 pseudo-NaNs and infinities, measured by the new test withmain's implementation:strtodandatofstrtofstrtoldnan(0x123)0x7ff0000000000123(signaling)0x7f800123(signaling)0x7fff'0000000000000123(pseudo-NaN)-NaN(1)0xfff00000000000010xff8000010xffff'0000000000000001nan(0x800000)0x7ff00000008000000x7f800000(+inf)0x7fff'0000000000800000nan(0x10000000000000)0x7ff0000000000000(+inf)0x7f8000010x7fff'0010000000000000nan(0x8000000000000000)0x7ff00000000000010x7f8000010x7fff'8000000000000000(+inf)With this change every row gives what
nanandnanfreturn:0x7ff8000000000000and0x7fc00000, with the sign bit set for-NaN(1), and forstrtoldthe x87 default quiet NaN0x7fff'c000000000000000. The end pointers are unchanged.In C++ the MinGW-w64 toolchain defines
__USE_MINGW_ANSI_STDIO, sostd::strtodandstd::strtofare__mingw_strtodand__mingw_strtof, andstrtoldis mingw-w64's x87strtold. Their NaN-with-payload case copies the hex digits into the significand without the quiet bit (strtodnrp.c builds0x7ff00000 | bits[1]; strtof.c and strtopx.c fall through to the normal-number case), so a payload whose only set bit is the implicit or the x87 integer bit gives an infinity. On Linux, glibc returns quiet NaNs that keep the payload.ParseFloat, whichstrtod,strtof,strtold,atofand the wide variants share since #3905, now replaces an infinite or NaN result with the default quiet NaN of the same sign when the subject, after white space and a sign, starts withnorN;infandinfinitysubjects are untouched. The result is tested on its bits rather than withisnan, so the signaling host value raises noFE_INVALID. TechnicalDebt records that the result is inferred fromnan,nanfand 7.12.11.2p2, not observed on the console, and that no consolenanlwas observed forstrtold.Tested
Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release,
-DBUILD_TESTING=ON, onmainat 2a5eca1:CheckNanConversionsinguest_mathchecks the bits and end pointer of the five inputs throughstrtod,atof,strtofandstrtold, andguest_wide_stringscheckswcstod,wcstofandwcstoldonu" -nan(0x123)é". Withmain'sStrings.cpp,guest_mathprints the 20 values in the table and fails, andguest_wide_stringsaborts at thewcstodcheck. With the change both pass, 20 runs in a row.core/libs/prx/libc/CMakeLists.txtandcore/libs/prx/libkernel/CMakeLists.txtthat build on Windows pass.tools/check_conventions.py --base origin/mainpasses.mainat c952c2b. Withmain'sStrings.cpp,guest_mathandguest_wide_stringsfail, since glibc keeps the payload in the quiet NaN (strtod("nan(0x123)")gives0x7ff8000000000123). With the change both pass, 20 runs in a row.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)