Skip to content

fix(libc): report EISDIR when a secure stream scan reads a directory stream - #23974

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
ToRaNek:fix/libc-secure-stream-scan-directory-error
Nov 20, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
ToRaNek:fix/libc-secure-stream-scan-directory-error

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

What

fscanf_s, vfscanf_s, scanf_s and vscanf_s did not report EISDIR when they read a directory stream. fscanf_s(directoryStream, "%d", &value) returned EOF and set the error indicator, but errno kept whatever the host's failed read left instead of 21.

The fopen debt entry states the rule: "reading a directory stream fails with EISDIR and sets the error indicator on every host". Every other reader applies it through FileStream::KeepDirectoryReadError: fread, fgetc, fgets, and the plain fscanf and vfscanf, with fscanf pinned in GuestFilesystem.cpp. The secure stream forms share ScanGuestStream, whose line was if (!secure) stream->KeepDirectoryReadError(result == -1);, so they alone skipped it. That condition came with the secure parameter and only kept what the secure path did before the directory handling existed; nothing in the debt or the tests says the secure forms should differ.

This PR removes the condition. KeepDirectoryReadError only acts when the scan failed, the stream is a directory stream and its error indicator is set, so nothing changes for ordinary streams or for a secure scan that fails on a runtime-constraint violation of an ordinary stream.

Tested

Windows 11 IoT Enterprise LTSC 2024 (10.0.26100), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

…stream

fscanf, vfscanf, fgetc, fgets and fread keep EISDIR when a read of a
directory stream fails. fscanf_s, vfscanf_s, scanf_s and vscanf_s went
through ScanGuestStream, which skipped that step for the secure forms,
so they failed with the host's errno instead. Keep the error for them
too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions github-actions Bot added the libc label Nov 19, 2026

@PrivacyOnion PrivacyOnion 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.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against cf8a9634da:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_filesystem_tests built cleanly.
  • CTest (guest_filesystem):
    1/1 Test #5591: guest_filesystem .................   Passed    0.64 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Formatting.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5591: guest_filesystem .................   Passed    0.15 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified ScanGuestStream in Formatting.cpp: removed !secure check so stream->KeepDirectoryReadError(result == -1) runs uniformly for secure and non-secure scan routines.
    • Verified FileStream::KeepDirectoryReadError behavior: only triggers when failed is true, stream is marked as directory stream, and error indicator is set, ensuring regular files and ordinary constraint failures are unaffected.
    • Verified POSIX error behavior and error code 21 (EISDIR) when attempting to read from directory streams across fscanf, fscanf_s, and other stdio stream readers.
    • Verified AGENTS.md compliance: no comments added, clean conventions, no magic constants or single-title workarounds.
    • Verified GuestFilesystem.cpp test coverage: properly checks fscanf_s returning -1, errno == 21, and ferror != 0 across directory open modes, matching existing fscanf test semantics.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit e8c62fe into boykopovar:main Nov 20, 2026
22 checks passed
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