fix(libc): keep errno when strtol converts nothing on macOS #6853

Merged
DavidSamir merged 2 commits from upstream-pr/6853 into main 2026-10-19 12:17:55 +00:00
DavidSamir commented 2026-10-16 14:17:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T14:17:32Z.


What

On macOS, a guest integer conversion of a subject with no digits sets errno to EINVAL:

  • "", "words", " +", "\n";
  • in atoi, strtol and the rest of the family.

macOS's strtoll and strtoull set EINVAL there, and the guest functions pass the host's errno through. guest_atoi, guest_wide_conversion and guest_math expect errno unchanged in these cases, as glibc leaves it, so all three fail on macOS on main:

atoi(): expected 0, errno 1234; received 0, errno 22
_WStoul empty string: value 0, consumed 0, errno 22
Guest strtol failed for '' in base 10

A ParseInteger helper in SonyCrt.hpp calls the host conversion. On macOS it restores the caller's errno when nothing was converted and the base is 0 or 2 to 36:

  • an overflow still sets ERANGE;
  • an invalid base still sets EINVAL;
  • endptr and the value are unchanged;
  • on other hosts the helper calls the conversion as before.

Every host strtoll, strtoull, strtoimax and strtoumax call behind a guest export now goes through it:

  • strtol, strtoul, strtoll, strtoull;
  • atoi, atol, atoll;
  • _Stoul, _Stoull;
  • strtoimax, strtoumax;
  • the wcsto* integer conversions, and through them _WStoul.

Commits:

  1. The helper and the conversions in Strings.cpp.
  2. The ones in RuntimeSupport.cpp.

Tested

macOS 26.7.1, Apple M2, AppleClang 21.0.0, x86-64 under Rosetta, Release. main doesn't build the system libraries on macOS by itself, so the tests ran in a local tree: main ce991f1ec with the macOS runtime work merged (#4627, #4630, #6124, the macOS relinker PRs, mugurc's port branches). The branch's three files are identical to the ones tested.

  • guest_atoi and guest_wide_conversion fail on main's files and pass with this branch, 5 runs of 5.
  • guest_math gets past every strtol and strtoimax case. It then stops on an unrelated check: acosf(0.5f) returns 0x3f860a91 on macOS, one ulp from the 0x3f860a92 the test expects. A standalone program on this Mac gets the same bits from the host's acosf.
  • Wrong version: restoring errno after every conversion, which also drops ERANGE on overflow, fails all three tests:
    • atoi(9223372036854775808): expected -1, errno 34; received -1, errno 1234
    • _WStoul overflow: … errno 0
    • Guest strtol failed for '9223372036854775808!'
  • All 372 guest_ tests here, with main's files and with this branch: guest_atoi and guest_wide_conversion are the only differences. The other 20 failures are the same with both.
  • Titles: SwanStationPS5 (PPSA98510, catalog sha256 checked) converts "" and "\n" with strtol in base 10 43 times in its first 45 s, each time with EINVAL set on main. A temporary log line in the helper counted them. Over 40 s its own log, with numbers masked, and the list of files it writes under /data are the same with main's files and with this branch, so it shows no visible difference in that time. The other 15 catalog titles run here for 45 s didn't reach the case.
  • tools/check_conventions.py --base origin/main and git diff --check pass.

Not tested: Linux and Windows, where the helper calls the conversion unchanged.

