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

Closed
opened 2026-10-10 21:56:44 +00:00 by husseinkad · 4 comments
husseinkad commented 2026-10-10 21:56:44 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T21:56:44Z.


What

On Windows the guest strtod, strtof, strtold, wcstod, wcstof and wcstold (core/libs/prx/libc/src/Strings.cpp) pass straight through to the host, which returns -0.0 when a sign prefix is not followed by a number and nothing is consumed: strtod("-.", &e), "-x", "-.e1", "- 1" and "-e5" all return -0.0 with e == nptr. FreeBSD's gdtoa strtod.c takes its ret0 path there (s = s00; sign = 0;) and returns +0.0; strtodg.c, used for strtold, clears the sign for STRTOG_NoNumber in 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. atof goes through the host as well and differs in the same way.

Reproduce

Windows 11, WinLibs GCC 15.2.0 r7, main at the time of writing: signbit(strtod_nid_postfix("-.", &end)) is true and end equals 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 on main; PR #2725, closed by the intake check): a small helper returns +0 when the host consumed nothing (it detects that itself, so a null endptr works) and leaves every real conversion alone; atof now calls strtod(str, NULL) as FreeBSD's does (for the string nan it returns the quiet NaN 0x7ff8... instead of the CRT's 0x7fff..., and it sets ERANGE on overflow like strtod). guest_math covers the no-conversion shapes for double, float, long double and the wide variants, the -0 conversions that must keep their sign and atof("nan"), and fails without the change.

