Skip to content

fix(libc): set the error indicator when a write-only stream is read on Linux as FreeBSD does - #15773

Closed
Ragbie wants to merge 3 commits into
boykopovar:mainfrom
Ragbie:fix/linux-scanf-write-only-error
Closed

Ragbie wants to merge 3 commits into
boykopovar:mainfrom
Ragbie:fix/linux-scanf-write-only-error

Conversation

@Ragbie

@Ragbie Ragbie commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

What

On Linux, a read from a stream opened only for writing now fails as on FreeBSD 11.4: EOF (or 0 for fread), errno EBADF, and the error indicator set. Before, fscanf, vfscanf, scanf and vscanf left the error indicator clear, as the libc.md debt entry "Reads on a write-only stream" recorded.

  • FreeBSD 11.4 __srefill sets errno = EBADF and __SERR on a stream with neither __SRD nor __SRW, and every stdio read reaches it first. ungetc returns EOF on such a stream without touching either.
  • On Linux the guest scanf family went to glibc's __vfscanf_internal, which rejects _IO_NO_READS without setting _IO_ERR_SEEN.
  • The glibc branch of FileStream::SwitchToReadingUnlocked now fails a stream that __freadable reports unreadable, as the Windows branch already does (errno 9, the guest error flag, status sync). Every guest read entry point (fgetc, getc_unlocked, fgets, fread, the scanf family and the wide reads built on fgetc) passes through it, so they all agree, and none reaches the host stream.
  • The glibc branch of OpenForReading returns __freadable, so ungetc and ungetwc return EOF before that check and leave errno and the error indicator alone, as FreeBSD and the Windows branch do.
  • The debt entry is removed.

Tested

file_streams: CheckReadsOnWriteOnlyStreams checked errno and the error indicator after fscanf, and ungetc on a write-only stream, on Windows only. Those checks now run on every host.

Linux (Ubuntu 24.04 container, GCC 13.3.0):

  • Without the fix: what(): fscanf on a write-only stream left errno or the error indicator unset.
  • With the fix: file_streams passes with --repeat until-fail:3. The 66 tests matching stream|stdio|scanf|ungetc|fopen|fread|fget|getc|freopen|fdopen|popen|wide|format pass.

Windows 11 Pro, Intel Core i5-14600KF, NVIDIA GPU, WinLibs GCC 15.2.0, full Release build with libs: the Windows code is unchanged.

  • file_streams passes.
  • Full ctest: 99% tests passed, 6 tests failed out of 4005. All six fail on main on this machine too:
    • test_registration and select_tests_MissingCommandTests fail with WinError 1314, because this user cannot create symbolic links.
    • agc_driver_direct_write_completion, agc_driver_image_plane_address_on_line, agc_driver_image_sample_lod_clamp and agc_driver_image_sample_lod_clamp_shader_lane are GPU tests on an NVIDIA card.
  • check_skips.py reports one skip, guest_fopen_errors, which skips itself here because this user cannot create symbolic links.

macOS: not tested. Apple's libc follows BSD stdio here (__srefill and ungetc as above), so the checks that are no longer limited to Windows are expected to pass there. The #else branch is unchanged.

Overlaps: of the open PRs that touch FileStream.hpp, only #12036 (mine) changes SwitchToReadingUnlocked, in its Windows branch; a trial merge with this branch has no conflicts (git merge-tree). #11831 already conflicts with main in FileStream.hpp, FileAndHeap.cpp and libc.md; this branch adds no other file to that list.

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

@Ragbie
Ragbie force-pushed the fix/linux-scanf-write-only-error branch from efb989c to 26bd2fc Compare November 5, 2026 13:17
@Ragbie
Ragbie force-pushed the fix/linux-scanf-write-only-error branch from 26bd2fc to e033313 Compare November 6, 2026 11:18
@Ragbie

