fix(libc): encode each UTF-16 unit of a narrow %ls on its own, as titles' libc.prx does #9939

Closed
JJenkx wants to merge 2 commits from upstream-pr/9939 into main
JJenkx commented 2026-10-22 22:56:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T22:56:21Z.


What

The narrow printf family (snprintf and the rest) converts a 16-bit guest string given to %ls or %S to UTF-8. It joined a surrogate pair into one 4-byte sequence. The libc.prx that titles ship encodes characters in at most 3 bytes, as the console's C locale does (#8855 measures the same 3-byte limit in the firmware's multibyte functions). It therefore writes each surrogate as its own 3-byte sequence:

Call Console main
[%ls] of U+1F600 5B ED A0 BD ED B8 80 5D (8) 5B F0 9F 98 80 5D (6)
%.3ls of U+1F600 ED A0 BD (3) empty
%.10ls of A é € U+1F600 Z A é € and ED A0 BD A é € and F0 9F 98 80
[%-9S] of U+1F600 the 6 bytes and 3 spaces the 4 bytes and 5 spaces

This removes the line in FormatWindows that joined the pair. Every code unit, a surrogate included, now takes the existing 1-to-3-byte path, and the precision still keeps whole code units. Lone surrogates already matched the console. The debt entry no longer calls this unverified.

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main bd1d3994ce.

  • Differential run: a scratch loader runs PPSA07923's decrypted libc.prx natively and compares its snprintf with this build's snprintf_nid_postfix. It uses [%ls], [%.2ls] and [%5ls] on ten strings: ASCII, é, €, U+1F600, a lone high surrogate, a lone low surrogate, a high surrogate followed by A, U+FFFF, U+FFFE, U+0080, and U+07FF U+0800. It also tests [%lc] on six values, and %.*ls with precision 0–13 on A é € U+1F600 Z. On main, the U+1F600 cases differ. With this change, every case gives the same bytes and count.
  • guest_formatting: CheckWidePrecision expected the 4-byte form, and its expectations for precisions 9, 10 and 11 and for %.3ls of U+1F600 now use the console's results. There are two new checks, [%ls] and [%-9S] of U+1F600. The libc formatting, scanning, stream, wide-character and string tests (34) pass in three runs out of three. With main's WindowsFormatting.hpp and this test, guest_formatting fails.
  • python tools/check_conventions.py --base origin/main and git diff --check pass.
  • Overlap: #9927 shares WindowsFormatting.hpp, core/libs/tests/Formatting.cpp and docs/dev/debt/libc.md, while its behavior is distinct. This PR removes surrogate-pair joining in the narrow wide-string conversion and updates CheckWidePrecision; #9927 adds floating-point formatting paths and CheckHexFloat/CheckDecimalFloat, plus separate wide-float tests. In either landing order, retain the per-code-unit conversion and all updated/new string expectations here, both floating-point test calls and cases, the separate wide-float implementation/tests, and both debt descriptions. #8855, #9552, #9699 and #9916 also edit neighbouring debt text; reconcile their independent locale, argument-limit and floating-point notes. This is a review of the submitted diffs, not a combined build or test result; Depends on remains none.
  • Independent Linux review: Dom3005's report tested head c2bde4fe3 merged cleanly onto main fcd455870: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. The reviewer reports guest_formatting and guest_wide_formatting each passing five runs (--repeat until-fail:5). With this PR's test retained and main's changed production sources restored, the reviewer reports a failure: wide precision 9: expected 9 complete UTF-8 bytes, received 6. This is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim.
  • Not tested: a console, the wide printf family's %s (the other direction).

