fix(libc): print NaN without a sign in the guest printf formatter #4548

Merged
coltken95 merged 1 commit from upstream-pr/4548 into main 2026-10-13 14:31:35 +00:00
coltken95 commented 2026-10-12 15:23:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-12T15:23:08Z.


What

The guest printf formatter (LibcDetail::FormatWindows, used for every format on Windows and for formats with 16-bit wide arguments on Linux) handed NaN to the host's snprintf, which prints -nan for a negative NaN and +nan or nan with the + and space flags. FreeBSD 11 vfprintf.c clears the sign of every NaN (sign = '\0') and prints nan (NAN for the upper-case conversions), keeping the width and the - flag and dropping zero padding. The formatter now prints NaN that way for a, e, f, g, their upper-case forms and long double; infinities are unchanged.

Formats without wide arguments still go to glibc on Linux, so the TechnicalDebt note that already lists %p ((nil)) there now lists NaN too.

Tested

Linux (Ubuntu 24.04, GCC 13.3). New guest_formatting cases through vsnprintf with a wide argument: negative and positive NaN with %f, %+f, % F, %5.1e, %-6g, %06a, %LG, and -inf for comparison. They fail before this change (-nan, +nan) and pass after. All 44 libc guest_* tests registered in this configuration pass. Windows not built locally (no MinGW toolchain available). No title tested.

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 (attach them to this PR)
  • 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:4831476690 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4548). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-12T15:23:08Z. --- ### What The guest printf formatter (`LibcDetail::FormatWindows`, used for every format on Windows and for formats with 16-bit wide arguments on Linux) handed NaN to the host's `snprintf`, which prints `-nan` for a negative NaN and `+nan` or ` nan` with the `+` and space flags. FreeBSD 11 [vfprintf.c](https://github.com/freebsd/freebsd-src/blob/5ed7eb0d97ba4436218e810f61bd059acba984c2/lib/libc/stdio/vfprintf.c#L729-L737) clears the sign of every NaN (`sign = '\0'`) and prints `nan` (`NAN` for the upper-case conversions), keeping the width and the `-` flag and dropping zero padding. The formatter now prints NaN that way for `a`, `e`, `f`, `g`, their upper-case forms and `long double`; infinities are unchanged. Formats without wide arguments still go to glibc on Linux, so the TechnicalDebt note that already lists `%p` (`(nil)`) there now lists NaN too. ### Tested Linux (Ubuntu 24.04, GCC 13.3). New `guest_formatting` cases through `vsnprintf` with a wide argument: negative and positive NaN with `%f`, `%+f`, `% F`, `%5.1e`, `%-6g`, `%06a`, `%LG`, and `-inf` for comparison. They fail before this change (`-nan`, `+nan`) and pass after. All 44 libc `guest_*` tests registered in this configuration pass. Windows not built locally (no MinGW toolchain available). No title tested. ### 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 (attach them to this PR) - [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-13 10:35:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T10:35:51Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#5070 core/libs/tests/Formatting.cpp L5
docs/dev/debt/libc.md L53
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143662279 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4548#issuecomment-6143662279). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T10:35:51Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #5070 | `core/libs/tests/Formatting.cpp` [L5](https://github.com/boykopovar/AnyPS5/blob/146f1715499c1f6346662cb45176ff33ec3ad445/core/libs/tests/Formatting.cpp#L5)<br>`docs/dev/debt/libc.md` [L53](https://github.com/boykopovar/AnyPS5/blob/146f1715499c1f6346662cb45176ff33ec3ad445/docs/dev/debt/libc.md#L53) | | |
adriadam10 commented 2026-10-13 14:17:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:17:12Z.


Checked against FreeBSD vfprintf.c (NaN clears the sign, prints nan/NAN, drops zero padding), build + libc/libs ctest on Linux, and the new guest_formatting cases fail without the change. Merging after the full Build.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6147620259 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4548#issuecomment-6147620259). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:17:12Z. --- Checked against FreeBSD `vfprintf.c` (NaN clears the sign, prints `nan`/`NAN`, drops zero padding), build + libc/libs ctest on Linux, and the new `guest_formatting` cases fail without the change. Merging after the full Build.
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!4548
No description provided.