Overlap: #6707 and #6133 edit SonyCrt.hpp; #6541, #6447 and #6133 edit RuntimeSupport.cpp; #6532 and #6144 edit Strings.cpp. None of them changes these conversions. Whichever lands second rebases.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none (PPSA98510 reaches it on macOS without a visible difference in 40 s)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4889673400 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6853). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-16T14:17:32Z. --- ### What On macOS, a guest integer conversion of a subject with no digits sets `errno` to `EINVAL`: - `""`, `"words"`, `" +"`, `"\n"`; - in `atoi`, `strtol` and the rest of the family. macOS's `strtoll` and `strtoull` set `EINVAL` there, and the guest functions pass the host's `errno` through. `guest_atoi`, `guest_wide_conversion` and `guest_math` expect `errno` unchanged in these cases, as glibc leaves it, so all three fail on macOS on `main`: ``` atoi(): expected 0, errno 1234; received 0, errno 22 _WStoul empty string: value 0, consumed 0, errno 22 Guest strtol failed for '' in base 10 ``` A `ParseInteger` helper in `SonyCrt.hpp` calls the host conversion. On macOS it restores the caller's `errno` when nothing was converted and the base is 0 or 2 to 36: - an overflow still sets `ERANGE`; - an invalid base still sets `EINVAL`; - `endptr` and the value are unchanged; - on other hosts the helper calls the conversion as before. Every host `strtoll`, `strtoull`, `strtoimax` and `strtoumax` call behind a guest export now goes through it: - `strtol`, `strtoul`, `strtoll`, `strtoull`; - `atoi`, `atol`, `atoll`; - `_Stoul`, `_Stoull`; - `strtoimax`, `strtoumax`; - the `wcsto*` integer conversions, and through them `_WStoul`. Commits: 1. The helper and the conversions in `Strings.cpp`. 2. The ones in `RuntimeSupport.cpp`. ### Tested macOS 26.7.1, Apple M2, AppleClang 21.0.0, x86-64 under Rosetta, Release. `main` doesn't build the system libraries on macOS by itself, so the tests ran in a local tree: `main` ce991f1ec with the macOS runtime work merged (#4627, #4630, #6124, the macOS relinker PRs, mugurc's port branches). The branch's three files are identical to the ones tested. - **`guest_atoi` and `guest_wide_conversion`** fail on `main`'s files and pass with this branch, 5 runs of 5. - **`guest_math`** gets past every `strtol` and `strtoimax` case. It then stops on an unrelated check: `acosf(0.5f)` returns `0x3f860a91` on macOS, one ulp from the `0x3f860a92` the test expects. A standalone program on this Mac gets the same bits from the host's `acosf`. - **Wrong version:** restoring `errno` after every conversion, which also drops `ERANGE` on overflow, fails all three tests: - `atoi(9223372036854775808): expected -1, errno 34; received -1, errno 1234` - `_WStoul overflow: … errno 0` - `Guest strtol failed for '9223372036854775808!'` - **All 372 `guest_` tests here,** with `main`'s files and with this branch: `guest_atoi` and `guest_wide_conversion` are the only differences. The other 20 failures are the same with both. - **Titles:** SwanStationPS5 (PPSA98510, catalog sha256 checked) converts `""` and `"\n"` with `strtol` in base 10 43 times in its first 45 s, each time with `EINVAL` set on `main`. A temporary log line in the helper counted them. Over 40 s its own log, with numbers masked, and the list of files it writes under `/data` are the same with `main`'s files and with this branch, so it shows no visible difference in that time. The other 15 catalog titles run here for 45 s didn't reach the case. - `tools/check_conventions.py --base origin/main` and `git diff --check` pass. Not tested: Linux and Windows, where the helper calls the conversion unchanged. Overlap: #6707 and #6133 edit `SonyCrt.hpp`; #6541, #6447 and #6133 edit `RuntimeSupport.cpp`; #6532 and #6144 edit `Strings.cpp`. None of them changes these conversions. Whichever lands second rebases. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none (PPSA98510 reaches it on macOS without a visible difference in 40 s) - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-16 14:40:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T14:40:08Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libc/src/RuntimeSupport.cpp
core/libs/prx/libc/src/Strings.cpp
PR 6879 core/libs/prx/libc/src/Strings.cpp
PR 6975 core/libs/prx/libc/src/RuntimeSupport.cpp
PR 7081 core/libs/prx/libc/include/SonyCrt.hpp
PR 7201 core/libs/prx/libc/src/RuntimeSupport.cpp
core/libs/prx/libc/src/Strings.cpp
PR 7332 core/libs/prx/libc/src/RuntimeSupport.cpp
core/libs/prx/libc/src/Strings.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6217908014 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6853#issuecomment-6217908014). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T14:40:08Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/src/RuntimeSupport.cpp`<br>`core/libs/prx/libc/src/Strings.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libc/src/Strings.cpp` | | [PR 6975](https://www.github.com/boykopovar/AnyPS5/pull/6975) | `core/libs/prx/libc/src/RuntimeSupport.cpp` | | [PR 7081](https://www.github.com/boykopovar/AnyPS5/pull/7081) | `core/libs/prx/libc/include/SonyCrt.hpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libc/src/RuntimeSupport.cpp`<br>`core/libs/prx/libc/src/Strings.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libc/src/RuntimeSupport.cpp`<br>`core/libs/prx/libc/src/Strings.cpp` |
karimibrahim07 commented 2026-10-17 03:52:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-17T03:52:26Z.