Checklist

  • Based on current main; no other open PR changes how %ls encodes
  • 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 under Silent stubs in their library's technical debt file
  • 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: none
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4975238255 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9939). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T22:56:21Z. --- ### What The narrow printf family (`snprintf` and the rest) converts a 16-bit guest string given to `%ls` or `%S` to UTF-8. It joined a surrogate pair into one 4-byte sequence. The `libc.prx` that titles ship encodes characters in at most 3 bytes, as the console's C locale does (#8855 measures the same 3-byte limit in the firmware's multibyte functions). It therefore writes each surrogate as its own 3-byte sequence: | Call | Console | `main` | |---|---|---| | `[%ls]` of U+1F600 | `5B ED A0 BD ED B8 80 5D` (8) | `5B F0 9F 98 80 5D` (6) | | `%.3ls` of U+1F600 | `ED A0 BD` (3) | empty | | `%.10ls` of `A é € U+1F600 Z` | `A é €` and `ED A0 BD` | `A é €` and `F0 9F 98 80` | | `[%-9S]` of U+1F600 | the 6 bytes and 3 spaces | the 4 bytes and 5 spaces | This removes the line in `FormatWindows` that joined the pair. Every code unit, a surrogate included, now takes the existing 1-to-3-byte path, and the precision still keeps whole code units. Lone surrogates already matched the console. The debt entry no longer calls this unverified. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` bd1d3994ce. - **Differential run:** a scratch loader runs PPSA07923's decrypted `libc.prx` natively and compares its `snprintf` with this build's `snprintf_nid_postfix`. It uses `[%ls]`, `[%.2ls]` and `[%5ls]` on ten strings: ASCII, `é`, `€`, U+1F600, a lone high surrogate, a lone low surrogate, a high surrogate followed by `A`, U+FFFF, U+FFFE, U+0080, and U+07FF U+0800. It also tests `[%lc]` on six values, and `%.*ls` with precision 0–13 on `A é € U+1F600 Z`. On `main`, the U+1F600 cases differ. With this change, every case gives the same bytes and count. - **`guest_formatting`:** `CheckWidePrecision` expected the 4-byte form, and its expectations for precisions 9, 10 and 11 and for `%.3ls` of U+1F600 now use the console's results. There are two new checks, `[%ls]` and `[%-9S]` of U+1F600. The libc formatting, scanning, stream, wide-character and string tests (34) pass in three runs out of three. With `main`'s `WindowsFormatting.hpp` and this test, `guest_formatting` fails. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Overlap:** #9927 shares `WindowsFormatting.hpp`, `core/libs/tests/Formatting.cpp` and `docs/dev/debt/libc.md`, while its behavior is distinct. This PR removes surrogate-pair joining in the narrow wide-string conversion and updates `CheckWidePrecision`; #9927 adds floating-point formatting paths and `CheckHexFloat`/`CheckDecimalFloat`, plus separate wide-float tests. In either landing order, retain the per-code-unit conversion and all updated/new string expectations here, both floating-point test calls and cases, the separate wide-float implementation/tests, and both debt descriptions. #8855, #9552, #9699 and #9916 also edit neighbouring debt text; reconcile their independent locale, argument-limit and floating-point notes. This is a review of the submitted diffs, not a combined build or test result; Depends on remains none. - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9939#issuecomment-6353483877) tested head `c2bde4fe3` merged cleanly onto `main` `fcd455870`: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, `BUILD_TESTING=ON`. The reviewer reports `guest_formatting` and `guest_wide_formatting` each passing five runs (`--repeat until-fail:5`). With this PR's test retained and main's changed production sources restored, the reviewer reports a failure: `wide precision 9: expected 9 complete UTF-8 bytes, received 6`. This is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim. - **Not tested:** a console, the wide printf family's `%s` (the other direction). ### Checklist - [x] Based on current `main`; no other open PR changes how `%ls` encodes - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: none - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-23 05:45:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T05:45:22Z.

Review: APPROVED


Reviewed exact head c2bde4fe30659f922238104ca2302cc4857d42d6. The per-UTF-16-code-unit conversion matches the contributor’s differential against a title-shipped libc.prx; the added precision and surrogate expectations exercise the changed semantics, with Linux WSL and Windows runs reported and an independent WSL reviewer confirming 5/5 repetitions and a failing-main control. Current-main diff-check and conventions pass, and the branch merges cleanly; the intervening 295 main commits do not touch the three changed paths. Hosted policy and relinker matrix checks pass; full product builds are skipped. I did not rerun the C++ suite locally. Console/title generalization beyond the measured libc and untested %s direction remain appropriately documented.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5624399423 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9939#pullrequestreview-5624399423). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T05:45:22Z. **Review:** APPROVED --- Reviewed exact head `c2bde4fe30659f922238104ca2302cc4857d42d6`. The per-UTF-16-code-unit conversion matches the contributor’s differential against a title-shipped `libc.prx`; the added precision and surrogate expectations exercise the changed semantics, with Linux WSL and Windows runs reported and an independent WSL reviewer confirming 5/5 repetitions and a failing-main control. Current-main diff-check and conventions pass, and the branch merges cleanly; the intervening 295 main commits do not touch the three changed paths. Hosted policy and relinker matrix checks pass; full product builds are skipped. I did not rerun the C++ suite locally. Console/title generalization beyond the measured libc and untested `%s` direction remain appropriately documented.

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