fix(libc): end sscanf and vsscanf at a q length on Linux and macOS #11852

Merged
ionut27001 merged 3 commits from upstream-pr/11852 into main 2026-10-29 12:57:19 +00:00
ionut27001 commented 2026-10-27 09:51:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ionut27001 on 2026-10-27T09:51:15Z.


What

On Linux and macOS, sscanf and vsscanf passed a q length straight to the host libc, which reads it as ll: sscanf("5", "%qd", &q) stored 5 and returned 1. On Windows the scan already ends at the q directive with nothing stored (WindowsScanning.hpp), so the hosts differed.

Evidence is our own payload probe on a PS5 (firmware 12.70, sscanf resolved from libSceLibcInternal.sprx). It was not run in a title process. Case lines:

case scan input="ab cd" fmt="%qS" ret=0 units=!!!!!!!!
case scan input="ab" fmt="%qC" ret=0 units=!!!!!!!!
case scan input="xy -5000000000" fmt="%ls %qd%qn" ret=1 large=0 count=-1 units=xy\^@!!!!!

%qd stored nothing and %qn was not reached, so the scan ended at the first q directive. That probe did not have a plain narrow %qd case without a preceding wide field.

This change adds LibcDetail::FindQuadScanDirective, which finds the first %q directive the same way the Windows path does: it skips %%, * and width digits. On non-Windows hosts sscanf and vsscanf cut the format before that directive, before choosing between the host libc and the in-process wide scanner. The format is copied only when a q is present.

Only sscanf and vsscanf change. sscanf was measured; vsscanf was not, and follows sscanf as it already did on the Windows path. HostVsscanf and HostVfscanf are unchanged, so fscanf, vfscanf, scanf, vscanf and the _s forms keep their previous behaviour. That is recorded in the debt entry.

Overlaps: open scanf PRs #7386, #11724, #11726, #9340, #9087 and #9863 have no added lines that handle q. #5303 mentions %qd for printf only.

Tested

The changed code in sscanf and vsscanf is compiled only on Linux and macOS. It was not built or run here; nothing was run on Linux, macOS or with a title.

