fix(libc): stop the strtol family at a binary prefix on Linux #1459
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1459"
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 nauroth on 2026-10-08T07:54:56Z.
What
On Linux, the narrow integer converters
strtol,strtoul,strtoll,strtoull,_Stoull,strtoimax,strtoumaxand_Stoulpass the guest's text and base straight to glibc. The wide converterswcstol,wcstoll,wcstoulandwcstoullpass the ASCII prefix of the guest's text. Since glibc 2.38, code built as GNU C++ calls the C23__isoc23_*functions (g++ defines_GNU_SOURCE). Those accept a0b/0Bbinary prefix in base 0 and 2. C17 has no binary prefix (7.22.1.4p3): the subject sequence of"0b101"is"0", so the result is 0 and the end pointer stops at theb.Results on Linux with the code on main:
strtol("0b101", &end, 0)strtol("0b101", &end, 2)strtol(" -0B1", &end, 0)strtoul("0b11", &end, 2)strtoimax("0b101", &end, 0)strtoumax("0b101", &end, 0)_Stoul("0b101", &end, 0)wcstol(u"0b101", &end, 0)Separately, on Windows,
_Stoulwas declared with the host'sunsigned longand returnedstd::strtoul, both 32-bit in the LLP64 build, while the guest'sunsigned longis 64-bit (#2424)."4294967296"clamped to 4294967295 withERANGEand"-1"gave0xffffffff, so a gueststd::stoulthrewout_of_rangeon values the console accepts.Changes, one commit each:
StopAtBinaryPrefix_nid_no_patch(Strings.cpp, declared inGeneral.hpp) handles text in base 0 or 2. After C-locale white space and an optional sign, if the text starts with0bor0Band a binary digit, it returns 0 and sets the end pointer after the0. The eight narrow exports check it first. Every other input still goes to the host function, so errno, overflow and the0/0xprefixes are unchanged.guest_mathgets rows for base 0 and base 2, a sign with leading white space,0b2(where glibc already stops after the0) and0b101in base 16 (still the hex value0xb101), and checksstrtoll,strtoull,strtoumax,_Stoulland_Stoulon" -0B11".ParseAsciiPrefixmaps the end pointer back to the guest's UTF-16 text as before.guest_wide_stringschecks them onu" -0b101"in base 0 and base 2, andu"0b101"in base 16._Stoul_nid_postfixreturnsstd::uint64_tfromstd::strtoull, asstrtoul_nid_postfixand_Stoullalready do, with the prefix check still in front.guest_mathchecks"4294967296","18446744073709551615"and"-1". On Linux this changes nothing.%ireaches glibc's C23 conversion the same way:sscanf("0b101", "%i", &value)stores 5. Because the format is handed to glibc, this is recorded in TechnicalDebt rather than changed here.The Windows behaviour of the prefix check is unchanged: the UCRT functions accept only the
0and0xprefixes, so the new check returns what they already return.Thanks to @Zheorgyan for catching the wide functions, @AchrafReyani for #2424, @SurajKhanna for #2442, and @MarcZX100 for suggesting the fold.
Tested
-DBUILD_TESTING=ON, this branch ond724cad(current main).guest_mathandguest_wide_stringspass;guest_mathfails on main with "Guest strtol failed for '0b101' in base 0", andguest_wide_stringsfails on the first commit, before the wide change. The table above comes from main's function bodies, built with the same compiler and glibc._Stoulchange is the one #2442 tested on Windows 11, GCC 16.2.0 (UCRT64), whereguest_mathaborted on the first new case before the change. The CI Windows run on the previous push (419/419) predates that commit.python3 tools/check_conventions.py --base upstream/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by Zheorgyan on 2026-10-08T09:26:10Z.
the wide versions have the same problem and aren't covered here.
wcstol,wcstoll,wcstoulandwcstoullinStrings.cppalso go straight to glibc, and its C23wcstoll/wcstoulltake the prefix too. with this branch merged onto current main,wcstol_nid_postfix(L"0b101", &end, 0)returns 5 with end +5, andwcstoul_nid_postfixin base 2 does the same. maybe worth a wide twin ofStopAtBinaryPrefix_nid_no_patch, or a follow-up if you'd rather keep this one narrowthe rest checks out on Linux, Ubuntu 24.04 in Docker with glibc 2.39 and GCC 14.2.
guest_mathpasses with the change and fails on main with "Guest strtol failed for '0b101' in base 0"Imported from boykopovar/AnyPS5. Originally posted by nauroth on 2026-10-08T11:06:53Z.
Thank you @Zheorgyan!
The wide versions now go through the same check:
wcstol,wcstoll,wcstoulandwcstoullcallStopAtBinaryPrefix_nid_no_patchon the ASCII prefix before glibc, andParseAsciiPrefixmaps the end pointer back like before (second commit).guest_wide_stringsnow coversu" -0b101"in base 0 and 2 for all four, plus a base 16 case to make sure0b101still parses as hex there. It fails on the previous commit and passes with this one.I also rebased onto current main to clear the TechnicalDebt conflict with #1278.
Imported from boykopovar/AnyPS5. Originally posted by nauroth on 2026-10-09T11:33:01Z.
Rebased onto current main (
d70b899); the branch had drifted 90 commits and conflicted again inTechnicalDebt.md. No code changes: still the same two commits and 55 lines. On the new baseguest_mathandguest_wide_stringspass on Linux (Ubuntu 24.04, GCC 13.3.0) andtools/check_conventions.pypasses.The earlier red Linux run was
agc_driver_flat_storetiming out on its cold first run, which #1715 has since serialized on main. Since #2113 the full builds only run when a collaborator dispatches Build with the PR number, and they build the PR merged into main, so the rebase was needed before that can run. Could a collaborator trigger it when convenient?Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T20:57:19Z.
One related issue surfaced while reviewing the Windows
_Stoulreport: #2424 found that_Stoul_nid_postfixusesunsigned long/strtoul, which are only 32-bit in the Windows LLP64 build, while the guest ABI expects the PS5’s 64-bit result. The proposed fix in #2442 (std::uint64_t+std::strtoull, with twoguest_mathcases) was auto-closed by the intake pause, not rejected on code quality.This PR also changes
_Stoul_nid_postfixinRuntimeSupport.cppto enforce C17 binary-prefix parsing on Linux. The two changes appear compatible: the Linux prefix guard can stay as-is, while the Windows fallback delegates to the 64-bit parser. Would you prefer to include the small Windows-width correction in this branch, or keep this PR narrowly scoped and record that #2424 needs a follow-up when intake resumes? I have not prepared or pushed a duplicate patch.Imported from boykopovar/AnyPS5. Originally posted by nauroth on 2026-10-09T21:39:22Z.
Thanks @MarcZX100 for connecting the dots, and thanks @AchrafReyani for the report in #2424 and @SurajKhanna for the patch in #2442. Folded in as a third commit:
_Stoul_nid_postfixnow returnsstd::uint64_tfromstd::strtoull, with the binary-prefix check still in front of the host call, exactly as you described. The twoguest_mathcases from #2442 are taken as they were plus a"-1"→UINT64_MAXcase. On Linux nothing changes, sinceunsigned longis already 64-bit there. Also rebased onto current main (d724cad) so the manual build can run;guest_math,guest_wide_stringsand the conventions check pass on Linux.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T23:31:13Z.
Checked on Linux (glibc 2.39, GCC):
guest_mathandguest_wide_stringspass and both fail without the change; the0b/0Bstop matches C17 7.22.1.4 (subject"0", end at theb), base 16 and the0/0xprefixes are untouched, and the_Stoul64-bit return matchesstrtoul_nid_postfix/_Stoull. TechnicalDebt line is in the right section. The Windows_Stoulbehaviour is not run here. Merging.