libc: strtod, strtof, strtold and the wide variants return -0.0 when a sign is not followed by a number, where FreeBSD returns +0.0 #3090
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T21:56:44Z.
What
On Windows the guest
strtod,strtof,strtold,wcstod,wcstofandwcstold(core/libs/prx/libc/src/Strings.cpp) pass straight through to the host, which returns-0.0when a sign prefix is not followed by a number and nothing is consumed:strtod("-.", &e),"-x","-.e1","- 1"and"-e5"all return-0.0withe == nptr. FreeBSD's gdtoastrtod.ctakes itsret0path there (s = s00; sign = 0;) and returns+0.0;strtodg.c, used forstrtold, clears the sign forSTRTOG_NoNumberin the same way, and the wide functions go through the narrow ones. C17 7.22.1.3 only says zero is returned when no conversion could be performed.atofgoes through the host as well and differs in the same way.Reproduce
Windows 11, WinLibs GCC 15.2.0 r7,
mainat the time of writing:signbit(strtod_nid_postfix("-.", &end))is true andendequals the input; FreeBSD gives+0.0. Real conversions that give-0.0(-0,-.0,-0e5,-0x0p0) are correct and must stay that way.Fix
A tested fix is on my fork:
husseinkad:fix/libc-strtod-no-conversion-zero(two commits onmain; PR #2725, closed by the intake check): a small helper returns+0when the host consumed nothing (it detects that itself, so a nullendptrworks) and leaves every real conversion alone;atofnow callsstrtod(str, NULL)as FreeBSD's does (for the stringnanit returns the quiet NaN0x7ff8...instead of the CRT's0x7fff..., and it setsERANGEon overflow likestrtod).guest_mathcovers the no-conversion shapes for double, float, long double and the wide variants, the-0conversions that must keep their sign andatof("nan"), and fails without the change.Checked with the old and new
libc.prxside by side over 937 million inputs (all 917 million strings of length 0 to 6 over+-.0123456789eExXpPaAfFnNiI _()plus 20 million random numeric-looking strings with every whitespace kind, embedded NULs and non-ASCII characters), comparing value bits (x87 for long double), end offset anderrnofor the six functions and with a nullendptr: 0 unintended differences; the only changes are-0.0becoming+0.0where nothing was consumed (about 20 million cases) and theatofdifferences above. A lead from the same audit,strtod("nan(a b)")consuming all 8 characters, is not a defect: FreeBSD'shexnan.caccepts whitespace inside the parentheses too. Linux not run.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by ArekatlaNishanthchowdary on 2026-10-11T16:25:45Z.
Rebased branch ready:
ArekatlaNishanthchowdary:fix/libc-strtod-no-conversion-zero(d4f1303d4), @husseinkad's two commits on currentmain(a11c8f7f7) with authorship kept. The original branch no longer merges:GuestMath.cppconflicted with thelroundchecks added since, and both sides are kept.Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release:
guest_mathandguest_stringspass 5/5 with the change;guest_mathaborts (0xc0000409) withmain'sStrings.cppand the new test.ctest: 827/836 pass, 5 skipped.guest_kernel_sys(#3510),windows_exceptions(#3411),agc_driver_emulated_color_compareandguest_filesystem(#3518) fail the same way on unmodifiedmainon this host.tools/check_conventions.py --base origin/mainpasses.My one open-PR slot is taken by #3744, so I will open this as a PR once that is merged or closed. Anyone eligible is welcome to carry it sooner.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by aureau on 2026-10-11T19:06:28Z.
@ArekatlaNishanthchowdary
Hey bro I carried forward your rebased fix into #3905 - I preserved your original commits and all of the tests passed on my end as well just as you stated. Thanks
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T07:25:06Z.
Windows test of the fix branch (
husseinkad:fix/libc-strtod-no-conversion-zero,53a18da08+a02016b9a) merged onto current upstream/maind146ec1b6(Windows 10, WinLibs GCC 15.2, Release; builtguest_math_tests,guest_string_tests,guest_wide_string_tests):b5f5362d3and now conflicts with main incore/libs/tests/GuestMath.cpp(both sides add a test function beforemain():CheckMissingClassificationExportsfrom main andCheckNoConversionIsPositiveZerofrom the branch, plus both calls inmain). I resolved it by keeping both;Strings.cppmerges cleanly. The branch needs a rebase before it can be opened.guest_math,guest_stringsandguest_wide_stringspass, andpython tools/check_conventions.py --base upstream/mainpasses.Strings.cppchange (main's version, same test file):guest_mathfails, so the new cases do test the change.libc.prx, and the FreeBSD gdtoa semantics beyond what the issue cites (contrib/gdtoa/strtod.cret0path); Linux not run.The earlier PRs for this change (#2725, #3613) are closed, so this still needs someone to carry it.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by ArekatlaNishanthchowdary on 2026-10-12T07:38:11Z.
This is already carried: #3905 is open with the two commits rebased (from the branch in my comment above) and was updated against
mainon 12 October.AI-assisted: yes (Claude Code).