Files
AnyPS5/core/libs/tests/GuestAtoi.cpp
T
hypermistandClaude Opus 5.5 8e46144d48 fix(libc): follow the console for integer bases and 0x prefixes
- strtol, strtoul, strtoll, strtoull and strtouq return 0 for a base
  outside 0 and 2 to 36, leave the end at the start and set EINVAL;
  strtoimax, strtoumax, _Stoull, _Stoul and the wide forms do the same
  without errno.
- A 0x prefix with no hexadecimal digit after it (base 0 or 16) is no
  conversion: the end stays at the start of the string.
- wcstol, wcstoll and wcstoimax take one sign, convert the rest with
  wcstoull and clamp to the signed range with ERANGE, so "- 1" is -1
  and "+\t-1" overflows.
- atoi, atol and atoll truncate strtoull's result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
2026-10-21 16:05:00 +13:00

56 lines
1.8 KiB
C++

#include "prx/libc/include/general/VabiMacros.hpp"
#include <cstdint>
#include <cstdio>
extern "C" {
int APS5_VABI atoi_nid_postfix(const char*);
int* APS5_VABI __error_nid_postfix();
}
int main() {
struct Case {
const char* text;
int value;
int error;
};
constexpr int SavedError = 1234;
constexpr Case cases[] = {
{"0", 0, SavedError},
{"+42", 42, SavedError},
{" \t\n\r\f\v-0012tail", -12, SavedError},
{"010", 10, SavedError},
{"0x10", 0, SavedError},
{"0b10", 0, SavedError},
{"", 0, SavedError},
{"words", 0, SavedError},
{" +", 0, SavedError},
{"2147483647", INT32_MAX, SavedError},
{"-2147483648", INT32_MIN, SavedError},
{"2147483648", INT32_MIN, SavedError},
{"-2147483649", INT32_MAX, SavedError},
{"4294967295", -1, SavedError},
{"-4294967295", 1, SavedError},
{"4294967296", 0, SavedError},
{"-4294967296", 0, SavedError},
{" \t+4294967297tail", 1, SavedError},
{"9223372036854775807", -1, SavedError},
{"-9223372036854775808", 0, SavedError},
{"9223372036854775808", 0, SavedError},
{"-9223372036854775809", -1, SavedError},
{"999999999999999999999999999999999999", -1, 34},
{"-999999999999999999999999999999999999", -1, 34},
};
bool correct = true;
for (const auto& test : cases) {
*__error_nid_postfix() = SavedError;
const int value = atoi_nid_postfix(test.text);
const int error = *__error_nid_postfix();
if (value != test.value || error != test.error) {
std::fprintf(stderr, "atoi(%s): expected %d, errno %d; received %d, errno %d\n",
test.text, test.value, test.error, value, error);
correct = false;
}
}
return correct ? 0 : 1;
}