Repository navigation
feat(libc): scan 16-bit wide conversions in sscanf and vsscanf on Linux - #6017
Closed
KernelPanicNT wants to merge 1 commit into
Closed
KernelPanicNT wants to merge 1 commit into
KernelPanicNT wants to merge 1 commit into
Conversation
11 tasks done
This was referenced Oct 14, 2026
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
On Linux, sscanf, vsscanf, sscanf_s and vsscanf_s threw for %ls, %lc, %l[, %S and %C because glibc stores a 32-bit wchar_t per character into the guest's 16-bit buffer. A format with one of these conversions is now scanned directive by directive in ScanGuest: each wide directive reads its bytes as the narrow directive does and widens every byte through this library's mbrtowc, which is what FreeBSD 11.4's vfscanf.c reads in the single-byte C locale of none.c. The byte buffer for a wide directive is bounded by the remaining input, not only by the guest's width. As in FreeBSD, %S and %C are wide whatever length modifier precedes them. The format pre-scan and ScanGuest recognise the q length modifier as FreeBSD does, so %lS, %hC and %qS are found too instead of reaching glibc, and %qd or %qn in a format scanned by ScanGuest stores a long long. On Windows, ScanGuest hands q to the host as ll for integer conversions. A narrow s, c or [ is handed to the host without any length modifier other than l, so %lls, %qs or %Lc in a format scanned by ScanGuest are narrow, as in FreeBSD, instead of reaching glibc as wide. A wide conversion combined with a positional argument or the ' flag still throws. fscanf keeps throwing for wide conversions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 15, 2026
This was referenced Oct 15, 2026
KernelPanicNT
force-pushed
the
feat/libc-sscanf-guest-wide-conversions
branch
from
October 15, 2026 13:16
178e811 to
f4fa4a3
Compare
11 tasks done
This was referenced Oct 15, 2026
This was referenced Oct 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
On Linux,
sscanf,vsscanf,sscanf_sandvsscanf_sthrow for the 16-bit wide conversions%ls,%lc,%l[,%Sand%C(RejectGuestWideScan, added in #3984): glibc would store a 32-bitwchar_tper character into the guest's 16-bit buffer. This PR scans them instead.Source of the behaviour. No console measurement is involved. The model is the FreeBSD 11.4 libc (
release/11.4.0, commit8c63994) in its C locale, which this library's multibyte functions already follow:vfscanf.cL594-L612:%Sand%Care%sand%cwith thelflag, which they set whatever length modifier precedes them, so%lSand%hCare wide too. L683-L728: withl,%c,%[and%scallconvert_wchar,convert_wcclandconvert_wstring(L161-L308). Each reads one character at a time through the locale'smbrtowc(__fgetwc_mbs).%lsstops atiswspaceand is terminated.%l[testsccltab[wctob(wi)], and an empty match leaves the buffer untouched.%lcreadswidthcharacters (default 1) with no terminator.none.cL99-L113: in the C localembrtowcconsumes one byte and stores its value. So a wide directive reads exactly the bytes the narrow directive reads, andiswspace/wctobagree withisspaceand the narrow scanset.mbrtowc(fix(runtime): run converted SDK samples through clean C++ shutdown #98) does the same, and the scanner calls it for every byte.vfscanf.cL804-L805: an input failure returnsEOFonly when no conversion, suppressed or not, has completed.That the console's
libc.prxscans this way is assumed, not checked: the console's locale and scanner were not read.Changes:
sscanfandvsscanfon Linux send a format with a 16-bit wide conversion toScanGuest, the per-directive scanner thatsscanf_sandvsscanf_salready use (feat: boot PPSA04263 with its own sce_module, into the story mode load (supersedes #144) #153 introduced it, feat(libc): implement vsnprintf_s and vsscanf_s #3046 madevsscanf_suse it). Formats without one still go to glibc unchanged.ScanGuest, a wide directive is scanned by glibc as its narrow form (%S→%s,%C→%c,%l[→%[) into a temporary byte buffer. Each byte is then widened throughmbrtowcinto the guest's buffer.%lsand%l[are terminated,%lcis not. In the_sforms the capacity counts 16-bit units, and a string that does not fit stores an empty string, as%salready does. The temporary byte buffer is bounded by the remaining input, so a large guest width (%2000000000ls) does not size it.ScanGuestrather than glibc. The one visible difference: when input fails after only suppressed conversions, the call returns 0, as FreeBSD (L804-L805) and C17 do, where glibc returnsEOF. The debt entry records this.%Sand%Cafter one (%lS,%hC). It also skips the contents of a%l[scanset, which matters now that the pre-scan continues past the first wide conversion instead of throwing there.ScanGuestscans%Sand%Cas wide whatever length modifier precedes them, as FreeBSD does (L594-L596, L607-L609). Before, a format whose only wide directive was%lSor%hCwent to glibc, which stored a 32-bitwchar_tper character. The pre-scan andScanGuestalso accept theqlength modifier, as FreeBSD does (L529-L531,qislong long), so%qSand%qCare wide and%qdor%qnin a format scanned byScanGueststores along long.ScanGuesthands a narrows,cor[to the host without any length modifier other thanl, so%lls,%qsor%Lcthere is narrow, as in FreeBSD, whereq,ll,L,j,zandtonly change integer and floating conversions. Before,sscanf_sandvsscanf_spassedll,L,j,zandton these conversions to glibc, which scans them as wide and wrote a 32-bitwchar_tper character into a buffer whose capacity the guest gave in bytes.Still throwing, and why:
%1$ls) or the'flag throwsstd::runtime_error, becauseScanGuestimplements neither.fscanfkeeps throwing for wide conversions. It reads a host stream, so it needs a separate source; that is the follow-up, feat(libc): scan 16-bit wide conversions in fscanf on Linux #6018, which is stacked on this PR and should be merged after it.sscanfandvsscanf, so there%lls,%Ls,%qs,%js,%zsand%ts(and these modifiers withcor[) are still scanned as wide with a 32-bitwchar_tper character, as on the base. These formats are undefined behaviour in C; the debt entry records them.%ls,%lcand%l[still go to the host, whosewchar_tis 16 bits. The only Windows change is inScanGuest, whichsscanf_sandvsscanf_suse there:qis handed to the host asllfor integer conversions, ands,cand[lose any length modifier other thanl.%Sand%Cstill throw insscanf,vsscanfandfscanfthere, in the format translation.Tests: on Linux,
guest_formattingno longer expects these formats to throw. It now checks%ls,%S,%3lcafter a suppressed%*s,%C,%l[a-z],%lS,%hC, a width, bytes above 0x7f (\xe9,\xff→ U+00E9, U+00FF), a mix with%d, a suppressed%*ls,vsscanf, empty and blank input (EOF), an empty%l[match (0, buffer untouched), and the_scapacity (fit,%lcwith capacity 1, overflow → empty string). It also checks%qS,%qC, a width of 2000000000 on short input for%lsand%lc,%ls %qd%qn(two conversions, along longand along longcount), the variadicsscanf_swith%ls %C, and%qs %ll[a-z] %Lcthroughsscanf_s, which must store narrow bytes. Guard units after every result must stay untouched.%2$C,%'lsand%1$lsstill throw without writing, and%1$lsis checked throughsscanf,vsscanf,vsscanf_sandsscanf_s.Progress (
tools/progress.pyon the branch basee27ff7cand on the branch head): system libraries 3269/3679 → 3270/3679 (+vsscanf_nid_postfix). The tool countedvsscanfas unimplemented because its single definition reachedNotImplemented_nid_no_patchon Linux.sscanf,sscanf_sandvsscanf_salready counted as implemented through their Windows definitions.vsscanfnow counts as implemented because the one case still unimplemented on Linux, a wide conversion with a positional argument or the'flag, throwsstd::runtime_errorrather than callingNotImplemented_nid_no_patch.Tested
Head
f4fa4a38fbffa8f19a3cdb6ec4b593c3da919e10, basee27ff7c545cb5ac669612567d853956ffa02ebe6.BUILD_TESTING=ON, AMD EPYC 7763 (GitHub Codespaces), tests run natively):guest_formatting_tests,guest_scan_count_testsandguest_string_testseach pass in 5 of 5 runs, the test executable being run directly five times.core/libs/prx/libc/src/Formatting.cppwith the base version:guest_formatting_testsfails withscanf 16-bit wide conversion on Linux not implemented;guest_scan_count_tests,guest_string_testsstill pass there, as expected for regression coverage. Restoring the PR source and rebuilding,guest_formatting_tests,guest_scan_count_testsandguest_string_testseach pass in 5 of 5 runs again.mainatb08db6a093f440af697d71ea907cec5f675fcdc5, WinLibs MinGW-w64 GCC 15.2.0, Release,BUILD_TESTING=ON: 1278/1279 tests pass, includingguest_formatting,guest_scan_countandguest_strings. The one failure,prx_thread_local_destructors, reports athread_localdestructor inlibSceAgcDriver(traceDrawKeyLookup), whichmaingained in de2ddda the same day; this PR does not touch that library. The relinker (Linux, Windows, macOS), register-context and write-watch jobs of the same run pass.git diff --checkpass. The Linux job of the Build workflow now targets the project's self-hosted runner, so it was not run on the fork; no title was run.Duplicate and overlap check
ScanGuestplus per-byte widening. The design overlaps with that PR; credit to @Gashrod1. feat(libc): scan 16-bit wide conversions on Linux #5285 by @Nixitov (closed 2026-10-14) decoded UTF-8 into UTF-16 instead, which does not match the C locale ofnone.c. feat(libc): scan 16-bit wide conversions in fscanf on Linux #5291 by @Nixitov (closed 2026-10-14) was an earlier attempt at thefscanffollow-up and changed the sameRejectGuestWideScanandfscanflines as this PR. fix(libc): retain suppressed scanf conversions before EOF #4966 by @Dripl1x (closed 2026-10-14) made the same change to return 0 when input fails after only suppressed conversions, at these call sites; credit to @Dripl1x for that part. A maintainer closed all four with the same comment: "Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down." It asks to reopen with the title ID and the log line once a title stops on the change. This PR names no title either (see Unblocks). fix(libc): throw for 16-bit wide scanf conversions on Linux #3984 by @zmeizy introduced the throw this PR replaces.sscanf,vsscanf,sscanf_sandvsscanf_s, their NIDs (1Pk0qZQGeWo,UTrpOVLcoOA,24m4Z4bUaoY,tfNbpqL3D0M) and "wide scanf". No open PR implements wide scanning.gh pr diffand ran a three-way merge (git merge-file) of each PR head against this branch, per file, from the PR's merge base. None touchesScanGuest,RejectGuestWideScanor the scanf exports.vasprintf/vfprintfchecks inFormatting.cpp.vfprintf,fscanf,printf,libc_printf,vprintfandputs.core/libs/tests/Formatting.cpp.core/libs/tests/Formatting.cppand another line ofdocs/dev/debt/libc.md.docs/dev/debt/libc.md.#include <cstdio>incore/libs/tests/Formatting.cpp, where this PR adds#include <string_view>.collate<char>::_Getcatline ofdocs/dev/debt/libc.md, directly above the scanf line this PR rewrites.core/libs/tests/Formatting.cpp.mainindocs/dev/debt/libc.md; this PR does not change that.e27ff7cand merges intoe27ff7cwithout conflicts.Checklist
e27ff7c; 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)🤖 Generated with Claude Code