fix(libc): switch update streams to reading in vfscanf #8982

Merged
dantdmdarealguy merged 1 commit from upstream-pr/8982 into main 2026-10-25 00:58:44 +00:00
dantdmdarealguy commented 2026-10-21 08:24:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by dantdmdarealguy on 2026-10-21T08:24:05Z.


What

vfscanf, which vscanf and scanf also go through, read from an update stream without switching it to reading first. #8226 added that switch to fscanf and the other read functions but not to vfscanf. On Windows the UCRT does not switch an r+, w+ or a+ stream from writing to reading by itself, so a vfscanf directly after an fwrite read bytes that were still in the write buffer. Without the switch, fwrite("AB") then vfscanf("%3s") on an r+ stream over 0123456789 does not return 234.

  • vfscanf_nid_postfix now calls if (!stream->SwitchToReading()) return EOF; after GetNativeStream, at the same point and under the same stream lock as fscanf_nid_postfix. As in FreeBSD's __srefill, pending output is flushed and the stream is marked as reading before the read; a failed flush returns EOF.
  • On glibc, vfscanf reads through __uflow, which already switches, so Linux behaviour does not change.
  • file_streams gets a vfscanf case next to the fscanf one. It does fwrite("AB") on an r+ stream over 0123456789, then vfscanf("%3s") through a va_list wrapper, and checks the value read (234), the error indicator and the file contents (AB23456789).

On Windows the test fails on main with 0xc0000409, "vfscanf after fwrite on an r+ stream read other bytes", and passes with the change.

Follow-up to the review on #8226 (merged).

