feat(libc): read 16-bit wide fields in fscanf on Linux #11724

Closed
cocoa55 wants to merge 5 commits from upstream-pr/11724 into main
cocoa55 commented 2026-10-27 05:48:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cocoa55 on 2026-10-27T05:48:45Z.


What

On Linux, fscanf, vfscanf, scanf and vscanf threw NotImplemented on the 16-bit wide conversions (%ls, %lc, %l[, %S, %C), and so did the _s stream forms. sscanf already reads them with the guest scanner (ScanSource), which also has a StreamSource, but its wide branch only ran for contiguous strings (if constexpr (TSource::Contiguous)). The UTF-8 decoding is the existing sscanf behaviour; #12468 replaces it with the byte-by-byte rule measured on titles’ libc.prx.

  • The wide branch of ScanSource no longer walks a const char*. Each source gets PeekWide, which decodes the next character without consuming it, and the branch calls Skip for its bytes only once the field takes the character. StringSource::PeekWide is the old ReadWideScanCharacter call. StreamSource::PeekWide reads the UTF-8 sequence with fgetc and pushes it back with ungetc, as the fwscanf stream reader in ScanningWide.cpp already does. A rejected character therefore stays in the stream, and the accept/reject logic is the same code for strings and streams.
  • fscanf/vfscanf use the scanner when the format has a wide conversion (same check as sscanf, ScansWideInProcess) and the host vfscanf otherwise. ScanGuestStream (fscanf_s and friends) no longer rejects wide conversions and keeps the positional-argument check.
  • RejectGuestWideScan is removed. ScanWideField gets WideScanCharacterLength, which DecodeUtf8 now uses too.
  • docs/dev/debt/libc.md: the scanf family and _s stream entries no longer list fscanf as throwing.

Overlap: #9087 and #9340 change the same wide branch of ScanSource (secure-size violations, and surrogate pairs / non-ASCII scansets). Neither reads wide fields from a stream, so they don't duplicate this PR. Agreed order (asked on both PRs): #9087, then #9340, then this PR, which I'll rebase after each one merges. StringSource::Remaining is kept because #12530 uses it. #11726 and #12468 build on this PR.

Tested

Ubuntu 24.04, GCC 13.3, full build. New CheckNarrowFormatWideFields in core/libs/tests/GuestWideStreamScan.cpp (Linux only, like the sscanf wide tests): fscanf %ls/%lc on UTF-8, vfscanf %S with %ln, wide and narrow fields in one call, a scanset stopping before é and the é read by the next call, %3lc with the next character left in the stream, matching failure and EOF, fscanf_s/vfscanf_s sizes, scanf/vscanf on the guest stdin. It fails on main with fscanf 16-bit wide conversion on Linux not implemented. guest_formatting, guest_scan_count, guest_wide_stream_scan and file_streams pass. Full ctest: 2476 of 2491 pass, and the 15 failures (agc_* submit/metadata, guest_net_*, guest_tcp, guest_socket_*) fail the same way on main in the same container (no GPU, sandboxed network). 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 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
  • Generic behaviour, not specific to one title
  • Depends on: #9087, #9340
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5029268603 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11724). Originally posted by [cocoa55](https://github.com/cocoa55) on 2026-10-27T05:48:45Z. --- ### What On Linux, `fscanf`, `vfscanf`, `scanf` and `vscanf` threw `NotImplemented` on the 16-bit wide conversions (`%ls`, `%lc`, `%l[`, `%S`, `%C`), and so did the `_s` stream forms. `sscanf` already reads them with the guest scanner (`ScanSource`), which also has a `StreamSource`, but its wide branch only ran for contiguous strings (`if constexpr (TSource::Contiguous)`). The UTF-8 decoding is the existing sscanf behaviour; #12468 replaces it with the byte-by-byte rule measured on titles’ libc.prx. - The wide branch of `ScanSource` no longer walks a `const char*`. Each source gets `PeekWide`, which decodes the next character without consuming it, and the branch calls `Skip` for its bytes only once the field takes the character. `StringSource::PeekWide` is the old `ReadWideScanCharacter` call. `StreamSource::PeekWide` reads the UTF-8 sequence with `fgetc` and pushes it back with `ungetc`, as the `fwscanf` stream reader in `ScanningWide.cpp` already does. A rejected character therefore stays in the stream, and the accept/reject logic is the same code for strings and streams. - `fscanf`/`vfscanf` use the scanner when the format has a wide conversion (same check as `sscanf`, `ScansWideInProcess`) and the host `vfscanf` otherwise. `ScanGuestStream` (`fscanf_s` and friends) no longer rejects wide conversions and keeps the positional-argument check. - `RejectGuestWideScan` is removed. `ScanWideField` gets `WideScanCharacterLength`, which `DecodeUtf8` now uses too. - `docs/dev/debt/libc.md`: the scanf family and `_s` stream entries no longer list `fscanf` as throwing. Overlap: #9087 and #9340 change the same wide branch of `ScanSource` (secure-size violations, and surrogate pairs / non-ASCII scansets). Neither reads wide fields from a stream, so they don't duplicate this PR. Agreed order (asked on both PRs): #9087, then #9340, then this PR, which I'll rebase after each one merges. StringSource::Remaining is kept because #12530 uses it. #11726 and #12468 build on this PR. ### Tested Ubuntu 24.04, GCC 13.3, full build. New `CheckNarrowFormatWideFields` in `core/libs/tests/GuestWideStreamScan.cpp` (Linux only, like the `sscanf` wide tests): `fscanf` `%ls`/`%lc` on UTF-8, `vfscanf` `%S` with `%ln`, wide and narrow fields in one call, a scanset stopping before `é` and the `é` read by the next call, `%3lc` with the next character left in the stream, matching failure and EOF, `fscanf_s`/`vfscanf_s` sizes, `scanf`/`vscanf` on the guest stdin. It fails on `main` with `fscanf 16-bit wide conversion on Linux not implemented`. `guest_formatting`, `guest_scan_count`, `guest_wide_stream_scan` and `file_streams` pass. Full `ctest`: 2476 of 2491 pass, and the 15 failures (`agc_*` submit/metadata, `guest_net_*`, `guest_tcp`, `guest_socket_*`) fail the same way on `main` in the same container (no GPU, sandboxed network). 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](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 - [x] Generic behaviour, not specific to one title - [x] Depends on: #9087, #9340 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-27 06:00:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T06:00:50Z.

Review: CHANGES_REQUESTED


I reviewed the scanner changes and the reported build/test results. git diff --check and the current-base conventions check pass. The author reports a full Ubuntu build, the focused scanner tests, and a main-vs-change control for the wider CTest failures; hosted product build jobs are currently skipped.

There is one repository-policy blocker before this can be approved. Both overlapping PRs #9087 and #9340 are still open, and I independently confirmed that merging this head with either branch produces a content conflict in core/libs/prx/libc/src/Formatting.cpp (#9340 also conflicts in docs/dev/debt/libc.md). CONTRIBUTING requires the authors to agree which overlapping PR goes first and declare that order with Depends on. The PR description identifies the overlaps and says the second merge will need a textual rebase, but its checklist still says Depends on: none, and I found no coordination with those authors in the PR discussion.

Please coordinate the merge order with the authors of #9087 and #9340, update the dependency line accordingly, and rebase when appropriate. This is a merge-order request; I did not find a separate correctness blocker in the reviewed code.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5662861218 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11724#pullrequestreview-5662861218). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T06:00:50Z. **Review:** CHANGES_REQUESTED --- I reviewed the scanner changes and the reported build/test results. `git diff --check` and the current-base conventions check pass. The author reports a full Ubuntu build, the focused scanner tests, and a main-vs-change control for the wider CTest failures; hosted product build jobs are currently skipped. There is one repository-policy blocker before this can be approved. Both overlapping PRs #9087 and #9340 are still open, and I independently confirmed that merging this head with either branch produces a content conflict in `core/libs/prx/libc/src/Formatting.cpp` (`#9340` also conflicts in `docs/dev/debt/libc.md`). CONTRIBUTING requires the authors to agree which overlapping PR goes first and declare that order with `Depends on`. The PR description identifies the overlaps and says the second merge will need a textual rebase, but its checklist still says `Depends on: none`, and I found no coordination with those authors in the PR discussion. Please coordinate the merge order with the authors of #9087 and #9340, update the dependency line accordingly, and rebase when appropriate. This is a merge-order request; I did not find a separate correctness blocker in the reviewed code.

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