Review: COMMENTED


Windows check, since the description lists Windows as not tested. Head 8469126a6 merged onto main e3a8e4430 (clean merge). Windows 11 Pro (26300), WinLibs GCC 15.2.0 r7 (UCRT), Ninja Release.

  • guest_atoi, guest_wide_conversion, guest_math, guest_strings and guest_qsort pass, --repeat until-fail:3. On Windows ParseInteger takes the #else branch, so this confirms the change leaves Windows behaviour alone. These tests already expect errno unchanged for a subject with no digits, and they pass, so the UCRT doesn't set EINVAL there either.

Code: the macOS branch only restores errno when nothing was converted, the host reported EINVAL and the base is valid. So an invalid base still reports EINVAL, and ERANGE can't be hidden (it never comes with parsedEnd == text). Passing &parsedEnd and copying it to end afterwards also covers the atoi/atol callers, which pass a null end. I have no code requests.

Overlap with #6707 (mine): the bot reports a textual conflict at SonyCrt.hpp L23-25. #6707 removes the namespace { that ParseInteger is inserted after: it moves the constraint-handler slot out of the unnamed namespace so that every libc source file shares one handler (a review request there). If #6707 lands first, the resolution is to keep ParseInteger where it is, inside a namespace { } of its own or as a plain inline function. Nothing else in the two PRs touches the same code. I'll do the rebase on my side if yours lands first.

Not checked: macOS (no Mac here).

AI-assisted: yes. I ran the build and the tests above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5555933329 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6853#pullrequestreview-5555933329). Originally posted by [karimibrahim07](https://github.com/karimibrahim07) on 2026-10-17T03:52:26Z. **Review:** COMMENTED --- Windows check, since the description lists Windows as not tested. Head `8469126a6` merged onto `main` `e3a8e4430` (clean merge). Windows 11 Pro (26300), WinLibs GCC 15.2.0 r7 (UCRT), Ninja Release. - `guest_atoi`, `guest_wide_conversion`, `guest_math`, `guest_strings` and `guest_qsort` pass, `--repeat until-fail:3`. On Windows `ParseInteger` takes the `#else` branch, so this confirms the change leaves Windows behaviour alone. These tests already expect `errno` unchanged for a subject with no digits, and they pass, so the UCRT doesn't set `EINVAL` there either. **Code:** the macOS branch only restores `errno` when nothing was converted, the host reported `EINVAL` and the base is valid. So an invalid base still reports `EINVAL`, and `ERANGE` can't be hidden (it never comes with `parsedEnd == text`). Passing `&parsedEnd` and copying it to `end` afterwards also covers the `atoi`/`atol` callers, which pass a null `end`. I have no code requests. **Overlap with #6707 (mine):** the bot reports a textual conflict at `SonyCrt.hpp` L23-25. #6707 removes the `namespace {` that `ParseInteger` is inserted after: it moves the constraint-handler slot out of the unnamed namespace so that every libc source file shares one handler (a review request there). If #6707 lands first, the resolution is to keep `ParseInteger` where it is, inside a `namespace { }` of its own or as a plain `inline` function. Nothing else in the two PRs touches the same code. I'll do the rebase on my side if yours lands first. Not checked: macOS (no Mac here). AI-assisted: yes. I ran the build and the tests above.
simonbbby commented 2026-10-17 06:24:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-17T06:24:11Z.


Independent native arm64 macOS conversion check and Linux control on exact head 8469126a6da4a4b46ffa1ff2aeb4c27cda5a0470.

Scope first: this is an integer-export slice test, not a full libc/guest CTest run. I extracted the original integer conversion functions unchanged from Strings.cpp and RuntimeSupport.cpp, including the original prefix helpers, and compiled them against each revision's unchanged SonyCrt.hpp. Function-source hashes were recorded. Full native files cannot compile here because the unrelated long-double code requires x87 layout and FileStream.hpp hits macOS stdio macro errors. No production implementation was patched for this test.

Hosts: macOS 27.2 arm64, AppleClang 21.0.0 (executed natively); Ubuntu 24.04 x86-64, GCC 13.3/glibc.

The matrix calls all sixteen covered exports: eight narrow strto*/_Stoul/_Stoull forms, four wcsto* forms plus _WStoul, and atoi/atol/atoll. It checks exact values, errno and consumed positions with both null and non-null end pointers; 36 valid bases (0 and 2–36), five saved errno values, no-digit subjects, normal/prefixed conversions, signed/unsigned boundaries and overflow. Narrow exports also get invalid bases -1, 1 and 37.

