fix(libc): retain short stream character scan fields #13057

Merged
JJenkx merged 3 commits from upstream-pr/13057 into main 2026-10-30 10:43:02 +00:00
JJenkx commented 2026-10-29 23:58:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-29T23:58:56Z.


What

For a temporary stream containing a single byte, %2c%n in the bundled libc returns1, stores that byte, and writes1 to %n. The current secure stream scanner writes the byte but reports EOF and leaves %n untouched. Read narrow character fields directly from StreamSource so any nonempty short field counts as a conversion, suppressed fields advance the count without an assignment, and exact-width fields leave the following input available. Preserve capacity validation, UTF16 paths and all string paths.

Scope avoids the contiguous-string implementation in #9087, whose bounded string path already handles part of this case. No dependency on that PR; its stream fallback is unchanged. The ordinary host-backed fscanf/vfscanf short-field gap is still outside this change.

Hold the stream's recursive file lock throughout secure scanning so another reader cannot interleave between the character-field peek and consume operations. The lock is released on return and exception paths.

Tested

Linux x86_64 Release; final head ff25c7aba5f6a53215385cae3627b00556c4dd99, original base f0c8f26eec6edd346338e7c2d7f060ece52e8f50. All four focused suites (guest_formatting, guest_wide_formatting, guest_safe_errors, file_streams) execute and pass with zero skips. The final header-only placement adjustment leaves both the qualified production library and regression-test executable byte-identical.

The new concurrency regression runs both fscanf_s and vfscanf_s over a 1MiB character field. A second thread uses ftrylockfile and ftell to detect a lock becoming available before the field ends; it also checks lock release after return, complete output, %n and destination guards. It fails at the intended lock assertion with initial PR head 63af66d6c5e. With unchanged main it passes the locking check and then fails the existing short-field assertion. Both assertions pass with this head. This is a host/HLE regression check; bundled SDK multithreaded execution is not claimed.

The original two bundled libc modules agree on all 2,880 authored single-thread rows across sscanf/sscanf_s/fscanf/fscanf_s. Secure stream differences remain 165/720 →24/720, correcting 141 complete rows. After the locking correction, all2,880 outputs remain byte-for-byte identical to the previously qualified PR outputs. The other three API matrices are unchanged; the24 remaining secure stream rows concern literal-prefix exhaustion. No title speed, graphics, native game, GPU, Windows/macOS local or console validation claimed.

Conventions pass. Merge preview is clean against pinned current main f6fddd60d826831f70f7c62e25423610ee742bcc; Formatting.cpp, FileStreams.cpp, FileStream.hpp and RuntimeSupport.cpp are unchanged on main since the tested base. The1449-PR overlap scan found one include-placement collision with #13049; moving the includes resolves the current-main pairwise merge without changing either tested binary. The9 already-conflicted shared-file peers were manually reviewed as before.

Locking correction, exact pins, controls, full focused logs/JUnit and overlap resolution, SHA256 f1e7c08f61d6d91ce27a3c3a3334345750f0b1adf72b7f4b67311af00d11d39b. Original two-reference authored probe and synthetic rows, SHA256 0bef305e812549fd8408a9cb41123548c9f3ae5be70f73cb7b4c5eb60b0a421a. No SDK/game binaries or captured game data included.

Independent Windows validation reported by bunbeans2-arch for exact topic ff25c7aba5f6a53215385cae3627b00556c4dd99 merged into main f7f03daf52: Windows 11 Pro 22631, i9-9900K, MSYS2 UCRT64 GCC 16.1, Release. file_streams passes 5/5; keeping the new test while restoring Formatting.cpp to main fails 5/5 at the assertion that a short character scan retains the file lock through the field. Plain main with its own source/test passes 3/3. This is the contributor's run, not a new local Windows execution.

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; library compatibility only
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5077307925 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13057). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-29T23:58:56Z. --- ### What For a temporary stream containing a single byte, `%2c%n` in the bundled libc returns1, stores that byte, and writes1 to `%n`. The current secure stream scanner writes the byte but reports EOF and leaves `%n` untouched. Read narrow character fields directly from StreamSource so any nonempty short field counts as a conversion, suppressed fields advance the count without an assignment, and exact-width fields leave the following input available. Preserve capacity validation, UTF16 paths and all string paths. Scope avoids the contiguous-string implementation in #9087, whose bounded string path already handles part of this case. No dependency on that PR; its stream fallback is unchanged. The ordinary host-backed fscanf/vfscanf short-field gap is still outside this change. Hold the stream's recursive file lock throughout secure scanning so another reader cannot interleave between the character-field peek and consume operations. The lock is released on return and exception paths. ### Tested Linux x86_64 Release; final head `ff25c7aba5f6a53215385cae3627b00556c4dd99`, original base `f0c8f26eec6edd346338e7c2d7f060ece52e8f50`. All four focused suites (guest_formatting, guest_wide_formatting, guest_safe_errors, file_streams) execute and pass with zero skips. The final header-only placement adjustment leaves both the qualified production library and regression-test executable byte-identical. The new concurrency regression runs both fscanf_s and vfscanf_s over a 1MiB character field. A second thread uses ftrylockfile and ftell to detect a lock becoming available before the field ends; it also checks lock release after return, complete output, `%n` and destination guards. It fails at the intended lock assertion with initial PR head `63af66d6c5e`. With unchanged main it passes the locking check and then fails the existing short-field assertion. Both assertions pass with this head. This is a host/HLE regression check; bundled SDK multithreaded execution is not claimed. The original two bundled libc modules agree on all **2,880** authored single-thread rows across sscanf/sscanf_s/fscanf/fscanf_s. Secure stream differences remain **165/720 →24/720**, correcting **141 complete rows**. After the locking correction, all2,880 outputs remain byte-for-byte identical to the previously qualified PR outputs. The other three API matrices are unchanged; the24 remaining secure stream rows concern literal-prefix exhaustion. No title speed, graphics, native game, GPU, Windows/macOS local or console validation claimed. Conventions pass. Merge preview is clean against pinned current main `f6fddd60d826831f70f7c62e25423610ee742bcc`; Formatting.cpp, FileStreams.cpp, FileStream.hpp and RuntimeSupport.cpp are unchanged on main since the tested base. The1449-PR overlap scan found one include-placement collision with #13049; moving the includes resolves the current-main pairwise merge without changing either tested binary. The9 already-conflicted shared-file peers were manually reviewed as before. [Locking correction, exact pins, controls, full focused logs/JUnit and overlap resolution](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/libc-short-stream-scan-locking-linux-20261029-final.tar.gz), SHA256 `f1e7c08f61d6d91ce27a3c3a3334345750f0b1adf72b7f4b67311af00d11d39b`. Original [two-reference authored probe and synthetic rows](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/libc-short-stream-character-fields-linux-20261029.tar.gz), SHA256 `0bef305e812549fd8408a9cb41123548c9f3ae5be70f73cb7b4c5eb60b0a421a`. No SDK/game binaries or captured game data included. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Independent Windows validation reported by [bunbeans2-arch](https://github.com/boykopovar/AnyPS5/pull/13057#issuecomment-6518264155) for exact topic `ff25c7aba5f6a53215385cae3627b00556c4dd99` merged into main `f7f03daf52`: Windows 11 Pro 22631, i9-9900K, MSYS2 UCRT64 GCC 16.1, Release. `file_streams` passes 5/5; keeping the new test while restoring `Formatting.cpp` to main fails 5/5 at the assertion that a short character scan retains the file lock through the field. Plain main with its own source/test passes 3/3. This is the contributor's run, not a new local Windows execution. ### 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 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none; library compatibility only <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-10-30 00:05:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T00:05:38Z.

Review: APPROVED


Reviewed — looks good

The narrow stream %c path now treats a nonempty short field as a conversion, preserves suppressed-input counting, and leaves the next field available after exact-width reads.

What I ran

  • Reviewed exact head 63af66d6c5e20c9d3755bb5d84e6970077a39724 against current main e7d8e353362935a1f00e893d901b1debc671f5ce; merge preview was clean at 99ffa586aeebd06a8c37d85c62859a47fe3586c8.
  • Environment: Linux 6.1.0-54-amd64, x86-64, Python 3.11.2. No compiler was used.
  • The author’s test baseline is f0c8f26eec6edd346338e7c2d7f060ece52e8f50; I checked that main has no changes to either touched file since that baseline.
git merge-tree --write-tree upstream/main refs/review/pr-13057-current
git diff --check upstream/main...refs/review/pr-13057-current
python3 tools/check_conventions.py --base upstream/main --head refs/review/pr-13057-current

Results

Check Result
Merge preview Clean; tree 99ffa586aeebd06a8c37d85c62859a47fe3586c8
git diff --check Exit 0; no output
Conventions Passed
Hosted CI Eligibility, conventions, intake, and report pass; labels and platform jobs queued; Linux product job skipped
Author-reported focused tests Four libc test targets pass 4/4 each on Linux; the author reports the new short-field assertion fails on unchanged main and passes with the patch. I did not run these build-dependent tests locally.
Command output
99ffa586aeebd06a8c37d85c62859a47fe3586c8
All conventions checks passed.

