fix(libc): format swprintf output as 16-bit guest units #1512

Closed
Noface07 wants to merge 1 commit from upstream-pr/1512 into main
Noface07 commented 2026-10-08 10:41:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Noface07 on 2026-10-08T10:41:52Z.


What

swprintf passed guest strings to the host's vswprintf. The guest's wchar_t is 16 bits; on Linux the host's is 32 bits, so the output and the %ls arguments were misread and miswritten. On Windows it used a separate formatter (FormatWideWindows) that did not decode UTF-8 %s arguments as vswprintf does.

swprintf now formats through vswprintf_nid_postfix (16-bit FormatWide) on both hosts, so the two functions behave the same:

  • %s arguments are decoded from UTF-8, as in vswprintf and snwprintf_s.
  • Output that does not fit returns -1 and leaves the truncated, terminated output, as vswprintf does.
  • An invalid or unsupported conversion returns -1 with errno 22, as vswprintf does; the Windows formatter called NotImplemented_nid_no_patch for it.

WindowsWideFormatting.hpp has no other user and is removed. The TechnicalDebt entry keeps wcsrtombs, which #1507 implements.

Tested

  • Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.1.0 (x86_64-ucrt-posix-seh; not the 15.2.0 release from BUILD.md), Ninja Release, BUILD_TESTING=ON: full build and libs target, ctest 434/434 passed.
  • Linux (WSL Ubuntu, GCC 15.2.0, no CMake): FormattingWide.cpp compiled with GuestWideFormatting.cpp directly; the test passes. The same test against main's Formatting.cpp aborts at the first swprintf check.
  • tools/check_conventions.py passed. No title tested.

tools/pr_overlap.py reports #1507 on the same TechnicalDebt line (L232) only: it changes wcsrtombs, this PR changes swprintf, no shared exports. Whichever merges second needs that line rebased.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4785520721 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1512). Originally posted by [Noface07](https://github.com/Noface07) on 2026-10-08T10:41:52Z. --- ### What `swprintf` passed guest strings to the host's `vswprintf`. The guest's `wchar_t` is 16 bits; on Linux the host's is 32 bits, so the output and the `%ls` arguments were misread and miswritten. On Windows it used a separate formatter (`FormatWideWindows`) that did not decode UTF-8 `%s` arguments as `vswprintf` does. `swprintf` now formats through `vswprintf_nid_postfix` (16-bit `FormatWide`) on both hosts, so the two functions behave the same: - `%s` arguments are decoded from UTF-8, as in `vswprintf` and `snwprintf_s`. - Output that does not fit returns -1 and leaves the truncated, terminated output, as `vswprintf` does. - An invalid or unsupported conversion returns -1 with errno 22, as `vswprintf` does; the Windows formatter called `NotImplemented_nid_no_patch` for it. `WindowsWideFormatting.hpp` has no other user and is removed. The TechnicalDebt entry keeps `wcsrtombs`, which #1507 implements. ### Tested - Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.1.0 (`x86_64-ucrt-posix-seh`; not the 15.2.0 release from BUILD.md), Ninja Release, `BUILD_TESTING=ON`: full build and `libs` target, `ctest` 434/434 passed. - Linux (WSL Ubuntu, GCC 15.2.0, no CMake): `FormattingWide.cpp` compiled with `GuestWideFormatting.cpp` directly; the test passes. The same test against `main`'s `Formatting.cpp` aborts at the first `swprintf` check. - `tools/check_conventions.py` passed. No title tested. `tools/pr_overlap.py` reports #1507 on the same TechnicalDebt line (L232) only: it changes `wcsrtombs`, this PR changes `swprintf`, no shared exports. Whichever merges second needs that line rebased. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 (Claude Code)
github-actions[bot] commented 2026-10-08 10:53:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T10:53:47Z.


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
#1511 core/libs/tests/GuestWideFormatting.cpp L10, L113, L116
docs/dev/TechnicalDebt.md L241
swprintf_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6058289419 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1512#issuecomment-6058289419). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T10:53:47Z. --- <!-- 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 | |---|---|---|---| | #1511 | `core/libs/tests/GuestWideFormatting.cpp` [L10](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/libs/tests/GuestWideFormatting.cpp#L10), [L113](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/libs/tests/GuestWideFormatting.cpp#L113), [L116](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/libs/tests/GuestWideFormatting.cpp#L116)<br>`docs/dev/TechnicalDebt.md` [L241](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/docs/dev/TechnicalDebt.md#L241) | `swprintf_nid_postfix` | |
klosixgaming-arch commented 2026-10-08 10:54:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T10:54:29Z.

