From 72c91892bc7da1dd17a7ec65baa942aeddf639ee Mon Sep 17 00:00:00 2001 From: Ayush Kapoor <71757760+mrlempticles@users.noreply.github.com> Date: Fri, 9 Oct 2026 08:05:18 +0530 Subject: [PATCH] fix(libc): prevent _Stoul 32-bit truncation on Windows On Windows (LLP64), unsigned long is 32-bit, causing _Stoul_nid_postfix to clamp values above UINT32_MAX to 4294967295 and set ERANGE. PS5 (LP64) uses a 64-bit unsigned long. Return unsigned long long and call std::strtoull to preserve 64-bit values across platforms, matching strtoul_nid_postfix and _Stoull. Add unsigned conversion coverage for _Stoul in guest_math_tests. --- core/libs/prx/libc/src/RuntimeSupport.cpp | 4 ++-- core/libs/tests/GuestMath.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libc/src/RuntimeSupport.cpp b/core/libs/prx/libc/src/RuntimeSupport.cpp index e6bf99f61067..ea80b9b26b1c 100644 --- a/core/libs/prx/libc/src/RuntimeSupport.cpp +++ b/core/libs/prx/libc/src/RuntimeSupport.cpp @@ -130,8 +130,8 @@ void APS5_VABI _Unlockfilelock_nid_postfix(FileStream* stream) { #endif } -unsigned long APS5_VABI _Stoul_nid_postfix(const char* str, char** endptr, int base) { - return std::strtoul(str, endptr, base); +unsigned long long APS5_VABI _Stoul_nid_postfix(const char* str, char** endptr, int base) { + return std::strtoull(str, endptr, base); } void APS5_VABI _Locksyslock_nid_postfix() { diff --git a/core/libs/tests/GuestMath.cpp b/core/libs/tests/GuestMath.cpp index ffdc06dffa1d..cfe3a514aa4e 100644 --- a/core/libs/tests/GuestMath.cpp +++ b/core/libs/tests/GuestMath.cpp @@ -14,6 +14,7 @@ float APS5_VABI strtof_nid_postfix(const char*, char**); long double APS5_VABI strtold_nid_postfix(const char*, char**); std::int64_t APS5_VABI strtol_nid_postfix(const char*, char**, int); std::uint64_t APS5_VABI strtoul_nid_postfix(const char*, char**, int); +unsigned long long APS5_VABI _Stoul_nid_postfix(const char*, char**, int); std::intmax_t APS5_VABI strtoimax_nid_postfix(const char*, char**, int); int* APS5_VABI __error_nid_postfix(); struct LibcFloatConstant { std::uint32_t bits[4]; }; @@ -132,11 +133,22 @@ static void CheckIntegerConversions() { std::abort(); } } + for (const auto& test : unsignedCases) { + char* end = nullptr; + *__error_nid_postfix() = 0; + const auto value = _Stoul_nid_postfix(test.text, &end, test.base); + if (value != test.value || end != test.text + test.consumed || *__error_nid_postfix() != test.error) { + std::fprintf(stderr, "Guest _Stoul failed for '%s' in base %d\n", test.text, test.base); + std::abort(); + } + } *__error_nid_postfix() = 13; Require(strtol_nid_postfix("-4294967296", nullptr, 10) == -INT64_C(4294967296)); Require(*__error_nid_postfix() == 13); Require(strtoul_nid_postfix("4294967296", nullptr, 10) == UINT64_C(4294967296)); Require(*__error_nid_postfix() == 13); + Require(_Stoul_nid_postfix("4294967296", nullptr, 10) == UINT64_C(4294967296)); + Require(*__error_nid_postfix() == 13); *__error_nid_postfix() = 0; }