Repository navigation
fix(libc): fail scanf and ungetc on write-only streams on Linux as FreeBSD does - #15768
Open
AlexWhitehouse wants to merge 1 commit into
Open
AlexWhitehouse wants to merge 1 commit into
AlexWhitehouse wants to merge 1 commit into
Conversation
…eeBSD does On Linux, fscanf, vfscanf, scanf and vscanf on a stream opened only for writing returned EOF with EBADF but left the error indicator clear: SwitchToReadingUnlocked let the stream through to glibc's __vfscanf_internal, which rejects _IO_NO_READS without setting _IO_ERR_SEEN. FreeBSD 11.4's __srefill sets EBADF and __SERR. The glibc branch now fails an unreadable stream the way the Windows branch does since boykopovar#14878: errno 9, the guest error indicator, and false. OpenForReading gains a glibc branch (__freadable), so ungetc and ungetwc return EOF on such a stream without touching errno or the error indicator, as FreeBSD's __ungetc does and as Windows already did. Before, glibc pushed the character back onto the write-only stream. file_streams now checks fscanf, vfscanf, fgetwc, ungetc, ungetwc and clearerr on write-only streams on every host, not only on Windows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AlexWhitehouse
force-pushed
the
fix/libc-linux-scanf-write-only-error
branch
from
November 6, 2026 16:59
efe547e to
02e2d8d
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
On Linux, guest
fscanf,vfscanf,scanfandvscanfon a stream open only for writing returnedEOFwithEBADFbut left the error indicator clear. #14878 fixed reads on such streams for Windows and recorded this Linux gap indocs/dev/debt/libc.md:FileStream::SwitchToReadingUnlocked(core/libs/prx/libc/include/FileStream.hpp) let a non-readable stream through to glibc's__vfscanf_internal. That function rejects_IO_NO_READSwithout setting_IO_ERR_SEEN.__srefillsetserrno = EBADFand__SERR, then returnsEOF.Change:
SwitchToReadingUnlockednow fails a stream that__freadablerejects exactly as the Windows branch does:errno9, the guest error indicator, andfalse. Every read path already returns on a failed switch, sofscanfandvfscanfnow reportEBADFwith the error indicator, likefread,fgetc,getc_unlockedandfgetsalready did through glibc's underflow. Update streams take the same path as before.FileStream::OpenForReadinggains a glibc branch (__freadable). As a result,ungetcandungetwcon a write-only stream returnEOFwithout settingerrnoor the error indicator, as FreeBSD 11.4's__ungetcdoes ((fp->_flags & __SRD) == 0and no__SRW) and as Windows has since fix(libc): fail reads on a write-only stream with EBADF on Windows #14878. Before, glibc pushed the character back onto the write-only stream.Tested
file_streams(CheckReadsOnWriteOnlyStreams): thefscanfchecks (errno==EBADF,ferror!= 0) and theungetccheck (EOF,errno0,ferror0) used to run on Windows only and now run on every host. New checks:vfscanfthrough the existingva_listhelper;fgetwc;ungetwc;ferror== 0 after eachclearerr.No existing assertion was removed or weakened.
Linux x86-64 (Ubuntu 26.04, GCC 15, glibc 2.43, Release):
main'sFileStream.hpp,file_streamsfails withfscanf on a write-only stream left errno or the error indicator unset;ctest -R 'guest_binary_word|guest_fwide|file_streams|file_position|guest_standard_streams|file_stream_status|guest_wide_stream_scan|guest_stream_purge|guest_append_streams|guest_host_streams'passes 14/14;OpenForReadingbranch fails withungetc on a write-only stream, and removing the new read check fails with thefscanfmessage above.Windows 11 (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release):
main'sFileStream.hpp,file_streamsalso passes, as expected: Windows already behaved this way since fix(libc): fail reads on a write-only stream with EBADF on Windows #14878, and the Windows code is unchanged.python3 tools/check_conventions.py --base origin/main: all conventions checks passed.python3 tools/pr_overlap.py --branch fix/libc-linux-scanf-write-only-error: no confirmed overlap among the 141 PRs it could merge.docs/dev/debt/libc.md) already conflict withmain.ungetcnote) adds its entry directly after the edited debt line, so whichever merges second has a one-line conflict there; keep both entries.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code