fix(libc): switch update streams to reading in vfscanf #8982
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8982"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by dantdmdarealguy on 2026-10-21T08:24:05Z.
What
vfscanf, whichvscanfandscanfalso go through, read from an update stream without switching it to reading first. #8226 added that switch tofscanfand the other read functions but not tovfscanf. On Windows the UCRT does not switch anr+,w+ora+stream from writing to reading by itself, so avfscanfdirectly after anfwriteread bytes that were still in the write buffer. Without the switch,fwrite("AB")thenvfscanf("%3s")on anr+stream over0123456789does not return234.vfscanf_nid_postfixnow callsif (!stream->SwitchToReading()) return EOF;afterGetNativeStream, at the same point and under the same stream lock asfscanf_nid_postfix. As in FreeBSD's__srefill, pending output is flushed and the stream is marked as reading before the read; a failed flush returnsEOF.vfscanfreads through__uflow, which already switches, so Linux behaviour does not change.file_streamsgets avfscanfcase next to thefscanfone. It doesfwrite("AB")on anr+stream over0123456789, thenvfscanf("%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
mainwith 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, onmainb77bd5880:ctest -R "^file_streams$"without theFormatting.cppchange (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.agc_driver_image_array_window_past_layer_limit, which is thealignas(65536)breakage already onmain(#8310).--target libsbuilds. No warnings in the touched files.Arch Linux (WSL2), GCC 16.1.1, glibc 2.43, Release,
BUILD_TESTING=ON, onmainb77bd5880:ctest -R "^(file_streams|guest_standard_streams|guest_fdopendir|guest_filesystem)": 6 of 6 pass, 3 runs out of 3.file_streamswithout theFormatting.cppchange also passes 3 runs out of 3, because glibc's__uflowalready switches.tools/check_conventions.py --base origin/mainandtools/check_test_registration.pypass. No title or homebrew run.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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):
net_dontwait_send,net_dontwait_recv,net_dontwait_duplex, etc.) pass cleanly.net_dontwait_blocking_send,sendto, andsendmsgfail withpeer never exhausted the send buffersbecause 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.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:28:41Z.
Review: APPROVED
Checked against main
6cd36e6517and FreeBSD releng/11.4 lib/libc/stdio/refill.c and vfscanf.c:vfscanf_nid_postfix(fscanf_nid_postfixhas it at Formatting.cpp:374), and this branch still merges cleanly despite its age.__srefill(refill.c:90-97): it flushes pending output, returns EOF if the flush fails, then marks the stream as reading.vfscanfneeds input (_r <= 0), while this PR, likefscanfon 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.