Negative control

Assertion Change to code under test Result
A one-byte stream read by %2c%n reports the converted byte and updates %n Author tested unchanged main scanner logic Author reports the new assertion failed on main; this is not an independently run result

Sources

Not verified

  • I did not build or run FileStreams locally. Windows and macOS runtime behavior and console behavior were not independently tested in this session.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5706537434 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13057#pullrequestreview-5706537434). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T00:05:38Z. **Review:** APPROVED --- # Reviewed — looks good The narrow stream `%c` path now treats a nonempty short field as a conversion, preserves suppressed-input counting, and leaves the next field available after exact-width reads. ## What I ran - Reviewed exact head `63af66d6c5e20c9d3755bb5d84e6970077a39724` against current `main` `e7d8e353362935a1f00e893d901b1debc671f5ce`; merge preview was clean at `99ffa586aeebd06a8c37d85c62859a47fe3586c8`. - Environment: Linux 6.1.0-54-amd64, x86-64, Python 3.11.2. No compiler was used. - The author’s test baseline is `f0c8f26eec6edd346338e7c2d7f060ece52e8f50`; I checked that `main` has no changes to either touched file since that baseline. ```sh git merge-tree --write-tree upstream/main refs/review/pr-13057-current git diff --check upstream/main...refs/review/pr-13057-current python3 tools/check_conventions.py --base upstream/main --head refs/review/pr-13057-current ``` ## Results | Check | Result | |---|---| | Merge preview | Clean; tree `99ffa586aeebd06a8c37d85c62859a47fe3586c8` | | `git diff --check` | Exit 0; no output | | Conventions | Passed | | Hosted CI | Eligibility, conventions, intake, and report pass; labels and platform jobs queued; Linux product job skipped | | Author-reported focused tests | Four libc test targets pass 4/4 each on Linux; the author reports the new short-field assertion fails on unchanged `main` and passes with the patch. I did not run these build-dependent tests locally. | <details> <summary>Command output</summary> ```text 99ffa586aeebd06a8c37d85c62859a47fe3586c8 All conventions checks passed. ``` </details> ## Negative control | Assertion | Change to code under test | Result | |---|---|---| | A one-byte stream read by `%2c%n` reports the converted byte and updates `%n` | Author tested unchanged `main` scanner logic | Author reports the new assertion failed on `main`; this is not an independently run result | ## Sources - [Short stream `%c` implementation](https://github.com/boykopovar/AnyPS5/blob/63af66d6c5e20c9d3755bb5d84e6970077a39724/core/libs/prx/libc/src/Formatting.cpp) - [Short stream field tests](https://github.com/boykopovar/AnyPS5/blob/63af66d6c5e20c9d3755bb5d84e6970077a39724/core/libs/tests/FileStreams.cpp) ## Not verified - I did not build or run `FileStreams` locally. Windows and macOS runtime behavior and console behavior were not independently tested in this session.
MarcZX10 commented 2026-10-30 02:01:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T02:01:54Z.

Review: APPROVED


Re-checked — new head, looks good

The updated scanner holds the recursive stream lock across the whole field, addressing the interleaving window; the added regression checks lock availability during and after scanning, and current-main checks pass.

What I ran

Reviewed PR head ff25c7aba5f6a53215385cae3627b00556c4dd99, recorded PR base from the current PR metadata, against current main 91a69952c10099966378a6a9006b8ef616ae22da. The merge preview is clean (tree 342220ad34b27c990a1cf1529c4ee175a606b065). Environment: Linux x86_64, Git 2.39.5, Python 3.11.2; no compiler or project build was run.

git diff --check upstream/main...ff25c7aba5f6a53215385cae3627b00556c4dd99
python3 tools/check_conventions.py --base upstream/main --head ff25c7aba5f6a53215385cae3627b00556c4dd99
git merge-tree --write-tree upstream/main ff25c7aba5f6a53215385cae3627b00556c4dd99

Results

Check Result
Current-main merge preview Clean; tree 342220ad34b27c990a1cf1529c4ee175a606b065.
git diff --check Passed; no output.
Conventions Passed.
Focused Linux suites Author reports all four suites pass on this head and 2,880 reference rows are unchanged; 141 rows are corrected with no field regressions.
New locking regression The author reports the try-lock/position regression fails on the initial unlocked implementation; on unchanged main, locking passes while the earlier short-field assertion fails.
Hosted checks Linux and Windows relinker jobs and Linux Debug/Release register-context checks passed; Windows passed. macOS relinker/register-context and write-watch jobs were still in progress at review time; product Linux job was skipped.
Unedited local command output
$ git diff --check upstream/main...ff25c7aba5f6a53215385cae3627b00556c4dd99
(no output; exit 0)

