fix(libc): preserve errno on stream scan input exhaustion #13025

Merged
JJenkx merged 2 commits from upstream-pr/13025 into main 2026-10-30 15:29:12 +00:00
JJenkx commented 2026-10-29 22:11:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-29T22:11:29Z.


What

On glibc, scanning an empty or whitespace-only file can return EOF and clear a previously set errno. Two title-bundled libc modules preserve that errno. Restore the incoming errno in HostVfscanf only when the host returns EOF with errno zero; preserve newly reported nonzero errors. Other host branches are unchanged.

The fixture exercises fscanf and vfscanf, empty/whitespace input, errno seeds 0/22/97, ordinary and suppressed conversions, unchanged destinations, EOF/error indicators, and an overflowing float that must retain ERANGE.

This is the separately measured file-stream follow-up suggested during review of #13012. It does not include or depend on that string-scanning change.

Tested

Linux x86-64, GCC Release. Exact head 30d5439075e33df009d3cd3cee47a9d8b8f1cedc, base 52f212ec053f917455f2f87674ddb708dcc545d8; merge preview clean against subsequently fetched main 4c8cff191609bc5f28bf9ee2f93672b531285aa4.

cmake --build build --parallel 2 --target libc file_streams_tests guest_formatting_tests guest_wide_formatting_tests guest_safe_error_tests
ctest --test-dir build -V -R '^(file_streams|guest_formatting|guest_wide_formatting|guest_safe_errors)$'
  • Actual libc build and all four focused suites pass, with zero skips.
  • Negative control: the new file_streams_tests against frozen unmodified base libraries fails with Stream scan input exhaustion changed errno (CTest exit 8).
  • CPU-only library reference: bundled libc modules from PPSA03766 and PPSA07923 agree on all 420 rows: 10 formats × 7 ASCII inputs × 3 errno seeds × 2 entry points. Base differs in 64 rows, solely EOF clearing errno; this change matches all 420, correcting 64 with zero regressions. Return values, destination bytes and new range errors match.
  • SDK stream setup uses an exported allocator initializer and fdopen on duplicated private temporary-file descriptors. No SDK instructions are patched; the probe rejects loaded Vulkan/AGC/VideoOut modules. This is neither console nor native-game qualification.
  • Required conventions/diff checks pass. The overlap scan examined 1,444 open PRs and found no automatic positive overlap. I manually reviewed shared-file patches for unmergeable peers #7386, #8150, #10324, #11332, #11726, #11831 and #12057: none supplies this stream EOF errno guard. Their existing base conflicts remain outside this change.

Authored probe, synthetic rows, exact hashes, test logs and negative control, SHA-256 b1ee388525a747e82742c547fb75700ccac234186f7ac9e857373529b1085b7e. No proprietary modules/assets are included.

Windows/macOS execution is not locally qualified; there is no title, graphics, audio or FPS claim.

Current qualification — 2026-10-30

Added a real read-error regression at ff736bd24ae73dfa54f1f27fca96419cba3b0195 for both fscanf and vfscanf. A readable FILE backed by a write-only descriptor forces a read syscall failure; with seeded errno97, the test requires EOF, errno different from97, unchanged destination, and ferror set. The row is guarded by GLIBC, matching the workaround's platform scope. Plain fopen("wb") rejects the mode before a read and leaves ferror clear on this glibc, so it cannot serve as this regression.

Production is unchanged from 30d5439075e33df009d3cd3cee47a9d8b8f1cedc. Four focused Linux suites passed three repeats before the mutant and three after restoring the exact implementation (24 passes), plus public_headers and conventions. The unconditional EOF errno-restore mutant fails with "Stream scan lost its read error". Restored libc SHA256 equals the qualified positive binary. No skips.

No new Windows/macOS/native SDK/title claim.