Review: COMMENTED


Good fix that unifies swprintf behavior across Linux and Windows by using the 16-bit FormatWide implementation on both platforms. The test coverage is comprehensive, covering UTF-8 decoding, padding, truncation, exact fit, zero size, and invalid conversions.

The removal of WindowsWideFormatting.hpp simplifies the codebase nicely. One minor note: this PR overlaps with #1511 on the TechnicalDebt.md line (L232) - whichever merges second will need to rebase that line.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5455469341 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1512#pullrequestreview-5455469341). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-08T10:54:29Z. **Review:** COMMENTED --- Good fix that unifies swprintf behavior across Linux and Windows by using the 16-bit FormatWide implementation on both platforms. The test coverage is comprehensive, covering UTF-8 decoding, padding, truncation, exact fit, zero size, and invalid conversions. The removal of WindowsWideFormatting.hpp simplifies the codebase nicely. One minor note: this PR overlaps with #1511 on the TechnicalDebt.md line (L232) - whichever merges second will need to rebase that line.
klosixgaming-arch commented 2026-10-08 11:25:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:25:11Z.

Review: APPROVED


Good fix for swprintf formatting consistency across platforms. This simplifies the codebase by removing the separate Windows-specific implementation and routing through the 16-bit guest formatter on both hosts.

Strengths:

  • Clean simplification that eliminates duplicate code paths
  • Proper handling of UTF-8 %s arguments (decoded as in vswprintf)
  • Comprehensive test coverage including edge cases (truncation, padding, invalid conversions, zero size)
  • Good documentation update removing swprintf from TechnicalDebt.md

Technical notes:

  • The approach of routing through vswprintf_nid_postfix ensures consistent behavior between the two functions
  • Proper error handling for invalid conversions and truncated output
  • Test cases properly verify both success and failure conditions

This will improve compatibility with titles that use swprintf. Ready to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5455809874 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1512#pullrequestreview-5455809874). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-08T11:25:11Z. **Review:** APPROVED --- Good fix for swprintf formatting consistency across platforms. This simplifies the codebase by removing the separate Windows-specific implementation and routing through the 16-bit guest formatter on both hosts. **Strengths:** - Clean simplification that eliminates duplicate code paths - Proper handling of UTF-8 %s arguments (decoded as in vswprintf) - Comprehensive test coverage including edge cases (truncation, padding, invalid conversions, zero size) - Good documentation update removing swprintf from TechnicalDebt.md **Technical notes:** - The approach of routing through vswprintf_nid_postfix ensures consistent behavior between the two functions - Proper error handling for invalid conversions and truncated output - Test cases properly verify both success and failure conditions This will improve compatibility with titles that use swprintf. Ready to merge.
cubersu commented 2026-10-08 13:57:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cubersu on 2026-10-08T13:57:22Z.


Hi @Noface07, #1511 is the same swprintf change and was opened just before this one. Following the suggestion in #1511, I've merged your padded, exact-fit and invalid-conversion tests into it (you're credited as co-author in the commit), so it now covers both test sets. If that works for you, this one could be closed as a duplicate. Thanks for the extra cases!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6061477414 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1512#issuecomment-6061477414). Originally posted by [cubersu](https://github.com/cubersu) on 2026-10-08T13:57:22Z. --- Hi @Noface07, #1511 is the same `swprintf` change and was opened just before this one. Following the suggestion in #1511, I've merged your padded, exact-fit and invalid-conversion tests into it (you're credited as co-author in the commit), so it now covers both test sets. If that works for you, this one could be closed as a duplicate. Thanks for the extra cases!
Dextroid17 commented 2026-10-08 14:00:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T14:00:43Z.


Independent run on Linux, since your Tested section notes the WSL compile-without-CMake route — this is the same change through the real build, and then through CI's own test target.

