Skip to content

fix(libc): fail fputs, fputc and fprintf on a closed stream as a PS5 does - #743

Open
Thimo188 wants to merge 3 commits into
mainfrom
upstream-pr/22047
Open

Thimo188 wants to merge 3 commits into
mainfrom
upstream-pr/22047

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#22047, originally opened by brajankw.

Source commit: 1d3137ca095f599251679fdb1825c3af71473cbf. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

A write to a stream that fclose or a failed freopen closed set errno to EBADF in fputs, fputc and fprintf. On a PS5 fputs and fputc return -1 and leave errno unchanged, and fprintf fails with EINVAL (boykopovar#9763; the freopen of a closed stream entry in docs/dev/debt/libc.md).

  • Those three now do the same; putc, putchar and __swbuf forward to fputc and follow it.
  • The other stream writes (fwrite, puts, printf and the wide forms) were not measured and keep EBADF; the debt entry says so.
  • GuestStandardStreams.cpp and GuestFreopenHostStandard.cpp check the measured results where they checked EBADF before.

Tested

Linux (Debian 13, GCC 14.2.0, Release), on main ec7369ecb8.

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

Summary by CodeRabbit

  • Bug Fixes
    • Corrected error handling for writes to closed streams. Some write operations now preserve the existing error value, while formatted writes report an invalid-argument error.
  • Documentation
    • Added details on closed-stream behavior, including the results of reopening streams with invalid filenames.

brajankw and others added 3 commits November 17, 2026 03:15
…does

A write to a stream that fclose or a failed freopen closed set errno to
EBADF in fputs, fputc and fprintf. On a PS5 fputs and fputc return -1
and leave errno unchanged, and fprintf fails with EINVAL (boykopovar#9763). Those
three now do the same; putc, putchar and __swbuf forward to fputc and
follow it. The other stream writes, which were not measured, keep
EBADF.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	core/libs/prx/libc/src/FileAndHeap.cpp
#	core/libs/prx/libc/src/StandardStreams.cpp
…unchanged

On a closed stream fputc returns -1 without setting errno, as measured on a PS5. The fcloseall test asserted EBADF.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Closed-stream write behavior

Layer / File(s) Summary
Closed-stream write error handling
core/libs/prx/libc/src/FileAndHeap.cpp, core/libs/prx/libc/src/StandardStreams.cpp, core/libs/prx/libc/src/Formatting.cpp
fputs and fputc return -1 without changing errno when the stream is not open. fprintf returns -1 and sets errno to 22 if opening the operation fails for a non-null stream.
Write behavior tests and change note
core/libs/tests/GuestStandardStreams.cpp, core/libs/tests/FileStreams.cpp, core/libs/tests/GuestFreopenHostStandard.cpp, docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
Tests check the updated errno behavior for closed-stream writes and failed freopen calls. The change note records the documented descriptor, filename, and write-call results.

Suggested reviewers: b-howell, boykopovar, rooklz

Change: Bug fix

Merge Risk: Minimal · up to 1d313

This change adjusts errno results for writes to closed streams so they match measured PS5 behavior. Tests cover the new results, and no merge-blocking risk is evident.

Security architecture risk: Minimal · up to 1d313

Closed-stream writes still fail before reaching the host stream. The change affects how libc reports that failure to guest callers, not which streams callers can write to.

Security review details

Trust Boundaries and Controls

  • observed — The changed paths still require a valid stream lease and an open stream before attempting a host write. fprintf retains the existing write-mode check in vfprintf rather than creating a separate write path.

Resilience and Maintainability Implications

  • inferred — The extra fprintf lease does not transfer stream ownership. It keeps the stream pinned while the same recursive gate serializes the call with close and reopen. The scoped lock and lease release on early return and exception unwinding, so the added check does not leave the stream pinned for recovery.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 7
  • Waived: $1.75 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ed458012-c2ea-4f44-a52c-1ebb01186b85
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and 1d3137c.

Files selected for processing: 7

  • core/libs/prx/libc/src/FileAndHeap.cpp
  • core/libs/prx/libc/src/Formatting.cpp
  • core/libs/prx/libc/src/StandardStreams.cpp
  • core/libs/tests/FileStreams.cpp
  • core/libs/tests/GuestFreopenHostStandard.cpp
  • core/libs/tests/GuestStandardStreams.cpp
  • docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md

Limit details: You’ve used all 10 included reviews currently available.

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 1 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 3 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 4 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 5 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 6 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 7 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 8 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 9 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 10 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 11 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 12 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 13 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 14 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 15 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 16 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 17 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 18 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 19 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md
PR 20 docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants