Skip to content

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

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
JJenkx:fix/libc-stream-scan-eof-errno
Oct 30, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
JJenkx:fix/libc-stream-scan-eof-errno

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

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)$'

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

@rooklz rooklz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 7386 core/libs/prx/libc/src/Formatting.cpp
core/libs/tests/FileStreams.cpp
PR 8150 core/libs/tests/FileStreams.cpp
PR 9340 core/libs/prx/libc/src/Formatting.cpp
PR 10324 core/libs/prx/libc/src/Formatting.cpp
PR 11332 core/libs/prx/libc/src/Formatting.cpp
PR 12057 core/libs/tests/FileStreams.cpp

@JJenkx

JJenkx commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

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).

AI-assisted: yes.

@github-actions github-actions Bot added the libc label Oct 30, 2026
@adriadam10
adriadam10 merged commit 3465fa3 into boykopovar:main Oct 30, 2026
19 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the HostVfscanf errno restore (only on EOF with errno 0, real read errors keep their errno), the new file_streams rows (seeds 0/22/97, fscanf and vfscanf, empty/whitespace input, ERANGE kept, write-only-fd read error under __GLIBC__), which fail without the change, and CI (conventions, relinker, windows); merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants