fix(libc): return +0 from strtod and friends when no conversion is performed #2725

Closed
husseinkad wants to merge 2 commits from upstream-pr/2725 into main
husseinkad commented 2026-10-10 12:19:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T12:19:55Z.


What

On Windows the guest strtod, strtof, strtold, wcstod, wcstof and wcstold passed straight through to the host, which returns -0.0 when a sign prefix is not followed by a number: strtod("-.", &e), "-x", "-.e1", "- 1", "-e5" all returned -0.0 with nothing consumed (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 the same way, and the wide functions go through the narrow ones. C17 7.22.1.3 only says that zero is returned when no conversion could be performed.

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, including those that give -0.0 (-0, -.0, -0e5, -0x0p0, -0x consuming -0). atof now calls strtod(str, NULL) as FreeBSD's does, so it gets the same fix; for the string nan it returns the quiet NaN 0x7ff8... instead of the CRT's 0x7fff..., and it now sets ERANGE on overflow like strtod.

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, so it is left alone.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release:

  • guest_math gains the no-conversion shapes ("", " ", "-", "+", "-.", "+.", "-x", "-.e1", "-e5", "- 1", " -.") for double, float and long double, a null endptr, the wide variants, the -0 conversions that must keep their sign, -inf, and atof("nan"); it fails without the change (exit 0xc0000409) and passes with it. ctest -R "guest_(math|wide_strings|wide_formatting|strings|formatting)$" passes.
  • Outside the repo, 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 whitespace kinds, embedded NULs, non-ASCII characters, nan(...) forms), 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 NaN bits and ERANGE; no real conversion lost its sign.
  • tools/check_conventions.py --base origin/main passes; tools/pr_overlap.py --branch reports no overlap.
  • Linux not run (the helper does not depend on the host's sign convention).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4811139982 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2725). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T12:19:55Z. --- ### What On Windows the guest `strtod`, `strtof`, `strtold`, `wcstod`, `wcstof` and `wcstold` passed straight through to the host, which returns `-0.0` when a sign prefix is not followed by a number: `strtod("-.", &e)`, `"-x"`, `"-.e1"`, `"- 1"`, `"-e5"` all returned `-0.0` with nothing consumed (`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` the same way, and the wide functions go through the narrow ones. C17 7.22.1.3 only says that zero is returned when no conversion could be performed. 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, including those that give `-0.0` (`-0`, `-.0`, `-0e5`, `-0x0p0`, `-0x` consuming `-0`). `atof` now calls `strtod(str, NULL)` as FreeBSD's does, so it gets the same fix; for the string `nan` it returns the quiet NaN `0x7ff8...` instead of the CRT's `0x7fff...`, and it now sets `ERANGE` on overflow like `strtod`. 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, so it is left alone. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release: - `guest_math` gains the no-conversion shapes (`""`, `" "`, `"-"`, `"+"`, `"-."`, `"+."`, `"-x"`, `"-.e1"`, `"-e5"`, `"- 1"`, `" -."`) for double, float and long double, a null `endptr`, the wide variants, the `-0` conversions that must keep their sign, `-inf`, and `atof("nan")`; it fails without the change (exit `0xc0000409`) and passes with it. `ctest -R "guest_(math|wide_strings|wide_formatting|strings|formatting)$"` passes. - Outside the repo, 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 whitespace kinds, embedded NULs, non-ASCII characters, `nan(...)` forms), 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` NaN bits and `ERANGE`; no real conversion lost its sign. - `tools/check_conventions.py --base origin/main` passes; `tools/pr_overlap.py --branch` reports no overlap. - Linux not run (the helper does not depend on the host's sign convention). ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-10 12:20:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T12:20:04Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097420031 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2725#issuecomment-6097420031). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T12:20:04Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

Pull request closed

Sign in to join this conversation.
No reviewers
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!2725
No description provided.