fix(libc): wrap narrow integers in sscanf_s as titles' libc.prx does #12507
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12507"
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 JJenkx on 2026-10-28T16:26:44Z.
Refreshed against main
0263899e996f26b165aed37f6b5a616dd1d4c6bb; current headaa8c1b58be70ac9b5305a8998cbf724b6ea48d69retains previous public head0b3eb0b3e53a8faa1f01fd01a6de4061247ab308and main as ancestors. The conflict resolution preserves the topic behavior and current-main ordinary-scan/input-failure handling and fixtures.The same five fixtures (
guest_formatting,guest_scan_count,guest_wide_stream_scan,guest_wide_formatting,guest_crt) pass three times on main and on this head: 15 executions per arm, zero failures/skips. Public-header audit, conventions and whitespace checks pass. These are actual scoped libc/kernel/Ampr owners and selected fixtures with existing frozen ZArchive/zstd providers, not a whole-project build. No new Windows, title-unblock or performance claim.Raw build/test/JUnit logs, commands, source patches, conflict resolutions and binary hashes, SHA-256
fd8ddb6a38a010916d71698df9cb60011e9800fe14db63baa9f4eca21e6b8e4f(download verified). The batch has 87 successful fixture executions in total; setup failures and corrected final qualification are retained in the archive.Previous report and contribution template, with historical pins preserved:
What
sscanf_s(and the other_sscanners that go throughScanSource:vsscanf_s,fscanf_s,vfscanf_s,scanf_s,vscanf_s) handed a narrow integer conversion to the host at the destination's width. On an out-of-range value the host saturates and setsERANGE. Titles' ownlibc.prx(PPSA07923 and PPSA05916 agree) instead reads the field as a 64-bit value and keeps its low bits, leavingerrnoalone:61439ebdf%x0x1439ebdf,errnounchanged0xffffffff,ERANGE9c747bca9c%x0x747bca9c0xffffffff,ERANGE-eb371ade9%hx0x52170xffff,ERANGE867259ccd%hhX0xcd0xff,ERANGEPlain
sscanfon Windows already does this:ScanWindowsArgumentsscans narrow integers aslland truncates.ScanSourcenow does the same for%d,%i,%o,%u,%xand%Xwith no length,horhh: it scans into a 64-bit temporary and stores the destination's low bytes. Past 64 bits the host still saturates withERANGE, as the titles do (%dof99999999999999999999setsERANGEin both).Tested
Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release,
BUILD_TESTING=ON, onmain6793a72475.libc.prx: I loaded PPSA07923's and PPSA05916'slibc.prxnatively and called theirsscanf_s(24m4Z4bUaoY) side by side with this build. It covered 200,000 random numeric conversions:d i u o x X f e g a, every length, random widths, and inputs drawn from digits, hex letters, signs,.,x,e,p,inf/nanletters and spaces. I compared the result, the stored bytes,%nanderrno.main21 → 0, on both titles;0xwith no hex digit (78, where the titles fail the conversion);%f/hex%aerrno(37, where the titles leaveerrno);long doubleNaN's upper bytes.guest_formatting(three new rows),guest_stdio_variadic,guest_scan_count,guest_wide_stream_scanandguest_wide_formattingpass in each of 3 runs.main'sFormatting.cpp,guest_formattingfails.Not tested on Linux or macOS.
ScanSourceis the same code there, so Linuxsscanf_snow also scans these conversions as 64-bit and truncates. I haven't measured what glibc did with them before.Overlap:
tools/pr_overlap.pyfinds no shared lines: the 14 pull requests touching these files merge cleanly with this one. #5021, #7386, #10324 and #11332 conflict withmainand couldn't be compared.Independent Windows validation reported by bunbeans2-arch for topic
0b3eb0b3e53a8faa1f01fd01a6de4061247ab308merged into main409fe64199: Windows 11 Pro 22631, i9-9900K, MSYS2 UCRT64 GCC 16.1, Release.guest_formattingpasses 5/5; retaining this PR's tests while restoringFormatting.cppto main fails 5/5; plain main with its own source/tests passes 3/3. This is the contributor's run, not a new local Windows execution.Independent reports on this head (their runs, summarised here):
0b3eb0b3e5merged intomain409fe64199:guest_formattingpasses 5/5; withFormatting.cppfrommainit fails 5/5.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-28T16:41:48Z.
Review: APPROVED
Reviewed exact head
0b3eb0b3e53a8faa1f01fd01a6de4061247ab308. For unsuppressed integer conversions with no length,h, orhh, the change scans through a 64-bit temporary and copies only the destination-width low bytes, avoiding the host-width saturation observed for narrower destinations. The tests cover%x,%hx,%hhX, and preservation of the preexisting errno value; the PR also supplies paired title-libc comparisons across 200,000 conversions. Windows, conventions, policy, Ubuntu relinker/register-context and ASan checks pass. Linux is skipped and macOS jobs are pending; the PR description clearly records that host limitation and the remaining >64-bit overflow behavior.