fix(libc): format 16-bit wide arguments in asprintf on Linux #3978

Merged
zmeizy merged 1 commit from upstream-pr/3978 into main 2026-10-12 03:11:48 +00:00
zmeizy commented 2026-10-11 20:26:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by zmeizy on 2026-10-11T20:26:57Z.


What

On Linux, the narrow printf functions send a format that has a guest wide conversion (%ls, %lc, %S, %C) to the guest formatter, which reads 16-bit characters. Only asprintf (AllocatingStrings.cpp) gave every format to the host's vasprintf. The host reads a 32-bit wchar_t per character, so a guest UTF-16 string came out wrong, or the call failed. Windows was not affected.

asprintf now forwards its arguments to vasprintf, as fprintf does with vfprintf. vasprintf already chooses the formatter and copies the result into the application heap. The null-argument checks and the out-of-memory result are the same as before.

Tested

Ubuntu 24.04, GCC 13.3, on main 12c22662.

  • guest_formatting: a new check registers an application heap and calls asprintf("%ls:%S:%lc:%d", u"é", u"ab", 0x20ac, 7). It expects 11 and the UTF-8 text é:ab:€:7, then frees the result with the guest free.
  • The check fails on main without the change and passes with it.
  • guest_wide_formatting passes.
  • Windows was not built. Its path calls vasprintf, which makes the same FormatWindows call that asprintf made before.

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 (n/a)
  • 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 (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ha6Tsy62AFUyp9a5Mf7vjA

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4822097949 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3978). Originally posted by [zmeizy](https://github.com/zmeizy) on 2026-10-11T20:26:57Z. --- ### What On Linux, the narrow printf functions send a format that has a guest wide conversion (`%ls`, `%lc`, `%S`, `%C`) to the guest formatter, which reads 16-bit characters. Only `asprintf` (`AllocatingStrings.cpp`) gave every format to the host's `vasprintf`. The host reads a 32-bit `wchar_t` per character, so a guest UTF-16 string came out wrong, or the call failed. Windows was not affected. `asprintf` now forwards its arguments to `vasprintf`, as `fprintf` does with `vfprintf`. `vasprintf` already chooses the formatter and copies the result into the application heap. The null-argument checks and the out-of-memory result are the same as before. ### Tested Ubuntu 24.04, GCC 13.3, on `main` `12c22662`. - `guest_formatting`: a new check registers an application heap and calls `asprintf("%ls:%S:%lc:%d", u"é", u"ab", 0x20ac, 7)`. It expects 11 and the UTF-8 text `é:ab:€:7`, then frees the result with the guest `free`. - The check fails on `main` without the change and passes with it. - `guest_wide_formatting` passes. - Windows was not built. Its path calls `vasprintf`, which makes the same `FormatWindows` call that `asprintf` made before. ### 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](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) (n/a) - [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 (n/a) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Ha6Tsy62AFUyp9a5Mf7vjA
github-actions[bot] commented 2026-10-11 21:13:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T21:13:50Z.


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
#3982 core/libs/tests/Formatting.cpp L1-2
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6113822484 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3978#issuecomment-6113822484). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T21:13:50Z. --- <!-- 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 | |---|---|---|---| | #3982 | `core/libs/tests/Formatting.cpp` [L1-2](https://github.com/boykopovar/AnyPS5/blob/9eb1486e3981facdd8223497a79077e8769f50f3/core/libs/tests/Formatting.cpp#L1-L2) | | |
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!3978
No description provided.