Merged onto current main (63c57d22): no conflicts. That is worth stating plainly, because in this pair you are the one that still merges cleanly.

ctest -R guest_wide_formatting   →  1/1 passed
full suite (ctest -j8)           →  462/462 passed
CachyOS, GCC 16.2.1, Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON

The tests bite. Implementation reverted to main (Formatting.cpp, FormattingWide.cpp back, WindowsWideFormatting.hpp restored) with your tests and registration kept:

Test #13: guest_wide_formatting ...***Failed (Subprocess aborted)
snwprintf_s: swprintf output length

So your claim — the same test aborts against main's Formatting.cpp — reproduces here with the CMake target and the full libc.prx, not only with the two translation units compiled together.

On the two halves of this change: #1511 is the same swprintf fix, opened 17 seconds after this one, currently CONFLICTING, and it does carry an independent Windows run (@popcoder565: 5/5 on the three wide tests, with a mutation check, 436/438 on the full suite). Its extra value over this PR is the test cases: it covers the non-ASCII path and a surrogate pair, where you cover padding (%5ls, %-3c, %lc) and an exact fit. That is the only real difference between the two diffs. Bringing those cases over here — or having the maintainer take this one and #1511 withdraw — keeps every case and leaves one thing to review instead of two.

One line of the checklist is worth revisiting given that: "no other open PR implements the same functions" is no longer true, and the honest version is what your Tested section already does — say which PR is the other half.

For the record, your TechnicalDebt.md handling reads correctly to me: keeping wcsrtombs in the entry because #1507 implements it. #1511's current edit of the same line says the opposite (wcsrtombs still passes guest strings), so that line will collide whichever of those two merges second.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6061540708 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1512#issuecomment-6061540708). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T14:00:43Z. --- Independent run on Linux, since your `Tested` section notes the WSL compile-without-CMake route — this is the same change through the real build, and then through CI's own test target. **Merged onto current `main` (`63c57d22`): no conflicts.** That is worth stating plainly, because in this pair you are the one that still merges cleanly. ``` ctest -R guest_wide_formatting → 1/1 passed full suite (ctest -j8) → 462/462 passed CachyOS, GCC 16.2.1, Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON ``` **The tests bite.** Implementation reverted to `main` (`Formatting.cpp`, `FormattingWide.cpp` back, `WindowsWideFormatting.hpp` restored) with your tests and registration kept: ``` Test #13: guest_wide_formatting ...***Failed (Subprocess aborted) snwprintf_s: swprintf output length ``` So your claim — the same test aborts against `main`'s `Formatting.cpp` — reproduces here with the CMake target and the full `libc.prx`, not only with the two translation units compiled together. **On the two halves of this change:** #1511 is the same `swprintf` fix, opened 17 seconds after this one, currently `CONFLICTING`, and it does carry an independent Windows run (`@popcoder565`: 5/5 on the three wide tests, with a mutation check, 436/438 on the full suite). Its extra value over this PR is the test cases: it covers the non-ASCII path and a surrogate pair, where you cover padding (`%5ls`, `%-3c`, `%lc`) and an exact fit. That is the only real difference between the two diffs. Bringing those cases over here — or having the maintainer take this one and #1511 withdraw — keeps every case and leaves one thing to review instead of two. **One line of the checklist is worth revisiting** given that: *"no other open PR implements the same functions"* is no longer true, and the honest version is what your `Tested` section already does — say which PR is the other half. For the record, your `TechnicalDebt.md` handling reads correctly to me: keeping `wcsrtombs` in the entry because #1507 implements it. #1511's current edit of the same line says the opposite (`wcsrtombs` still passes guest strings), so that line will collide whichever of those two merges second. Written with AI assistance.
Noface07 commented 2026-10-08 15:22:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Noface07 on 2026-10-08T15:22:46Z.


Closing as a duplicate of #1511.
Thanks @Dextroid17 for the independent Linux run and @klosixgaming-arch for the review.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6063170070 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1512#issuecomment-6063170070). Originally posted by [Noface07](https://github.com/Noface07) on 2026-10-08T15:22:46Z. --- Closing as a duplicate of #1511. Thanks @Dextroid17 for the independent Linux run and @klosixgaming-arch for the review.

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!1512
No description provided.