fix(libc): leave a stream that is reading untouched in fflush on macOS #8920

Merged
tkcm01 merged 1 commit from upstream-pr/8920 into main 2026-10-24 11:22:51 +00:00
tkcm01 commented 2026-10-21 06:13:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tkcm01 on 2026-10-21T06:13:54Z.


What

fflush_nid_postfix skips the host fflush for a stream that is not writing only on Linux (__fwriting, #4372). On macOS the host fflush of a stream that is reading seeks the descriptor back and drops the read buffer together with a pushed-back character, so after fgetc → 'a', ungetc('Z') and fflush on "abcdef\n", the next fread returned "\n" instead of "Zbcdef\n" (guest_standard_streams, CheckFlushWhileReading, for both "r" and "r+").

FreeBSD's __sflush returns without touching a stream whose flags lack __SWR. macOS's stdio keeps the same flag in FILE::_flags (<_stdio.h>), so the macOS branch tests it there.

The same two lines are part of the macOS port in #7201 (@marion909, credited as co-author); this splits them out as a small change, as for the rest of the macOS series on #1.

Tested

macOS 27.0.1, Apple M4, Apple clang 21, x86-64 build under Rosetta 2. The libraries do not link on macOS from main alone, so the run is on mugurc's ci/macos-series-2 (53db0badb, main 3fd1cc393) with #7524, the code commits of #7937 and the libSceSsl change from #7332, the setup described on #1. No Vulkan loader, so the AGC/GPU tests are skipped.

  • Before: guest_standard_streams fails with fflush of a "r" stream that is reading changed the data read next: "\n" (1 bytes), expected "Zbcdef\n" (7 bytes).
  • After: guest_standard_streams passes.
  • Full ctest -j4: 1648 tests, 14 failed, all of which also fail without this change (15 without it, the 15th being guest_standard_streams); no new failure.
  • Linux and Windows code is unchanged (#elif defined(__APPLE__)).
  • python3 tools/check_conventions.py --base upstream/main: passed. tools/pr_overlap.py lists #7081 and #7201 for FileAndHeap.cpp; #7081 does not touch fflush_nid_postfix, #7201 contains this same change.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4947253316 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8920). Originally posted by [tkcm01](https://github.com/tkcm01) on 2026-10-21T06:13:54Z. --- ### What `fflush_nid_postfix` skips the host `fflush` for a stream that is not writing only on Linux (`__fwriting`, #4372). On macOS the host `fflush` of a stream that is reading seeks the descriptor back and drops the read buffer together with a pushed-back character, so after `fgetc` → `'a'`, `ungetc('Z')` and `fflush` on `"abcdef\n"`, the next `fread` returned `"\n"` instead of `"Zbcdef\n"` (`guest_standard_streams`, `CheckFlushWhileReading`, for both `"r"` and `"r+"`). FreeBSD's `__sflush` returns without touching a stream whose flags lack `__SWR`. macOS's stdio keeps the same flag in `FILE::_flags` (`<_stdio.h>`), so the macOS branch tests it there. The same two lines are part of the macOS port in #7201 (@marion909, credited as co-author); this splits them out as a small change, as for the rest of the macOS series on #1. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> macOS 27.0.1, Apple M4, Apple clang 21, x86-64 build under Rosetta 2. The libraries do not link on macOS from `main` alone, so the run is on mugurc's `ci/macos-series-2` (53db0badb, `main` 3fd1cc393) with #7524, the code commits of #7937 and the libSceSsl change from #7332, the setup described on #1. No Vulkan loader, so the AGC/GPU tests are skipped. - Before: `guest_standard_streams` fails with `fflush of a "r" stream that is reading changed the data read next: "\n" (1 bytes), expected "Zbcdef\n" (7 bytes)`. - After: `guest_standard_streams` passes. - Full `ctest -j4`: 1648 tests, 14 failed, all of which also fail without this change (15 without it, the 15th being `guest_standard_streams`); no new failure. - Linux and Windows code is unchanged (`#elif defined(__APPLE__)`). - `python3 tools/check_conventions.py --base upstream/main`: passed. `tools/pr_overlap.py` lists #7081 and #7201 for `FileAndHeap.cpp`; #7081 does not touch `fflush_nid_postfix`, #7201 contains this same change. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-21 06:17:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:17:04Z.

Review: APPROVED


The Apple-only _SWR guard is narrowly scoped and matches the read-stream behavior this test exercises. The described macOS before/after result is directly relevant, and current PR checks pass for conventions and the focused register-context jobs. I did not rerun the macOS test locally. One coordination point: #7201 currently contains the same guard; if this focused PR lands first, that hunk should be removed from #7201 during its rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5595662382 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8920#pullrequestreview-5595662382). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T06:17:04Z. **Review:** APPROVED --- The Apple-only `_SWR` guard is narrowly scoped and matches the read-stream behavior this test exercises. The described macOS before/after result is directly relevant, and current PR checks pass for conventions and the focused register-context jobs. I did not rerun the macOS test locally. One coordination point: #7201 currently contains the same guard; if this focused PR lands first, that hunk should be removed from #7201 during its rebase.
tkcm01 commented 2026-10-21 08:12:51 +00:00 (Migrated from github.com)

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


Thanks for the review. Same note for #8658 (@iamthehimansh): it carries the same __SWR guard in fflush_nid_postfix, so if this one lands first that hunk can be dropped from #8658 on rebase, as for #7201.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6311292360 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8920#issuecomment-6311292360). Originally posted by [tkcm01](https://github.com/tkcm01) on 2026-10-21T08:12:51Z. --- Thanks for the review. Same note for #8658 (@iamthehimansh): it carries the same `__SWR` guard in `fflush_nid_postfix`, so if this one lands first that hunk can be dropped from #8658 on rebase, as for #7201.
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!8920
No description provided.