feat(libc): decode non-ASCII scanset members and non-BMP characters in the guest wide scanf #9340
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9340"
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 kanza-cm on 2026-10-21T18:50:52Z.
What
Two gaps in the guest wide
scanfon Linux, both listed in the technical debt:%l[...]read its scanset byte by byte, so a member outside ASCII threw instead of matching. The scanset is now decoded as UTF-8 and compared as code points, which also turns a range such asà-êinto a real code point range._scapacities count units, and a character that does not fit the destination leaves an empty result, as the capacity path already did.The remaining throwing cases are unchanged: input that is not UTF-8, positional arguments next to a wide conversion, and wide conversions in
fscanf. The debt entry no longer lists the two cases this fixes.Tested
-DBUILD_TESTING=ON):guest_formattingwith the new cases,guest_wide_formatting,guest_wide_stream_scanandguest_scan_countpass.core/libs/tests/Formatting.cpp:%l[é],%l[à-ê]with a multibyte range,%l[^é], a mixed ASCII/non-ASCII member,%lsof an emoji,%2lsstopping after one character, and%lsinto a 4-unit_sbuffer where one emoji fits and two do not.libcstill builds. This block is#ifndef _WIN32, so Windows CI does not exercise it; the Windows path hands%ls,%lcand%l[to the host CRT.tools/check_conventions.py --base origin/mainpasses.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 ycbuzi on 2026-10-21T22:41:49Z.
Windows run of dea162e177 merged onto
maina6dcea9279(e774d96f1a, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release,BUILD_TESTING=ON.Built
libcandguest_formatting_testswith no errors or warnings.ctest --repeat until-fail:2onguest_formatting: passes on both repeats. Not run: a title.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-21T23:39:51Z.
Linux run with a wider set of inputs. This is the only host where the change runs: the code is
#ifndef _WIN32, so the Windows run on this PR builds it but doesn't execute it.Head
dea162e177merged onto maina6dcea927(clean). WSL2 Ubuntu 24.04, GCC 13.3.0, Release.guest_formatting,guest_wide_formatting,guest_wide_stream_scan,guest_scan_count: 4/4, 3 runs.ScanWideField.cpp,ScanWideField.hppandFormatting.cppfrom main,guest_formattingaborts (scanf 16-bit wide scanset with a character outside ASCII not implemented).check_conventions.py --base a6dcea927: passes.Probe:
sscanf/sscanf_sinto achar16_t[8]pre-filled with0xa5a5, showing the code units written. All of these threw on main:%l[ê-à](reversed range) oné: 0, nothing written.%l[]é]on]éx:005d 00e9 0000.%l[^]é]onabé:0061 0062 0000.%l[a-]on-a:002d 0061 0000.%l[😀]on two emoji andx:d83d de00 d83d de00 0000.%l[à-😀]onU+FFFF 😀:ffff d83d de00 0000.%1lsand%1l[^x]on an emoji:d83d de00 0000(width counts characters).sscanf_s%ls, capacity 2 on an emoji: 0 with0000; capacity 3:d83d de00 0000; capacity 3 ona😀: 0 with0000.%lson U+10FFFF:dbff dfff 0000. UTF-8-encoded surrogateED A0 80: still throws (not UTF-8).%lcwrites two units for one character.%lcon an emoji writesd83d de00, and%2lcon two emoji writesd83d de00 d83d de00. So a guestchar16_t c; sscanf(s, "%lc", &c)gets one unit written pastc, and achar16_t c[2]with%2lcgets two.sscanf_sis bounded (capacity 1 returns 0 and writes0000). The plain forms have no capacity, so this is a write past the guest's object whenever the input is outside the BMP. Throwing for a non-BMP%cfield in the non-_sforms, as before, would avoid that until the console's behaviour is known.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by kanza-cm on 2026-10-22T12:18:30Z.
Thanks for the probe, it found a real defect: an unsized
%lcfield has no capacity to bound, so a supplementary character wrote its second unit past the caller's object. Fixed in 9143eee65: the unsized field throws for a character outside the BMP again, while the_sforms keep writing both units because their capacity is already checked in units. Two cases were added to the refusal test (%lcand%2lcon an emoji), the debt entry now records the unsized field, andguest_formattingwas re-run on Windows and on Ubuntu 24.04 under WSL2 after the change. All of your other probe cases (%l[😀], reversed and negated ranges,%1ls,%lson U+10FFFF,sscanf_scapacities) keep their results.Imported from boykopovar/AnyPS5. Originally posted by kanza-cm on 2026-10-22T12:19:10Z.
Ready for the full run, whenever a collaborator has a moment for the manual dispatch:
State:
MERGEABLE, conventions/intake/labels green, three commits onfeat/libc-wide-scanset-utf8.BUILD_TESTING=ON):libcandguest_formatting_testsbuild clean;guest_formattingpasses. Reported by @ycbuzi.guest_formatting,guest_wide_formatting,guest_wide_stream_scanandguest_scan_countpass, with a negative control onmain. Reported by @wachi35676, whose probe also found the unsized%lcoverflow now fixed in9143eee65.tools/check_conventions.py --base origin/mainpasses.Note for sequencing: the overlap comment lists #7386 and #9087 as touching
core/libs/prx/libc/src/Formatting.cpp. This change does not build on either; if they land first, I will rebase.Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-22T14:41:30Z.
Windows run of 9143eee652 merged onto
main8b3a6ba88d(9c2962a5b9, clean). Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release,BUILD_TESTING=ON.Built
libcand the four test executables with no errors or warnings.ctest --repeat until-fail:2onguest_formatting,guest_wide_formatting,guest_wide_stream_scanandguest_scan_count: all four pass on both repeats.On the Windows line in the summary above: as @wachi35676 noted, the wide-scan path is inside
#ifndef _WIN32inFormatting.cpp, so on Windows this change is compiled out. A negative control confirms it here: withFormatting.cpp,ScanWideField.cppandScanWideField.hpptaken frommain, the same four tests still pass on Windows. So my Windows runs show that this PR builds and keeps these tests passing on Windows, not that its change runs there; Linux is the host that exercises it. Not run: a title.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:47:46Z.
Review: COMMENTED
Checked against main
39a36bbec8and FreeBSD 11.4 lib/libc/stdio/vfscanf.c / vfwscanf.c:_sforms have the same problem the unsized%lcone had.wantedcounts characters (Formatting.cpp:196/205) andlimitisULONG_MAXoutside_s, but each non-BMP character appends 2 units. Reading the code,char16_t buf[3]; sscanf("😀😀", "%2ls", buf)writesd83d de00 d83d de00 0000, which is 5 units for a buffer sized width+1 (wachi35676's probe shows the same for%1ls: 3 units). On main this throws. FreeBSD'sconvert_wstring/convert_wcclstore onewchar_tper character, so width+1 is always enough there; with a 16-bitwchar_tI don't know what the console does. Until that's known, would it work to throw for a non-BMP character in a width-limited non-_s%ls/%l[field, as the%lcpath already does? The same applies to%2l[...].à-êaren't what either reference does. FreeBSD 11.4vfscanf.ctests%l[through a 256-entry byte table (__sccl, thenccltab[wctob(wi)]), andvfwscanf.c'sincclis a plainwmemchrwith no ranges. Could the debt entry say that scanset members and ranges are compared as code points, unverified on the console?%l[ê-à], andz-aalready on main) matches nothing, while FreeBSD's__sccltreats it as the literal membersê,-,à. Also, a suppressed%*lcthrows on a non-BMP character even though nothing is written. Neither blocks the PR.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:19:29Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: ensure the
%l[conversion reaches the guest wide scanner for non-ASCII scansets.What I ran
Reviewed head
3f2fa3b280504410e7ff6ed94dfd435992f1d92c. No tests or build commands were run; repository access was read-only. I read the prepared description, commit list, manifest, diff, and all five changed files listed in the manifest.Results
Findings
Blocking
core/libs/prx/libc/src/Formatting.cpp:45—RequiresGuestFormattingroutes%lsand%lcto guest formatting, but itslbranch omits%l[. Consequently%l[é]and the other new non-ASCII scanset cases are handled by hostsscanf, bypassingInWideScanset; the claimed support is not active for ordinarysscanfcalls. Include[among the wide conversions recognized here and check the dispatch path for the wide scanset.Non-blocking
None.
Not verified
No tests/builds, Windows behavior, or console behavior were verified. No changed files were excluded: all five files in the manifest were read. The review-folder
README.mdand ruling file were not read; the ruling instructions were supplied in the prompt.To get this to a good state
Route
%l[through the guest wide scanner, then run the newguest_formattingcases and the related wide-scan tests on the updated head.Pull request closed