Checked with the old and new libc.prx side 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 and errno for the six functions and with a null endptr: 0 unintended differences; the only changes are -0.0 becoming +0.0 where nothing was consumed (about 20 million cases) and the atof differences above. A lead from the same audit, strtod("nan(a b)") consuming all 8 characters, is not a defect: FreeBSD's hexnan.c accepts whitespace inside the parentheses too. Linux not run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5795631640 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3090). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T21:56:44Z. --- ### What On Windows the guest `strtod`, `strtof`, `strtold`, `wcstod`, `wcstof` and `wcstold` (`core/libs/prx/libc/src/Strings.cpp`) pass straight through to the host, which returns `-0.0` when a sign prefix is not followed by a number and nothing is consumed: `strtod("-.", &e)`, `"-x"`, `"-.e1"`, `"- 1"` and `"-e5"` all return `-0.0` with `e == nptr`. FreeBSD's gdtoa [`strtod.c`](https://github.com/freebsd/freebsd-src/blob/main/contrib/gdtoa/strtod.c) takes its `ret0` path there (`s = s00; sign = 0;`) and returns `+0.0`; `strtodg.c`, used for `strtold`, clears the sign for `STRTOG_NoNumber` in 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. `atof` goes through the host as well and differs in the same way. ### Reproduce Windows 11, WinLibs GCC 15.2.0 r7, `main` at the time of writing: `signbit(strtod_nid_postfix("-.", &end))` is true and `end` equals 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`](https://github.com/husseinkad/AnyPS5/tree/fix/libc-strtod-no-conversion-zero) (two commits on `main`; PR #2725, closed by the intake check): a small helper returns `+0` when the host consumed nothing (it detects that itself, so a null `endptr` works) and leaves every real conversion alone; `atof` now calls `strtod(str, NULL)` as FreeBSD's does (for the string `nan` it returns the quiet NaN `0x7ff8...` instead of the CRT's `0x7fff...`, and it sets `ERANGE` on overflow like `strtod`). `guest_math` covers the no-conversion shapes for double, float, long double and the wide variants, the `-0` conversions that must keep their sign and `atof("nan")`, and fails without the change. Checked with the old and new `libc.prx` side 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 and `errno` for the six functions and with a null `endptr`: 0 unintended differences; the only changes are `-0.0` becoming `+0.0` where nothing was consumed (about 20 million cases) and the `atof` differences above. A lead from the same audit, `strtod("nan(a b)")` consuming all 8 characters, is not a defect: FreeBSD's `hexnan.c` accepts whitespace inside the parentheses too. Linux not run. AI-assisted: yes.
ArekatlaNishanthchowdary commented 2026-10-11 16:25:45 +00:00 (Migrated from github.com)
Author
Contributor

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 current main (a11c8f7f7) with authorship kept. The original branch no longer merges: GuestMath.cpp conflicted with the lround checks added since, and both sides are kept.

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release:

  • guest_math and guest_strings pass 5/5 with the change; guest_math aborts (0xc0000409) with main's Strings.cpp and the new test.
  • Full ctest: 827/836 pass, 5 skipped. guest_kernel_sys (#3510), windows_exceptions (#3411), agc_driver_emulated_color_compare and guest_filesystem (#3518) fail the same way on unmodified main on this host.
  • tools/check_conventions.py --base origin/main passes.
  • No title or homebrew run. Linux not run.

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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6111129401 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3090#issuecomment-6111129401). Originally posted by [ArekatlaNishanthchowdary](https://github.com/ArekatlaNishanthchowdary) on 2026-10-11T16:25:45Z. --- Rebased branch ready: [`ArekatlaNishanthchowdary:fix/libc-strtod-no-conversion-zero`](https://github.com/ArekatlaNishanthchowdary/AnyPS5/tree/fix/libc-strtod-no-conversion-zero) (`d4f1303d4`), @husseinkad's two commits on current `main` (`a11c8f7f7`) with authorship kept. The original branch no longer merges: `GuestMath.cpp` conflicted with the `lround` checks added since, and both sides are kept. Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release: - `guest_math` and `guest_strings` pass 5/5 with the change; `guest_math` aborts (`0xc0000409`) with `main`'s `Strings.cpp` and the new test. - Full `ctest`: 827/836 pass, 5 skipped. `guest_kernel_sys` (#3510), `windows_exceptions` (#3411), `agc_driver_emulated_color_compare` and `guest_filesystem` (#3518) fail the same way on unmodified `main` on this host. - `tools/check_conventions.py --base origin/main` passes. - No title or homebrew run. Linux not run. 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).
aureau commented 2026-10-11 19:06:28 +00:00 (Migrated from github.com)
Author
Contributor

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6112608703 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3090#issuecomment-6112608703). Originally posted by [aureau](https://github.com/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
xXPBSXx commented 2026-10-12 07:25:06 +00:00 (Migrated from github.com)
Author
Contributor

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/main d146ec1b6 (Windows 10, WinLibs GCC 15.2, Release; built guest_math_tests, guest_string_tests, guest_wide_string_tests):

  • The branch is based on b5f5362d3 and now conflicts with main in core/libs/tests/GuestMath.cpp (both sides add a test function before main(): CheckMissingClassificationExports from main and CheckNoConversionIsPositiveZero from the branch, plus both calls in main). I resolved it by keeping both; Strings.cpp merges cleanly. The branch needs a rebase before it can be opened.
  • With the conflict resolved: guest_math, guest_strings and guest_wide_strings pass, and python tools/check_conventions.py --base upstream/main passes.
  • Without the Strings.cpp change (main's version, same test file): guest_math fails, so the new cases do test the change.
  • Not checked: the reporter's 937-million-input comparison against the old and new libc.prx, and the FreeBSD gdtoa semantics beyond what the issue cites (contrib/gdtoa/strtod.c ret0 path); 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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6119690679 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3090#issuecomment-6119690679). Originally posted by [xXPBSXx](https://github.com/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/main `d146ec1b6` (Windows 10, WinLibs GCC 15.2, Release; built `guest_math_tests`, `guest_string_tests`, `guest_wide_string_tests`): - The branch is based on `b5f5362d3` and now conflicts with main in `core/libs/tests/GuestMath.cpp` (both sides add a test function before `main()`: `CheckMissingClassificationExports` from main and `CheckNoConversionIsPositiveZero` from the branch, plus both calls in `main`). I resolved it by keeping both; `Strings.cpp` merges cleanly. The branch needs a rebase before it can be opened. - With the conflict resolved: `guest_math`, `guest_strings` and `guest_wide_strings` pass, and `python tools/check_conventions.py --base upstream/main` passes. - Without the `Strings.cpp` change (main's version, same test file): `guest_math` fails, so the new cases do test the change. - Not checked: the reporter's 937-million-input comparison against the old and new `libc.prx`, and the FreeBSD gdtoa semantics beyond what the issue cites (`contrib/gdtoa/strtod.c` `ret0` path); 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).
ArekatlaNishanthchowdary commented 2026-10-12 07:38:11 +00:00 (Migrated from github.com)
Author
Contributor

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 main on 12 October.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6119872009 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3090#issuecomment-6119872009). Originally posted by [ArekatlaNishanthchowdary](https://github.com/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 `main` on 12 October. AI-assisted: yes (Claude Code).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#3090
No description provided.