Evidence: immutable source and qualification receipts, SHA-256 69989f6c49c749dc948af7261b6751d07c30fe58c6610140798687195932284b (download verified).

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 correctness
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5076288044 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13025). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-29T22:11:29Z. --- ### What On glibc, scanning an empty or whitespace-only file can return `EOF` and clear a previously set `errno`. Two title-bundled libc modules preserve that errno. Restore the incoming errno in `HostVfscanf` only when the host returns `EOF` with errno zero; preserve newly reported nonzero errors. Other host branches are unchanged. The fixture exercises `fscanf` and `vfscanf`, empty/whitespace input, errno seeds 0/22/97, ordinary and suppressed conversions, unchanged destinations, EOF/error indicators, and an overflowing float that must retain `ERANGE`. This is the separately measured file-stream follow-up suggested during review of #13012. It does not include or depend on that string-scanning change. ### Tested Linux x86-64, GCC Release. Exact head `30d5439075e33df009d3cd3cee47a9d8b8f1cedc`, base `52f212ec053f917455f2f87674ddb708dcc545d8`; merge preview clean against subsequently fetched main `4c8cff191609bc5f28bf9ee2f93672b531285aa4`. ```sh cmake --build build --parallel 2 --target libc file_streams_tests guest_formatting_tests guest_wide_formatting_tests guest_safe_error_tests ctest --test-dir build -V -R '^(file_streams|guest_formatting|guest_wide_formatting|guest_safe_errors)$' ``` - Actual libc build and all four focused suites pass, with zero skips. - Negative control: the new `file_streams_tests` against frozen unmodified base libraries fails with `Stream scan input exhaustion changed errno` (CTest exit 8). - CPU-only library reference: bundled libc modules from PPSA03766 and PPSA07923 agree on all 420 rows: 10 formats × 7 ASCII inputs × 3 errno seeds × 2 entry points. Base differs in 64 rows, solely EOF clearing errno; this change matches all 420, correcting 64 with zero regressions. Return values, destination bytes and new range errors match. - SDK stream setup uses an exported allocator initializer and `fdopen` on duplicated private temporary-file descriptors. No SDK instructions are patched; the probe rejects loaded Vulkan/AGC/VideoOut modules. This is neither console nor native-game qualification. - Required conventions/diff checks pass. The overlap scan examined 1,444 open PRs and found no automatic positive overlap. I manually reviewed shared-file patches for unmergeable peers #7386, #8150, #10324, #11332, #11726, #11831 and #12057: none supplies this stream EOF errno guard. Their existing base conflicts remain outside this change. [Authored probe, synthetic rows, exact hashes, test logs and negative control](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/libc-stream-scan-eof-errno-linux-20261029.tar.gz), SHA-256 `b1ee388525a747e82742c547fb75700ccac234186f7ac9e857373529b1085b7e`. No proprietary modules/assets are included. Windows/macOS execution is not locally qualified; there is no title, graphics, audio or FPS claim. <!-- 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. --> <!-- codex-maintenance-20261030-repeat-13025 --> **Current qualification — 2026-10-30** Added a real read-error regression at `ff736bd24ae73dfa54f1f27fca96419cba3b0195` for both fscanf and vfscanf. A readable FILE backed by a write-only descriptor forces a read syscall failure; with seeded errno97, the test requires EOF, errno different from97, unchanged destination, and ferror set. The row is guarded by __GLIBC__, matching the workaround's platform scope. Plain fopen("wb") rejects the mode before a read and leaves ferror clear on this glibc, so it cannot serve as this regression. Production is unchanged from `30d5439075e33df009d3cd3cee47a9d8b8f1cedc`. Four focused Linux suites passed three repeats before the mutant and three after restoring the exact implementation (24 passes), plus public_headers and conventions. The unconditional EOF errno-restore mutant fails with "Stream scan lost its read error". Restored libc SHA256 equals the qualified positive binary. No skips. No new Windows/macOS/native SDK/title claim. Evidence: [immutable source and qualification receipts](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr13025-read-error-regression-linux-20261030.tar.gz), SHA-256 `69989f6c49c749dc948af7261b6751d07c30fe58c6610140798687195932284b` (download verified). ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none; library correctness - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes
rooklz commented 2026-10-29 22:22:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T22:22:36Z.

Review: APPROVED