Ragbie commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 6185bdcfc4 (head e033313846) without conflicts. The previous Windows run failed only in guest_http2_gzip_headers, where opening the sandbox certificate list raised a std::filesystem::filesystem_error inside libkernel; #16082 fixed that on main after the run. This PR does not touch that path.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 6, 2026
@github-actions

github-actions Bot commented Nov 7, 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 3965 docs/dev/debt/libc.md
PR 5021 core/libs/prx/libc/include/FileStream.hpp
docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6819 docs/dev/debt/libc.md
PR 7081 docs/dev/debt/libc.md
PR 7386 core/libs/tests/FileStreams.cpp
docs/dev/debt/libc.md
PR 7447 core/libs/prx/libc/include/FileStream.hpp
docs/dev/debt/libc.md
PR 7544 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 7851 docs/dev/debt/libc.md
PR 8150 core/libs/tests/FileStreams.cpp
docs/dev/debt/libc.md
PR 8417 docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9087 docs/dev/debt/libc.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9466 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9677 docs/dev/debt/libc.md
PR 10002 docs/dev/debt/libc.md
PR 10007 docs/dev/debt/libc.md
PR 10113 docs/dev/debt/libc.md
PR 10226 docs/dev/debt/libc.md
PR 10274 docs/dev/debt/libc.md
PR 10321 docs/dev/debt/libc.md
PR 10397 docs/dev/debt/libc.md
PR 10506 docs/dev/debt/libc.md
PR 10510 docs/dev/debt/libc.md
PR 10512 docs/dev/debt/libc.md
PR 10569 docs/dev/debt/libc.md
PR 10780 docs/dev/debt/libc.md
PR 10987 docs/dev/debt/libc.md
PR 11109 docs/dev/debt/libc.md
PR 11172 docs/dev/debt/libc.md
PR 11262 docs/dev/debt/libc.md
PR 11266 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11625 docs/dev/debt/libc.md
PR 11634 docs/dev/debt/libc.md
PR 11661 docs/dev/debt/libc.md
PR 11664 docs/dev/debt/libc.md
PR 11689 docs/dev/debt/libc.md
PR 11698 docs/dev/debt/libc.md
PR 11726 docs/dev/debt/libc.md
PR 11831 core/libs/prx/libc/include/FileStream.hpp
core/libs/tests/FileStreams.cpp
docs/dev/debt/libc.md
PR 11848 docs/dev/debt/libc.md
PR 11870 docs/dev/debt/libc.md
PR 12053 docs/dev/debt/libc.md
PR 12184 docs/dev/debt/libc.md
PR 12514 docs/dev/debt/libc.md
PR 12518 docs/dev/debt/libc.md
PR 12574 docs/dev/debt/libc.md
PR 12592 docs/dev/debt/libc.md
PR 12593 docs/dev/debt/libc.md
PR 12594 docs/dev/debt/libc.md
PR 12598 docs/dev/debt/libc.md
PR 12605 docs/dev/debt/libc.md
PR 12622 docs/dev/debt/libc.md
PR 12627 docs/dev/debt/libc.md
PR 12682 core/libs/prx/libc/include/FileStream.hpp
docs/dev/debt/libc.md
PR 12688 core/libs/prx/libc/include/FileStream.hpp
docs/dev/debt/libc.md
PR 12700 core/libs/prx/libc/include/FileStream.hpp
docs/dev/debt/libc.md
PR 12704 docs/dev/debt/libc.md
PR 12849 docs/dev/debt/libc.md
PR 12920 core/libs/prx/libc/include/FileStream.hpp
docs/dev/debt/libc.md
PR 12947 docs/dev/debt/libc.md
PR 12965 core/libs/prx/libc/include/FileStream.hpp
docs/dev/debt/libc.md
PR 13390 core/libs/prx/libc/include/FileStream.hpp
docs/dev/debt/libc.md
PR 13449 docs/dev/debt/libc.md
PR 13794 docs/dev/debt/libc.md
PR 13804 docs/dev/debt/libc.md
PR 13809 docs/dev/debt/libc.md
PR 13828 docs/dev/debt/libc.md
PR 14212 docs/dev/debt/libc.md
PR 14298 docs/dev/debt/libc.md
PR 14448 core/libs/prx/libc/include/FileStream.hpp
PR 14559 core/libs/tests/FileStreams.cpp
PR 14560 core/libs/tests/FileStreams.cpp
PR 14588 docs/dev/debt/libc.md
PR 14595 docs/dev/debt/libc.md
PR 14610 docs/dev/debt/libc.md
PR 14915 docs/dev/debt/libc.md
PR 15095 docs/dev/debt/libc.md
PR 15206 docs/dev/debt/libc.md
PR 15336 docs/dev/debt/libc.md
PR 15484 docs/dev/debt/libc.md
PR 15486 docs/dev/debt/libc.md
PR 15490 docs/dev/debt/libc.md
PR 15535 docs/dev/debt/libc.md
PR 15561 docs/dev/debt/libc.md
PR 15602 docs/dev/debt/libc.md
PR 15614 docs/dev/debt/libc.md
PR 15683 docs/dev/debt/libc.md
PR 15713 docs/dev/debt/libc.md
PR 15714 docs/dev/debt/libc.md
PR 15768 core/libs/prx/libc/include/FileStream.hpp
core/libs/tests/FileStreams.cpp
docs/dev/debt/libc.md
PR 15791 docs/dev/debt/libc.md
PR 15796 docs/dev/debt/libc.md
PR 15800 docs/dev/debt/libc.md
PR 15801 docs/dev/debt/libc.md
PR 15802 docs/dev/debt/libc.md
PR 15803 docs/dev/debt/libc.md
PR 15804 docs/dev/debt/libc.md
PR 15807 docs/dev/debt/libc.md
PR 15809 docs/dev/debt/libc.md
PR 15834 docs/dev/debt/libc.md
PR 15879 docs/dev/debt/libc.md
PR 15973 docs/dev/debt/libc.md
PR 16004 docs/dev/debt/libc.md
PR 16017 docs/dev/debt/libc.md
PR 16107 docs/dev/debt/libc.md
PR 16170 core/libs/tests/FileStreams.cpp
docs/dev/debt/libc.md
PR 16178 docs/dev/debt/libc.md
PR 16209 core/libs/tests/FileStreams.cpp
PR 16210 docs/dev/debt/libc.md
PR 16214 docs/dev/debt/libc.md
PR 16216 docs/dev/debt/libc.md
PR 16516 docs/dev/debt/libc.md
PR 16548 docs/dev/debt/libc.md
PR 16550 docs/dev/debt/libc.md
PR 16560 docs/dev/debt/libc.md
PR 16580 docs/dev/debt/libc.md
PR 16613 docs/dev/debt/libc.md
PR 16740 docs/dev/debt/libc.md

@AlexWhitehouse

Copy link
Copy Markdown
Contributor

This duplicates #15768, opened on 2026-11-04, which already implements the Linux failure of fscanf, vfscanf, scanf and vscanf on a write-only stream with EBADF and the error indicator (and ungetc and ungetwc returning EOF there) and is still open. Please take a look at #15768 before continuing here, to avoid two PRs for the same change.

@adriadam10 adriadam10 self-assigned this Nov 7, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator

Closing as a duplicate of #15768, which was opened 23 minutes earlier, is mergeable with passing checks, and contains the identical FileStream.hpp change (SwitchToReadingUnlocked failing an unreadable glibc stream with EBADF and the error flag, OpenForReading returning __freadable). Its test also covers ungetwc/fgetwc, and it rewrites the debt entry rather than deleting it.

Two PRs for the same change only split review; please take any remaining points to #15768. Feel free to reopen if this one covers something that one doesn't.

@adriadam10 adriadam10 closed this Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs libc needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants