feat(libc): scan 16-bit wide conversions in the sscanf family on Linux #5606

Closed
Gashrod1 wants to merge 1 commit from upstream-pr/5606 into main
Gashrod1 commented 2026-10-14 00:56:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gashrod1 on 2026-10-14T00:56:11Z.


What

On Linux, sscanf, vsscanf, sscanf_s and vsscanf_s threw for the 16-bit wide conversions %ls, %lc, %l[, %S and %C (#3984), because glibc would store a 32-bit wchar_t per character into the guest's 16-bit buffer. Windows already handed them to its host, whose wchar_t is 16 bits.

When the format has such a conversion, these functions now go through the per-directive scanner the _s forms already use (ScanGuest). Each wide directive is scanned as its narrow form into a temporary buffer, then each byte is widened to one char16_t, as the C locale of FreeBSD 11's none.c converts a byte to the wide character of the same value. %ls and %l[ are terminated, %lc is not, and in the _s forms the capacity counts 16-bit characters (a string that does not fit stores an empty string, as for %s). Formats without a wide conversion still go to the host unchanged.

A wide conversion combined with a positional argument or the ' flag still throws, as ScanGuest handles neither. fscanf keeps throwing for wide conversions: it reads from a host stream and needs separate handling. On Windows, sscanf, vsscanf and fscanf still throw for %S and %C, which their format translation does not accept; that is unchanged here. The technical debt entry is updated accordingly.

guest_formatting replaces the Linux rejection checks with checks on both platforms for %ls, %lc with a width, %l[ through vsscanf, a byte above 0x7f, the _s capacity (fit, overflow, %lc), empty input and the remaining rejections, plus %S and %C on Linux. It aborts on main and passes with the change.

Tested

Linux (Arch, GCC), Release, BUILD_TESTING=ON, based on main ce5c83953: guest_formatting aborts on main and passes with the change (3 runs); full build and full ctest pass. Windows (WinLibs MinGW-w64 GCC 15.2.0 r7, NVIDIA RTX 5060), Release, BUILD_TESTING=ON: full build and full ctest pass (1111 tests, the same 8 skipped as on main; guest_formatting was rerun after restricting the %S/%C cases to Linux and passes, 3 runs). No title or homebrew run.

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:4851426727 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5606). Originally posted by [Gashrod1](https://github.com/Gashrod1) on 2026-10-14T00:56:11Z. --- ### What On Linux, `sscanf`, `vsscanf`, `sscanf_s` and `vsscanf_s` threw for the 16-bit wide conversions `%ls`, `%lc`, `%l[`, `%S` and `%C` (#3984), because glibc would store a 32-bit `wchar_t` per character into the guest's 16-bit buffer. Windows already handed them to its host, whose `wchar_t` is 16 bits. When the format has such a conversion, these functions now go through the per-directive scanner the `_s` forms already use (`ScanGuest`). Each wide directive is scanned as its narrow form into a temporary buffer, then each byte is widened to one `char16_t`, as the C locale of FreeBSD 11's [`none.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/locale/none.c) converts a byte to the wide character of the same value. `%ls` and `%l[` are terminated, `%lc` is not, and in the `_s` forms the capacity counts 16-bit characters (a string that does not fit stores an empty string, as for `%s`). Formats without a wide conversion still go to the host unchanged. A wide conversion combined with a positional argument or the `'` flag still throws, as `ScanGuest` handles neither. `fscanf` keeps throwing for wide conversions: it reads from a host stream and needs separate handling. On Windows, `sscanf`, `vsscanf` and `fscanf` still throw for `%S` and `%C`, which their format translation does not accept; that is unchanged here. The technical debt entry is updated accordingly. `guest_formatting` replaces the Linux rejection checks with checks on both platforms for `%ls`, `%lc` with a width, `%l[` through `vsscanf`, a byte above 0x7f, the `_s` capacity (fit, overflow, `%lc`), empty input and the remaining rejections, plus `%S` and `%C` on Linux. It aborts on `main` and passes with the change. ### Tested Linux (Arch, GCC), Release, `BUILD_TESTING=ON`, based on `main` `ce5c83953`: `guest_formatting` aborts on `main` and passes with the change (3 runs); full build and full `ctest` pass. Windows (WinLibs MinGW-w64 GCC 15.2.0 r7, NVIDIA RTX 5060), Release, `BUILD_TESTING=ON`: full build and full `ctest` pass (1111 tests, the same 8 skipped as on `main`; `guest_formatting` was rerun after restricting the `%S`/`%C` cases to Linux and passes, 3 runs). No title or homebrew run. ### 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
github-actions[bot] commented 2026-10-14 01:09:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T01:09:21Z.


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
#3987 core/libs/tests/Formatting.cpp L247-255
#4966 core/libs/prx/libc/src/Formatting.cpp L396
#5285 core/libs/prx/libc/src/Formatting.cpp L374, L393, L396
docs/dev/debt/libc.md L56
#5291 core/libs/prx/libc/src/Formatting.cpp L98-99, L374, L393, L396
docs/dev/debt/libc.md L56
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158606166 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5606#issuecomment-6158606166). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T01:09:21Z. --- <!-- 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 | |---|---|---|---| | #3987 | `core/libs/tests/Formatting.cpp` [L247-255](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/tests/Formatting.cpp#L247-L255) | | | | #4966 | `core/libs/prx/libc/src/Formatting.cpp` [L396](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libc/src/Formatting.cpp#L396) | | | | #5285 | `core/libs/prx/libc/src/Formatting.cpp` [L374](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libc/src/Formatting.cpp#L374), [L393](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libc/src/Formatting.cpp#L393), [L396](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libc/src/Formatting.cpp#L396)<br>`docs/dev/debt/libc.md` [L56](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/docs/dev/debt/libc.md#L56) | | | | #5291 | `core/libs/prx/libc/src/Formatting.cpp` [L98-99](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libc/src/Formatting.cpp#L98-L99), [L374](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libc/src/Formatting.cpp#L374), [L393](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libc/src/Formatting.cpp#L393), [L396](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/core/libs/prx/libc/src/Formatting.cpp#L396)<br>`docs/dev/debt/libc.md` [L56](https://github.com/boykopovar/AnyPS5/blob/71c71f50b7a42ad1a62b78d9cd24f5a356965557/docs/dev/debt/libc.md#L56) | | |
oneandonlydean commented 2026-10-14 02:24:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:24:43Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159493288 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5606#issuecomment-6159493288). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:24:43Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

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