Checked on head 30d5439075, on macOS host, without the console.

  • Merge with current main 4c8cff1916: git merge-tree is clean, git diff --check is clean and check_conventions.py --base upstream/main passes. #13012 (8a7cd7d35a) also merges cleanly on top, so the two can land in either order.
  • pr_overlap.py: 1444 PRs probed, 21 share files and all 21 merge. Only #7386/#8150/#10324/#11332/#11726/#11831/#12057 could not be compared because they already conflict with main. That matches the list in the body.
  • Host behaviour, using a standalone copy of the HostVfscanf shape on tmpfile() streams. The grid was 5 inputs ("", " ", "\n\t ", abc, 1e99999) × 6 formats (%d %*d %f %s x%d %c) × errno seeds 0/22/97, so 90 rows:
    • glibc 2.41 (Debian, gcc:14 container): plain vfscanf clears errno in 24/90 rows, all of them EOF rows. With the PR guard it is 0/90. 1e99999 with %f still returns 1 with ERANGE for every seed.
    • macOS libc already keeps errno (0/90), so the #else branch needs no change.
  • Mutants of the guard:
    • if (result == EOF) restores errno even when there is a real error. All 90 grid rows still pass, but a read on a write-only stream (/dev/null opened "w") then returns −1 with errno 97 instead of EBADF (9). The PR test has no read-error row, so this mutant survives it. Optional extra row: scan a stream opened for writing only, seed errno with 97, and expect EOF, errno != 97 and ferror set. On glibc this row tells the PR guard apart from the unconditional mutant.
    • if (errno == 0) without the EOF check behaves the same as the PR on every row I tried. Keeping the EOF check is still the right, narrower choice.
  • Call paths: fscanf_nid_postfix (L479), vfscanf_nid_postfix (L808) and HostFscanf all go through HostVfscanf. After it, KeepDirectoryReadError only writes errno=21 on the directory path, so it does not undo the guard. Windows uses ScanFileWindows_nid_no_patch and is left unchanged; that CI lane is still pending.

Approving. The read-error row is optional.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5705738157 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13025#pullrequestreview-5705738157). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T22:22:36Z. **Review:** APPROVED --- Checked on head `30d5439075`, on macOS host, without the console. - Merge with current main `4c8cff1916`: `git merge-tree` is clean, `git diff --check` is clean and `check_conventions.py --base upstream/main` passes. #13012 (`8a7cd7d35a`) also merges cleanly on top, so the two can land in either order. - `pr_overlap.py`: 1444 PRs probed, 21 share files and all 21 merge. Only #7386/#8150/#10324/#11332/#11726/#11831/#12057 could not be compared because they already conflict with main. That matches the list in the body. - Host behaviour, using a standalone copy of the `HostVfscanf` shape on `tmpfile()` streams. The grid was 5 inputs (`""`, `" "`, `"\n\t "`, `abc`, `1e99999`) × 6 formats (`%d %*d %f %s x%d %c`) × errno seeds 0/22/97, so 90 rows: - glibc 2.41 (Debian, gcc:14 container): plain `vfscanf` clears errno in **24/90** rows, all of them EOF rows. With the PR guard it is **0/90**. `1e99999` with `%f` still returns 1 with ERANGE for every seed. - macOS libc already keeps errno (0/90), so the `#else` branch needs no change. - Mutants of the guard: - `if (result == EOF)` restores errno even when there is a real error. All 90 grid rows still pass, but a read on a write-only stream (`/dev/null` opened "w") then returns −1 with errno 97 instead of EBADF (9). The PR test has no read-error row, so this mutant survives it. Optional extra row: scan a stream opened for writing only, seed errno with 97, and expect EOF, `errno != 97` and `ferror` set. On glibc this row tells the PR guard apart from the unconditional mutant. - `if (errno == 0)` without the EOF check behaves the same as the PR on every row I tried. Keeping the EOF check is still the right, narrower choice. - Call paths: `fscanf_nid_postfix` (L479), `vfscanf_nid_postfix` (L808) and `HostFscanf` all go through `HostVfscanf`. After it, `KeepDirectoryReadError` only writes errno=21 on the directory path, so it does not undo the guard. Windows uses `ScanFileWindows_nid_no_patch` and is left unchanged; that CI lane is still pending. Approving. The read-error row is optional.
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!13025
No description provided.