fix(libc): return stream errors from fwrite, fread and fputs instead of throwing #4380

Merged
AzazelSensei merged 1 commit from upstream-pr/4380 into main 2026-10-12 18:22:53 +00:00
AzazelSensei commented 2026-10-12 08:21:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AzazelSensei on 2026-10-12T08:21:04Z.


What

fread, fwrite and fputs threw on an ordinary I/O failure, which takes the whole process down. FreeBSD reports these to the caller instead: fwrite returns the number of complete items written, fread returns a short count with the error flag set, and fputs returns EOF.

They now return the host result and keep the guest stream's error flag in sync (fputs was missing the SyncStatus call). The throws for null arguments, unwritable buffers and closed streams stay. fflush is left alone since #4372 is changing it.

Tested

Linux x86_64, file_streams test: writing to an rb stream returns 0 from fwrite and EOF from fputs with ferror set, reading from a wb stream returns 0 with ferror set. The new cases abort with fread: read failed on current main; the existing cases are unchanged and pass.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4826769437 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4380). Originally posted by [AzazelSensei](https://github.com/AzazelSensei) on 2026-10-12T08:21:04Z. --- ### What `fread`, `fwrite` and `fputs` threw on an ordinary I/O failure, which takes the whole process down. FreeBSD reports these to the caller instead: [fwrite](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/stdio/fwrite.c) returns the number of complete items written, [fread](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/stdio/fread.c) returns a short count with the error flag set, and [fputs](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libc/stdio/fputs.c) returns EOF. They now return the host result and keep the guest stream's error flag in sync (`fputs` was missing the `SyncStatus` call). The throws for null arguments, unwritable buffers and closed streams stay. `fflush` is left alone since #4372 is changing it. ### Tested Linux x86_64, `file_streams` test: writing to an `rb` stream returns 0 from `fwrite` and EOF from `fputs` with `ferror` set, reading from a `wb` stream returns 0 with `ferror` set. The new cases abort with `fread: read failed` on current `main`; the existing cases are unchanged and pass. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4380
No description provided.