feat(libc): scan 16-bit wide conversions on Linux #7386
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7386"
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 peeldealer on 2026-10-18T04:00:59Z.
What
On Linux,
fscanf,sscanf,vsscanf,sscanf_sandvsscanf_sthrew throughNotImplemented_nid_no_patchfor the 16-bit wide conversions%ls,%lc,%l[,%Sand%C, because glibc'swchar_tis 32 bits. A format with any of these conversions now goes through the guest scanner, the way the printf family already sends wide formats to the guest formatter. The scanner stores each input byte as one 16-bit unit of the same value, as the guest's C-localembrtowcconverts it. A format without these conversions still goes to glibc unchanged.ScanGuestnow reads from a buffer (BufferInput) or aFILE*(StreamInput), sofscanftakes the same path assscanf.%ncounts characters across both kinds of input.%lsskips leading white space and reads non-white-space bytes up to the width.%lcreads the width (default 1) without skipping or terminating.%l[reads the scanset.sscanf_sandvsscanf_sapply their size argument to wide fields in 16-bit elements, with the same overflow handling as the narrow fields. This path is shared, so it also applies on Windows.%2$C,%'ls) throwsstd::runtime_error, as before.fscanfandvsscanfexports no longer reachNotImplemented_nid_no_patch, which moves the libraries count from 3432 to 3434.docs/dev/debt/libc.md. Bytes above 0x7F and locales other than C are not measured on the console.Tested
Ubuntu 24.04 (WSL2), GCC 13.3.0, Release, no title or homebrew.
guest_formattingandfile_streamscover:sscanf,vsscanf,sscanf_sandvsscanf_swith%ls,%lc,%l[,%Sand%C, plus width, suppression,%n, a byte above 0x7F, EOF and matching failure;fscanfon a file stream, including%nand the stream position afterwards.mainand pass with this change.guest_scan_count,guest_wide_formatting,guest_wide_conversionguest_standard_streams,guest_standard_streams_closed_stdoutfile_streams,guest_formatting,guest_stringsguest_allocator_failures,guest_sysmodule,guest_sysmodule_maps_scanguest_kernel_stack_protect,guest_memory,guest_mmap_fixed_anonymous,guest_append_writessscanf,vsscanfandfscanfand through the guest scanner for the_sforms. The byte-above-0x7F check is Linux-only.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 oneandonlydean on 2026-10-18T04:23:43Z.
Thanks. Linux is green, but on Windows
guest_formattingandfile_streamsabort withvsscanf format conversion not implemented(CI run 39096164461). On Windows,sscanf/vsscanf/fscanfgo throughScanWindowsArguments_nid_no_patch(core/libs/prx/libc/include/WindowsScanning.hpp:35), which accepts onlydiouxXaAeEfFgGcspn[.%ls,%lcand%l[pass, but%Sand%CreachNotImplemented_nid_no_patch, so"%S %C"in Formatting.cpp and"%*ls %C"/"%3S"in FileStreams.cpp throw there. Either let the shim acceptS/Casls/lc(UCRT'swchar_tis 16 bits, which the%lspath already relies on; this also matches the_sforms, which now accept them on Windows) and update the debt sentence, or guard those cases with#ifndef _WIN32and check that they still throw on Windows. The first keeps both platforms consistent.Imported from boykopovar/AnyPS5. Originally posted by peeldealer on 2026-10-18T05:52:59Z.
Thanks, I took the first option.
ScanWindowsArguments_nid_no_patchnow passes%Sand%Cto the host as%lsand%lc, so both platforms accept the same conversions. A length modifier on them, such as%lS, still reachesNotImplemented_nid_no_patch. The debt sentence inlibc.mdnow covers this.I couldn't run the Windows tests here. What I checked:
WindowsScanning.hppcompiles with MinGW-w64 (-fsyntax-only).%S %C→%ls %lc;%*ls %C→%*ls%n %lc;%3S→%3ls;%ls,%l[a-z],%d %sand%ldtranslate as before;%lSthrows.guest_formattingandfile_streamsstill pass on Linux.Could you re-run the Windows build?
Imported from boykopovar/AnyPS5. Originally posted by peeldealer on 2026-10-18T09:41:01Z.
I ran the repository's Build workflow on my fork for this head,
06e6d3e99, merged intomain, so the%S/%Cfix has a Windows result. The run usedwindows-latest, the WinLibs GCC 15.2.0 toolchain and the full configure, with the Linux job off because it needs the self-hosted runner.Result: 1472 of 1472 tests passed in the Windows job, including
guest_formattingandfile_streams, which aborted in CI run 39096164461.Run: https://github.com/peeldealer/AnyPS5/actions/runs/39113627972
The same run's relinker, register-context and write-watch jobs passed too, on Linux, macOS and ASan.
Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T21:52:59Z.
Heads-up before this is merged: GitHub shows no conflict, but the merge with current
mainno longer builds on Linux.#8292 (merged today as
4b527adf05) addedvfscanf_nid_postfixtocore/libs/prx/libc/src/Formatting.cpp, and its Linux branch callsRejectGuestWideScan(format). This PR renames that helper toHasGuestWideScan, so in the merge of2b64ed4286intomain6830c91641(git merge-tree --write-tree origin/main 2b64ed4286→ tree423212e5ef), line 679 ofFormatting.cppcalls a function that no longer exists.git grep RejectGuestWideScanon that tree finds only this call:The earlier CI runs on this PR predate #8292, so they could not show it. Rebasing onto current
mainand sending the Linux path ofvfscanf_nid_postfixthrough the stream path thatfscanf_nid_postfixuses here would fix the build. It would also givevfscanf,vscanfandscanfthe same%ls/%Shandling on Linux asfscanf.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T22:12:27Z.
Review: CHANGES_REQUESTED
The stream lookahead can require more pushback than the portable stdio contract guarantees.
PeekWideScanreads up to four bytes, then restores them by callingStreamInput::Backrepeatedly; each call usesstd::ungetc, and this path is enabled for every non-Windows host, including macOS. POSIX guarantees one byte of pushback and allows laterungetccalls to fail without an intervening read or positioning operation (POSIXungetc). This can make valid multibyte wide input fail or leave the stream partially restored on a supported host. Please keep lookahead bytes in an internal buffer (or use another mechanism that does not depend on multiple consecutiveungetccalls), and add/ run the multibyte stream case on the supported non-Windows platforms.Pull request closed