Tested

Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1742), WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, on main b77bd5880:

  • ctest -R "^file_streams$" without the Formatting.cpp change (test change only): fails 3 runs out of 3 with 0xc0000409, "vfscanf after fwrite on an r+ stream read other bytes". With the change it passes 3 runs out of 3.
  • ctest -R "^(file_streams|guest_standard_streams|guest_fdopendir|guest_filesystem)": 6 of 6 pass, 3 runs out of 3.
  • ctest -R 'stream|stdio|scanf|printf|guest_file|fopen|fseek|wchar|libc|ungetc|guest_fdopendir|guest_filesystem|patched_libraries': 31 of 31 pass.
  • Full build: every target builds except agc_driver_image_array_window_past_layer_limit, which is the alignas(65536) breakage already on main (#8310). --target libs builds. No warnings in the touched files.

Arch Linux (WSL2), GCC 16.1.1, glibc 2.43, Release, BUILD_TESTING=ON, on main b77bd5880:

  • ctest -R "^(file_streams|guest_standard_streams|guest_fdopendir|guest_filesystem)": 6 of 6 pass, 3 runs out of 3.
  • file_streams without the Formatting.cpp change also passes 3 runs out of 3, because glibc's __uflow already switches.

tools/check_conventions.py --base origin/main and tools/check_test_registration.py pass. No title or homebrew run.

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, some AI assistance was used
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4948622715 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8982). Originally posted by [dantdmdarealguy](https://github.com/dantdmdarealguy) on 2026-10-21T08:24:05Z. --- ### What `vfscanf`, which `vscanf` and `scanf` also go through, read from an update stream without switching it to reading first. #8226 added that switch to `fscanf` and the other read functions but not to `vfscanf`. On Windows the UCRT does not switch an `r+`, `w+` or `a+` stream from writing to reading by itself, so a `vfscanf` directly after an `fwrite` read bytes that were still in the write buffer. Without the switch, `fwrite("AB")` then `vfscanf("%3s")` on an `r+` stream over `0123456789` does not return `234`. - `vfscanf_nid_postfix` now calls `if (!stream->SwitchToReading()) return EOF;` after `GetNativeStream`, at the same point and under the same stream lock as `fscanf_nid_postfix`. As in FreeBSD's `__srefill`, pending output is flushed and the stream is marked as reading before the read; a failed flush returns `EOF`. - On glibc, `vfscanf` reads through `__uflow`, which already switches, so Linux behaviour does not change. - `file_streams` gets a `vfscanf` case next to the `fscanf` one. It does `fwrite("AB")` on an `r+` stream over `0123456789`, then `vfscanf("%3s")` through a va_list wrapper, and checks the value read (`234`), the error indicator and the file contents (`AB23456789`). On Windows the test fails on `main` with 0xc0000409, "vfscanf after fwrite on an r+ stream read other bytes", and passes with the change. Follow-up to the review on #8226 (merged). ### Tested Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1742), WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, on `main` `b77bd5880`: - `ctest -R "^file_streams$"` without the `Formatting.cpp` change (test change only): fails 3 runs out of 3 with 0xc0000409, "vfscanf after fwrite on an r+ stream read other bytes". With the change it passes 3 runs out of 3. - `ctest -R "^(file_streams|guest_standard_streams|guest_fdopendir|guest_filesystem)"`: 6 of 6 pass, 3 runs out of 3. - `ctest -R 'stream|stdio|scanf|printf|guest_file|fopen|fseek|wchar|libc|ungetc|guest_fdopendir|guest_filesystem|patched_libraries'`: 31 of 31 pass. - Full build: every target builds except `agc_driver_image_array_window_past_layer_limit`, which is the `alignas(65536)` breakage already on `main` (#8310). `--target libs` builds. No warnings in the touched files. Arch Linux (WSL2), GCC 16.1.1, glibc 2.43, Release, `BUILD_TESTING=ON`, on `main` `b77bd5880`: - `ctest -R "^(file_streams|guest_standard_streams|guest_fdopendir|guest_filesystem)"`: 6 of 6 pass, 3 runs out of 3. - `file_streams` without the `Formatting.cpp` change also passes 3 runs out of 3, because glibc's `__uflow` already switches. `tools/check_conventions.py --base origin/main` and `tools/check_test_registration.py` pass. No title or homebrew run. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes, some AI assistance was used
clairtonmichel commented 2026-10-21 08:49:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by clairtonmichel on 2026-10-21T08:49:26Z.


Tested on AMD Ryzen 7 9800X3D and AMD Radeon RX 7900 XTX (Fedora Linux, Mesa 26.2.5):

  • Compiled successfully with zero errors.
  • Non-blocking send/recv suites (net_dontwait_send, net_dontwait_recv, net_dontwait_duplex, etc.) pass cleanly.
  • Note on edge cases: net_dontwait_blocking_send, sendto, and sendmsg fail with peer never exhausted the send buffers because the local loopback buffer drains so rapidly on high-end host configurations (Ryzen 7) that the test timing condition isn't met. This is likely a host-speed timing artifact rather than a functional regression.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6311942758 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8982#issuecomment-6311942758). Originally posted by [clairtonmichel](https://github.com/clairtonmichel) on 2026-10-21T08:49:26Z. --- Tested on AMD Ryzen 7 9800X3D and AMD Radeon RX 7900 XTX (Fedora Linux, Mesa 26.2.5): - Compiled successfully with zero errors. - Non-blocking send/recv suites (`net_dontwait_send`, `net_dontwait_recv`, `net_dontwait_duplex`, etc.) pass cleanly. - Note on edge cases: `net_dontwait_blocking_send`, `sendto`, and `sendmsg` fail with `peer never exhausted the send buffers` because the local loopback buffer drains so rapidly on high-end host configurations (Ryzen 7) that the test timing condition isn't met. This is likely a host-speed timing artifact rather than a functional regression.
harisibrar1 commented 2026-10-24 13:28:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:28:41Z.

Review: APPROVED


Checked against main 6cd36e6517 and FreeBSD releng/11.4 lib/libc/stdio/refill.c and vfscanf.c:

  • Main still lacks the switch in vfscanf_nid_postfix (fscanf_nid_postfix has it at Formatting.cpp:374), and this branch still merges cleanly despite its age.
  • The description matches __srefill (refill.c:90-97): it flushes pending output, returns EOF if the flush fails, then marks the stream as reading.
  • One non-blocking note: FreeBSD switches lazily, only when vfscanf needs input (_r <= 0), while this PR, like fscanf on main, switches on entry. They differ only for a format that reads no input, such as "%n", where a failed flush gives EOF here and 0 in FreeBSD. That seems fine to leave as it is.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638414068 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8982#pullrequestreview-5638414068). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:28:41Z. **Review:** APPROVED --- Checked against main 6cd36e6517 and FreeBSD releng/11.4 lib/libc/stdio/refill.c and vfscanf.c: - Main still lacks the switch in `vfscanf_nid_postfix` (`fscanf_nid_postfix` has it at Formatting.cpp:374), and this branch still merges cleanly despite its age. - The description matches `__srefill` (refill.c:90-97): it flushes pending output, returns EOF if the flush fails, then marks the stream as reading. - One non-blocking note: FreeBSD switches lazily, only when `vfscanf` needs input (`_r <= 0`), while this PR, like `fscanf` on main, switches on entry. They differ only for a format that reads no input, such as `"%n"`, where a failed flush gives EOF here and 0 in FreeBSD. That seems fine to leave as it is. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!8982
No description provided.