Windows 11, WinLibs GCC 15.2.0:

  • With this change: guest_formatting and guest_wide_formatting pass twice. On Windows this runs the existing Windows scan path and the new FindQuadScanDirective header, not the changed Linux/macOS code. The %qd check is no longer limited to Windows. It also checks that "%d %qd" on "7 5" returns 1 with only the first value stored through both sscanf and vsscanf, that %qS returns 0 with nothing stored, that %%qd is matched as literal text, and where FindQuadScanDirective stops.
  • Revert check: with only Formatting.cpp reverted, the test still passes on Windows, so no Windows run shows the fix. Removing the existing q cut from WindowsScanning.hpp makes guest_formatting fail, so the new checks catch a scan that does not stop at q.

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5031892406 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11852). Originally posted by [ionut27001](https://github.com/ionut27001) on 2026-10-27T09:51:15Z. --- ### What On Linux and macOS, `sscanf` and `vsscanf` passed a `q` length straight to the host libc, which reads it as `ll`: `sscanf("5", "%qd", &q)` stored 5 and returned 1. On Windows the scan already ends at the `q` directive with nothing stored (`WindowsScanning.hpp`), so the hosts differed. Evidence is our own payload probe on a PS5 (firmware 12.70, `sscanf` resolved from `libSceLibcInternal.sprx`). It was not run in a title process. Case lines: ``` case scan input="ab cd" fmt="%qS" ret=0 units=!!!!!!!! case scan input="ab" fmt="%qC" ret=0 units=!!!!!!!! case scan input="xy -5000000000" fmt="%ls %qd%qn" ret=1 large=0 count=-1 units=xy\^@!!!!! ``` `%qd` stored nothing and `%qn` was not reached, so the scan ended at the first `q` directive. That probe did not have a plain narrow `%qd` case without a preceding wide field. This change adds `LibcDetail::FindQuadScanDirective`, which finds the first `%q` directive the same way the Windows path does: it skips `%%`, `*` and width digits. On non-Windows hosts `sscanf` and `vsscanf` cut the format before that directive, before choosing between the host libc and the in-process wide scanner. The format is copied only when a `q` is present. Only `sscanf` and `vsscanf` change. `sscanf` was measured; `vsscanf` was not, and follows `sscanf` as it already did on the Windows path. `HostVsscanf` and `HostVfscanf` are unchanged, so `fscanf`, `vfscanf`, `scanf`, `vscanf` and the `_s` forms keep their previous behaviour. That is recorded in the debt entry. Overlaps: open scanf PRs #7386, #11724, #11726, #9340, #9087 and #9863 have no added lines that handle `q`. #5303 mentions `%qd` for printf only. ### Tested The changed code in `sscanf` and `vsscanf` is compiled only on Linux and macOS. It was not built or run here; nothing was run on Linux, macOS or with a title. Windows 11, WinLibs GCC 15.2.0: - **With this change:** `guest_formatting` and `guest_wide_formatting` pass twice. On Windows this runs the existing Windows scan path and the new `FindQuadScanDirective` header, not the changed Linux/macOS code. The `%qd` check is no longer limited to Windows. It also checks that `"%d %qd"` on `"7 5"` returns 1 with only the first value stored through both `sscanf` and `vsscanf`, that `%qS` returns 0 with nothing stored, that `%%qd` is matched as literal text, and where `FindQuadScanDirective` stops. - **Revert check:** with only `Formatting.cpp` reverted, the test still passes on Windows, so no Windows run shows the fix. Removing the existing `q` cut from `WindowsScanning.hpp` makes `guest_formatting` fail, so the new checks catch a scan that does not stop at `q`. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-27 10:42:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T10:42:55Z.

Review: DISMISSED


I configured and built the exact head on Linux with the repository-pinned submodules; guest_formatting and guest_wide_formatting pass. As a negative control, I restored only Formatting.cpp from the PR base: guest_formatting then fails at the new %qd assertion, and it passes again after restoring the PR file. This confirms the Linux regression test exercises the changed path.

One behavior-scope issue remains. The console evidence is from sscanf only, and the PR explicitly records fscanf and sscanf_s as unmeasured. The shared host changes also affect fscanf/vfscanf/scanf/vscanf through HostVfscanf, and secure variants through HostSscanf/HostFscanf called by ScanGuest; these functions now all stop at %q on Linux/macOS. The repository rule requires unknown behavior to remain an explicit failure until it is measured, rather than assuming all entry points share the observed console behavior. Please either provide console evidence for the affected scan variants and record any remaining gaps, or keep the unmeasured variants on an explicit unsupported path until then. I could not run the macOS build/runtime test on this Linux host; the PR’s Windows test does not compile the changed non-Windows branch.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5665754061 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11852#pullrequestreview-5665754061). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T10:42:55Z. **Review:** DISMISSED --- I configured and built the exact head on Linux with the repository-pinned submodules; `guest_formatting` and `guest_wide_formatting` pass. As a negative control, I restored only `Formatting.cpp` from the PR base: `guest_formatting` then fails at the new `%qd` assertion, and it passes again after restoring the PR file. This confirms the Linux regression test exercises the changed path. One behavior-scope issue remains. The console evidence is from `sscanf` only, and the PR explicitly records `fscanf` and `sscanf_s` as unmeasured. The shared host changes also affect `fscanf`/`vfscanf`/`scanf`/`vscanf` through `HostVfscanf`, and secure variants through `HostSscanf`/`HostFscanf` called by `ScanGuest`; these functions now all stop at `%q` on Linux/macOS. The repository rule requires unknown behavior to remain an explicit failure until it is measured, rather than assuming all entry points share the observed console behavior. Please either provide console evidence for the affected scan variants and record any remaining gaps, or keep the unmeasured variants on an explicit unsupported path until then. I could not run the macOS build/runtime test on this Linux host; the PR’s Windows test does not compile the changed non-Windows branch.
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!11852
No description provided.