Host / implementation Checks Mismatches
macOS, before both PR commits 29,234 18,872
macOS, this head 29,234 0
Linux, before both PR commits 29,234 0
Linux, this head 29,234 0

The Mac parent first fails with strtol/strtoll/strtoimax on no-digit input: errno 22 instead of the saved 0. A deliberately wrong helper that always restores errno fails 238 checks, including overflow returning the saturated value but clearing ERANGE. Thus this is sensitive to the overbroad-restoration error, as well as checking the no-digit correction and end-pointer handling.

No Rosetta/x86-64 guest ABI, complete wide-character suite, title, or current-main merge run is claimed. The Linux rows exercise the actual unchanged-host branch, rather than defining __APPLE__ on Linux.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6230651325 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6853#issuecomment-6230651325). Originally posted by [simonbbby](https://github.com/simonbbby) on 2026-10-17T06:24:11Z. --- Independent **native arm64 macOS** conversion check and Linux control on exact head `8469126a6da4a4b46ffa1ff2aeb4c27cda5a0470`. Scope first: this is an integer-export slice test, not a full libc/guest CTest run. I extracted the original integer conversion functions unchanged from `Strings.cpp` and `RuntimeSupport.cpp`, including the original prefix helpers, and compiled them against each revision's unchanged `SonyCrt.hpp`. Function-source hashes were recorded. Full native files cannot compile here because the unrelated long-double code requires x87 layout and `FileStream.hpp` hits macOS stdio macro errors. No production implementation was patched for this test. Hosts: macOS 27.2 arm64, AppleClang 21.0.0 (executed natively); Ubuntu 24.04 x86-64, GCC 13.3/glibc. The matrix calls all sixteen covered exports: eight narrow `strto*`/`_Stoul`/`_Stoull` forms, four `wcsto*` forms plus `_WStoul`, and `atoi`/`atol`/`atoll`. It checks exact values, errno and consumed positions with both null and non-null end pointers; 36 valid bases (0 and 2–36), five saved errno values, no-digit subjects, normal/prefixed conversions, signed/unsigned boundaries and overflow. Narrow exports also get invalid bases -1, 1 and 37. | Host / implementation | Checks | Mismatches | |---|---:|---:| | macOS, before both PR commits | 29,234 | 18,872 | | macOS, this head | 29,234 | **0** | | Linux, before both PR commits | 29,234 | **0** | | Linux, this head | 29,234 | **0** | The Mac parent first fails with `strtol`/`strtoll`/`strtoimax` on no-digit input: errno 22 instead of the saved 0. A deliberately wrong helper that always restores errno fails **238 checks**, including overflow returning the saturated value but clearing `ERANGE`. Thus this is sensitive to the overbroad-restoration error, as well as checking the no-digit correction and end-pointer handling. No Rosetta/x86-64 guest ABI, complete wide-character suite, title, or current-main merge run is claimed. The Linux rows exercise the actual unchanged-host branch, rather than defining `__APPLE__` on Linux.
adriadam10 commented 2026-10-19 12:20:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T12:20:24Z.


Checked the diff: every host strtoll/strtoull/strtoimax/strtoumax call behind a guest export goes through ParseInteger. On macOS it only restores errno when nothing was converted and the base is valid, so ERANGE and the invalid-base EINVAL are kept; other hosts call the conversion unchanged. Windows confirmed by @karimibrahim07, macOS arm64 by @simonbbby.
Rebased onto main (the helper now sits inside the anonymous namespace that main moved in SonyCrt.hpp); built and ran ctest on the rebase; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6267060956 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6853#issuecomment-6267060956). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T12:20:24Z. --- Checked the diff: every host `strtoll`/`strtoull`/`strtoimax`/`strtoumax` call behind a guest export goes through `ParseInteger`. On macOS it only restores `errno` when nothing was converted and the base is valid, so `ERANGE` and the invalid-base `EINVAL` are kept; other hosts call the conversion unchanged. Windows confirmed by @karimibrahim07, macOS arm64 by @simonbbby. Rebased onto `main` (the helper now sits inside the anonymous namespace that `main` moved in `SonyCrt.hpp`); built and ran ctest on the rebase; merged.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!6853
No description provided.