fix(libc): convert scanf integers as FreeBSD does on Windows #9479
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9479"
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 EagleBearerYS on 2026-10-21T23:01:32Z.
What
On Windows the scanf family (
sscanf,vsscanf,fscanfand their callers) translates the guest's length modifiers and hands the format to the host's scanner. Two integer cases differ from FreeBSD 11.4'svfscanf.%d,%i,%o,%u,%xor%Xwithout a length modifier, or withhorhh, goes to the host unchanged. The host converts it with its 32-bitlong, so an out-of-range value is clamped anderrnoset toERANGE. FreeBSD converts every integer withstrtoimaxorstrtoumaxand stores the result truncated to the destination type (vfscanf.cL741-L771).q, FreeBSD's quad length modifier, is not recognized, so%qd,%quand%qxthrowNotImplemented_nid_no_patch("vsscanf format conversion"). FreeBSD's source treatsqlikell(vfscanf.cL529-L531), but the console's own libc doesn't: run natively from three titles, itssscanfreturns 0 for%qdand stores nothing (@JJenkx's comment). The console's behaviour overrides FreeBSD 11.4's source here.Measured with
main's code:"2147483648"%derrno34"3000000000"%ierrno34"4294967296"%uerrno34"100000000"%xerrno34"3000000000"%qdA colour or mask written as
0xFFFFFFFFand read with%iinto aninttherefore comes back as0x7FFFFFFF.ScanWindowsArguments_nid_no_patch(WindowsScanning.hpp) now does two things:qdirective ends the scan there with nothing stored, as the console's libc does for%qd, and the count of conversions before it is returned. What the console does with aqafter other conversions isn't measured.llinto a temporary of the conversion's signedness:long longfor%dand%i,unsigned long longfor%o,%u,%xand%X. A%nafter it shows whether the conversion took place. The value is then stored truncated to the destination'sint,shortorchar, signed or unsigned to match, as FreeBSD's assignment does.A value beyond the 64-bit range still sets
ERANGE, asstrtoimaxdoes there. Linux hands the format to glibc, which already truncates this way but readsqasll; the libc debt file records that difference. No title is claimed.Overlap, merge order agreed: #9087 also adds a check function after
CheckWidePrecisionincore/libs/tests/Formatting.cppand a call at the end ofRunChecks, so the two conflict in that test file. Their library changes are in different files: #9087's inFormatting.cppandWindowsFormatting.hpp, this PR's inWindowsScanning.hpp. This PR merges first, and #9087 then rebases its test file over it, keeping both check functions and both calls; orezraey agreed to that order (comment).Tested
On GitHub's runners from my fork, merged with
mainata6dcea9279(run):CheckIntegerScanWidthsinguest_formattingchecks:%d,%i("0xFFFFFFFF"gives -1),%u,%x,%o,%hd("5000000000"gives -3584) and%hhd("300"gives 44), witherrnoleft alone;ERANGEfor"99999999999999999999"with%d(-1);%uof4294967295,%huof40000and of70000(4464),%hxofffff,%hhuof200and of300(44), and%hhoof377(255);%qdand%qx;%nstill counts after the new conversions, and that a failed conversion leaves its destination unchanged.main'sWindowsScanning.hppguest_formattingfails. With the change it passes 10 runs in a row.guest_formattingpasses withmain's code and, 10 runs in a row, with the change; the code change does not apply there, and glibc already gives these results.core/libs/prx/libcandcore/libs/prx/libkernelpasses: 115 and 197 on Linux, 142 and 207 on Windows.b39956b4e1, after MarcZX10's review (run):guest_formattingpasses 10 runs in a row on both. The new unsigned checks also pass with the previous head, since both hosts store the same bits there. In that run's Windows libkernel suite,guest_raise_in_system_calltimed out after 30 s with and without the commit.mainatda54c652e3(8fab12951e, no conflicts, the PR's own diff unchanged; run):guest_formattingfails on Windows withmain'sWindowsScanning.hpp; with the change,guest_formattingandguest_wide_formattingpass 10 runs in a row on both runners. The libc and libkernel suites pass on both (116 and 197 on Linux, 143 and 207 on Windows).tools/check_conventions.py --base origin/mainpasses.libc.prx, by @JJenkx (comment): head8fab12951emerged ontomainf1db096084, on Windows 11 with GCC 15.2.0 UCRT, Release. A loader runs a title's decryptedlibc.prxnatively and compares itssscanfwith this build's on 60,000 random inputs per format, checking the result, the value, the%ncount anderrno. The copies bundled with three titles give the same counts.%d,%u,%hdand%hhdmatch the console on all 60,000 inputs each. Onmain16,892, 7,541, 7,455 and 7,499 of them differ.%iand%xgo from 20,669 and 14,643 differences onmainto 3,521 and 3,597.%o,%lldand%3dmatch on both trees, and%lliand%llxdiffer as much as onmain.%iand%xdifferences left, here and onmain, are one case: a0xprefix with no hexadecimal digit after it, which the console treats as a matching failure. That matching rule is outside this PR.ctest -R 'scan|format|stream|libc|wide|string'selects 44 tests, and all pass in three runs of three.sscanfon fixed inputs, by @JJenkx (comment): thelibc.prxbundled with PPSA07923, PPSA13934 and PPSA35169, loaded natively on Windows, into a 64-bit destination filled with0x55…; all three give the same results.errno:2147483648and-2147483649with%dgive0x80000000and0x7fffffff,4294967296and-1with%ugive 0 and0xffffffff,99999999999with%xgives0x99999999,300with%hhdgives0x2cand70000with%hdgives0x1170. Only the 64-bit ones saturate withERANGE:%lldand%llupast their range.%qdof5returns 0 and stores nothing, which this PR now does too (a809ad7d2f).a809ad7d2f, theqchange (run):CheckIntegerScanWidthstakes those rows, and%qdof5must return 0 with the destination unchanged (Windows only, since Linux goes to glibc).WindowsScanning.hpp:guest_formattingfails. On Linux it passes before and after, as the change doesn't apply there.guest_formattingandguest_wide_formattingpass 10 runs in a row on both runners, 2 of 2 each, none skipped. The libc and libkernel suites pass on both, run withAPS5_SKIP_TIMING_TESTS=1, which relaxesprecise_wait's median bounds on GitHub's hosted runners.fscanfon a file in/data.%don99999999999returns 1 and stores0x4876e7ffwitherrno0,%hhdon300stores0x2c,%uon-1stores0xffffffff, and%lldon-9223372036854775809storesINT64_MINwitherrno34: the FreeBSD conversion this PR gives Windows, as they say. They also report two differences from glibc outside this PR:%ion0xreturns -1, and%xon0xZreturns 0 withZnext, where glibc returns 1 with 0 stored for both.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 MarcZX10 on 2026-10-21T23:03:19Z.
Code review location:
core/libs/prx/libc/include/WindowsScanning.hpp; line 22Could this temporary and the final store preserve the conversion signedness? For
%u,%oand%x, the translated format is%llu, but this passes&valueas along long*;scanfrequires anunsigned long long*. The post-scan stores likewise always usechar*,short*orint*, including for%hhu,%huand%udestinations. These mismatched variadic pointer types and typed stores are undefined behavior even where the host uses identical representations. Please keep a signed/unsigned temporary matching the rewritten conversion and store through the matching destination type; add%hu/%hhucoverage, including values above the corresponding signed range.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T23:03:23Z.
Review: CHANGES_REQUESTED
The unsigned narrow-integer path has undefined behavior: the rewritten
%llu/%llx/%lloformats are given along long*, and the results are stored through signedchar*,short*orint*, including for unsigned destinations. I left an inline comment at the temporary declaration with the required signedness fix and tests. The current hosted Linux/Windows build/test jobs are skipped, and the relinker matrix is still running; I have not run a local test.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T23:13:26Z.
Follow-up to my review: the relinker matrix, register-context, write-watch, eligibility, conventions, labels, and report jobs have now completed successfully. The full Linux and Windows build/test jobs were skipped by the PR workflow. The signed/unsigned temporary and destination-pointer issue remains, so the requested code change is unchanged.
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-21T23:25:24Z.
Code review location:
core/libs/prx/libc/include/WindowsScanning.hpp; line 22Thanks, @MarcZX10, agreed.
b39956b4e1keeps anunsigned long longtemporary for%u,%o,%xand%X, so the rewritten%llu,%lloand%llxget the pointer type they require, and stores those throughunsigned char*,unsigned short*orunsigned int*.%dand%ikeep thelong longtemporary and store throughsigned char*,short*orint*.CheckIntegerScanWidthsgains%uof4294967295,%huof40000and of70000(4464),%hxofffff,%hhuof200and of300(44), and%hhoof377(255). On GitHub's runners (run)guest_formattingpasses 10 of 10 on Linux and Windows. The new checks also pass with the previous head, since both hosts store the same bits there, so they pin the values rather than show a failure.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T03:37:47Z.
Review: CHANGES_REQUESTED
The latest commit fixes the signedness issue from my earlier review:
%u/%o/%xnow scan through anunsigned long longtemporary and store through unsigned destination types, while signed conversions retain signed temporaries/destinations. The added unsigned narrow cases cover truncation, and the author reports the focused Windows/Linux formatting tests and hosted relinker/platform matrices pass; full Linux/Windows product jobs were skipped.One repository coordination blocker remains. #9087 is open and edits the same
Formatting.cpptest function/call site. The body says whichever PR merges second needs a rebase, but the required merge order/dependency is not recorded and I do not see agreement from #9087's author about this overlap. Please coordinate and state the agreed order in theDepends onchecklist (or confirm the changes are order-independent and record the agreed rebase owner), then preserve both test sets. No local tests were run here.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T03:37:48Z.
The signedness correction in the latest head resolves my prior code blocker. The remaining request is merge coordination with #9087 because both change
Formatting.cpptest registration/body; please get agreement on sequencing/rebase responsibility and reflect the order inDepends onbefore requesting another review. Current hosted relinker and platform check matrix is green, while full product builds were skipped.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T03:57:00Z.
Thanks, @MarcZX10. orezraey agreed on #9087 that this PR merges first and #9087 then rebases its test file over it, keeping both check functions and both calls in
RunChecks(comment). The Overlap section and the Depends on line now say so.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:30:23Z.
Review: DISMISSED
The latest head fixes the earlier signedness/undefined-behavior issue: unsigned conversions use an
unsigned long longtemporary and unsigned destination stores, while signed conversions use signed types. The extensive%d/%i/%u/%x/%o, narrow-width,%q,%n, overflow and failed-conversion cases are covered in the reported Windows/Linux focused runs. The #9087 ordering blocker is now resolved: both authors agree #9479 lands first and #9087 rebases its test changes afterward; the PR records that order. I am requesting changes only because the tested branch remains based ona6dcea9279while current upstream main isab347d144d. Please rebase, preserve both Formatting test sets, and rerunguest_formatting, conventions and the overlap scan on current main. Hosted full Linux/Windows product jobs are skipped; I did not run local tests.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T06:00:42Z.
Thanks, @MarcZX10. The branch now has
mainatda54c652e3merged in (8fab12951e), with the PR's own diff unchanged. On GitHub's runners (run)guest_formattingandguest_wide_formattingpass 10 runs in a row on both, andguest_formattingfails on Windows withmain's code; the libc and libkernel suites pass on both, and conventions pass. Tested now says so.