$ python3 tools/check_conventions.py --base upstream/main --head ff25c7aba5f6a53215385cae3627b00556c4dd99
All conventions checks passed.

$ git merge-tree --write-tree upstream/main ff25c7aba5f6a53215385cae3627b00556c4dd99
342220ad34b27c990a1cf1529c4ee175a606b065

Negative control

The author reports the new lock/position test fails on the initial implementation and that the prior short-field behavior remains failing on unchanged main; I did not independently mutate the code or rerun these controls.

Findings

Blocking

None found in the reviewed head.

Non-blocking

The Linux test results are author-reported. Windows hosted CI linked the changed test executable, but its CTest execution step was skipped; Windows runtime and native-title behavior remain unverified.

Sources

Not verified

I did not build or run tests locally, run Windows/macOS CTest, or test SDK-thread/native-title behavior. Some macOS hosted jobs were still in progress at review time.

To get this to a good state

No further change is requested for this head. I will recheck the same commands and relevant tests if the PR head changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5707257630 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13057#pullrequestreview-5707257630). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T02:01:54Z. **Review:** APPROVED --- # Re-checked — new head, looks good The updated scanner holds the recursive stream lock across the whole field, addressing the interleaving window; the added regression checks lock availability during and after scanning, and current-main checks pass. ## What I ran Reviewed PR head `ff25c7aba5f6a53215385cae3627b00556c4dd99`, recorded PR base from the current PR metadata, against current `main` `91a69952c10099966378a6a9006b8ef616ae22da`. The merge preview is clean (tree `342220ad34b27c990a1cf1529c4ee175a606b065`). Environment: Linux x86_64, Git 2.39.5, Python 3.11.2; no compiler or project build was run. ```sh git diff --check upstream/main...ff25c7aba5f6a53215385cae3627b00556c4dd99 python3 tools/check_conventions.py --base upstream/main --head ff25c7aba5f6a53215385cae3627b00556c4dd99 git merge-tree --write-tree upstream/main ff25c7aba5f6a53215385cae3627b00556c4dd99 ``` ## Results | Check | Result | |---|---| | Current-main merge preview | Clean; tree `342220ad34b27c990a1cf1529c4ee175a606b065`. | | `git diff --check` | Passed; no output. | | Conventions | Passed. | | Focused Linux suites | Author reports all four suites pass on this head and 2,880 reference rows are unchanged; 141 rows are corrected with no field regressions. | | New locking regression | The author reports the try-lock/position regression fails on the initial unlocked implementation; on unchanged `main`, locking passes while the earlier short-field assertion fails. | | Hosted checks | Linux and Windows relinker jobs and Linux Debug/Release register-context checks passed; Windows passed. macOS relinker/register-context and write-watch jobs were still in progress at review time; product Linux job was skipped. | <details> <summary>Unedited local command output</summary> ```text $ git diff --check upstream/main...ff25c7aba5f6a53215385cae3627b00556c4dd99 (no output; exit 0) $ python3 tools/check_conventions.py --base upstream/main --head ff25c7aba5f6a53215385cae3627b00556c4dd99 All conventions checks passed. $ git merge-tree --write-tree upstream/main ff25c7aba5f6a53215385cae3627b00556c4dd99 342220ad34b27c990a1cf1529c4ee175a606b065 ``` </details> ## Negative control The author reports the new lock/position test fails on the initial implementation and that the prior short-field behavior remains failing on unchanged `main`; I did not independently mutate the code or rerun these controls. ## Findings ### Blocking None found in the reviewed head. ### Non-blocking The Linux test results are author-reported. Windows hosted CI linked the changed test executable, but its CTest execution step was skipped; Windows runtime and native-title behavior remain unverified. ## Sources - [Reviewed head](https://github.com/boykopovar/AnyPS5/commit/ff25c7aba5f6a53215385cae3627b00556c4dd99) - [Current-main merge base](https://github.com/boykopovar/AnyPS5/tree/91a69952c10099966378a6a9006b8ef616ae22da) - [Author’s exact-head report and negative controls](https://github.com/boykopovar/AnyPS5/pull/13057#issuecomment-6512371019) ## Not verified I did not build or run tests locally, run Windows/macOS CTest, or test SDK-thread/native-title behavior. Some macOS hosted jobs were still in progress at review time. ## To get this to a good state No further change is requested for this head. I will recheck the same commands and relevant tests if the PR head changes.
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!13057
No description provided.