fix(libc): ignore getcwd size when allocating a buffer #4927

Merged
QRMOE merged 3 commits from upstream-pr/4927 into main 2026-10-15 14:59:41 +00:00
QRMOE commented 2026-10-13 04:37:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-13T04:37:41Z.


What

Match FreeBSD's allocated-buffer extension to getcwd: when the buffer is null, ignore the supplied size and allocate enough space for the pathname. Current main rejects getcwd(nullptr, 1) with ERANGE in a non-root directory and passes a large ignored size to the allocator; SIZE_MAX reaches the heap overflow exception.

Apply the size check only to caller-provided buffers. Allocated results still use the application's allocator and must be freed through it. Caller-buffer bounds and allocation-failure handling keep their existing behavior.

FreeBSD 11.4 getcwd.c uses the size argument only in the non-null-buffer branch; its manual describes allocating space as necessary for a null buffer.

Tested

Windows, MinGW GCC 15.2, Release, BUILD_TESTING=ON, based on main e51428bf2:

  • The added working-directory regression fails on unchanged main with getcwd(NULL, size) must ignore size when allocating its result.
  • The regression covers sizes 1, 2, 1024 and SIZE_MAX in a non-root guest directory, verifies the returned pathname, and checks allocation/free routing. Existing caller-buffer zero-size and ERANGE checks remain.
  • Full default build and explicit libs build passed. All 21 selected library runtime tests passed, including working-directory, realpath, heap-overflow, formatting, strings, random state, time conversion, JSON and timestamp checks.
  • guest_filesystem separately fails at its existing open-file rename assertion (line 271, guest EACCES, Win32 error 5). Rebuilding with main's unchanged General.cpp reproduces the same failure. This PR does not change rename; the pending rename work in #4532 is a separate topic.
  • The final focused working-directory, realpath and heap-overflow tests passed. Conventions and whitespace checks passed.
  • No title playback was tested.

Review update (2026-10-15)

Corrected the existing replacement-heap assertion for getcwd(nullptr, 200) to require std::strlen(directory) + 1, preserving the check that the buffer uses the application allocator. Commit 7e34059a4 changes only that assertion.

Windows, MinGW GCC 15.2, Release: built the heap, heap-statistics and working-directory test targets with the PR merged onto main d92a8dad3. All nine tests selected by application_heap|guest_working_directory passed (7.97 seconds total). Restoring the old lastSize == 200 assertion reproduced application_heap_replacement failing; restoring the correction passed. Conventions and whitespace checks passed. Linux and a full project build were not rerun for this test-only update.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4838428511 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4927). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-13T04:37:41Z. --- ### What Match FreeBSD's allocated-buffer extension to `getcwd`: when the buffer is null, ignore the supplied size and allocate enough space for the pathname. Current main rejects `getcwd(nullptr, 1)` with `ERANGE` in a non-root directory and passes a large ignored size to the allocator; `SIZE_MAX` reaches the heap overflow exception. Apply the size check only to caller-provided buffers. Allocated results still use the application's allocator and must be freed through it. Caller-buffer bounds and allocation-failure handling keep their existing behavior. FreeBSD 11.4 [getcwd.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/gen/getcwd.c) uses the size argument only in the non-null-buffer branch; its [manual](https://man.freebsd.org/cgi/man.cgi?query=getcwd&sektion=3&manpath=FreeBSD+11.4-RELEASE) describes allocating space as necessary for a null buffer. ### Tested Windows, MinGW GCC 15.2, Release, `BUILD_TESTING=ON`, based on main `e51428bf2`: - The added working-directory regression fails on unchanged main with `getcwd(NULL, size) must ignore size when allocating its result`. - The regression covers sizes 1, 2, 1024 and `SIZE_MAX` in a non-root guest directory, verifies the returned pathname, and checks allocation/free routing. Existing caller-buffer zero-size and `ERANGE` checks remain. - Full default build and explicit libs build passed. All 21 selected library runtime tests passed, including working-directory, realpath, heap-overflow, formatting, strings, random state, time conversion, JSON and timestamp checks. - `guest_filesystem` separately fails at its existing open-file rename assertion (line 271, guest `EACCES`, Win32 error 5). Rebuilding with main's unchanged `General.cpp` reproduces the same failure. This PR does not change rename; the pending rename work in #4532 is a separate topic. - The final focused working-directory, realpath and heap-overflow tests passed. Conventions and whitespace checks passed. - No title playback was tested. ### Review update (2026-10-15) Corrected the existing replacement-heap assertion for `getcwd(nullptr, 200)` to require `std::strlen(directory) + 1`, preserving the check that the buffer uses the application allocator. Commit `7e34059a4` changes only that assertion. Windows, MinGW GCC 15.2, Release: built the heap, heap-statistics and working-directory test targets with the PR merged onto main `d92a8dad3`. All nine tests selected by `application_heap|guest_working_directory` passed (7.97 seconds total). Restoring the old `lastSize == 200` assertion reproduced `application_heap_replacement` failing; restoring the correction passed. Conventions and whitespace checks passed. Linux and a full project build were not rerun for this test-only update. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [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 - [x] AI-assisted: yes
xXPBSXx commented 2026-10-13 07:05:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T07:05:53Z.


Windows test of this PR.

Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, BUILD_TESTING=ON, -j2, libusb-cmake from a local checkout. Built from main ea2bbefb2 with this PR and six other open PRs (#4958, #4956, #4954, #4953, #4946, #4942, #4927) merged together; the merges had no conflict.

Result: guest_working_directory (1 of 1) pass.

Not checked: whether the test fails without the change in the library code; the PR was not tested alone, only in the combined merge (the tests of the other PRs in the batch also pass); no title run; Linux not tested.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140229594 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4927#issuecomment-6140229594). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T07:05:53Z. --- Windows test of this PR. Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `BUILD_TESTING=ON`, `-j2`, `libusb-cmake` from a local checkout. Built from `main` `ea2bbefb2` with this PR and six other open PRs (#4958, #4956, #4954, #4953, #4946, #4942, #4927) merged together; the merges had no conflict. Result: guest_working_directory (1 of 1) pass. Not checked: whether the test fails without the change in the library code; the PR was not tested alone, only in the combined merge (the tests of the other PRs in the batch also pass); no title run; Linux not tested. AI-assisted (Claude Code).
nicolasfalesy commented 2026-10-13 07:24:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-13T07:24:52Z.


Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto main 7e181fcc3 together with #4927, #4903, #4877, #4822 and #4823 (no conflicts): guest_working_directory passes. With this PR's source change reverted (test kept), guest_working_directory fails on Linux, so the check guards the change on Linux too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140512214 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4927#issuecomment-6140512214). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T07:24:52Z. --- Linux test (Arch, GCC 16.2.1, glibc, Release), this PR merged onto `main` 7e181fcc3 together with #4927, #4903, #4877, #4822 and #4823 (no conflicts): `guest_working_directory` passes. With this PR's source change reverted (test kept), `guest_working_directory` fails on Linux, so the check guards the change on Linux too.
oneandonlydean commented 2026-10-14 01:24:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T01:24:15Z.


With this on current main, application_heap_replacement aborts with application heap test failed (Linux, built locally); without it the test passes. getcwd(NULL, size) now allocates its buffer differently, so the replaced application heap no longer sees what the test expects. Please run ctest -R application_heap and fix the allocation before this goes in.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158782934 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4927#issuecomment-6158782934). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T01:24:15Z. --- With this on current `main`, `application_heap_replacement` aborts with `application heap test failed` (Linux, built locally); without it the test passes. `getcwd(NULL, size)` now allocates its buffer differently, so the replaced application heap no longer sees what the test expects. Please run `ctest -R application_heap` and fix the allocation before this goes in.
adriadam10 commented 2026-10-14 17:23:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T17:23:30Z.

Review: CHANGES_REQUESTED


Thanks, the change itself matches FreeBSD's null-buffer behaviour. One failure remains:

  • application_heap_replacement aborts ("application heap test failed") on this head, as @oneandonlydean reported. core/libs/tests/ApplicationHeap.cpp:303 still pins the old behaviour: getcwd_nid_postfix(nullptr, 200) is required to allocate lastSize == 200. With this PR the allocation is strlen(path) + 1. Update that check to the new size (e.g. lastSize == std::strlen(directory) + 1) and run ctest -R application_heap together with guest_working_directory before pushing.

I can't push the fix myself (maintainer edits are disabled on the branch), so please push it; everything else looked fine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5521894950 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4927#pullrequestreview-5521894950). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T17:23:30Z. **Review:** CHANGES_REQUESTED --- Thanks, the change itself matches FreeBSD's null-buffer behaviour. One failure remains: - `application_heap_replacement` aborts ("application heap test failed") on this head, as @oneandonlydean reported. `core/libs/tests/ApplicationHeap.cpp:303` still pins the old behaviour: `getcwd_nid_postfix(nullptr, 200)` is required to allocate `lastSize == 200`. With this PR the allocation is `strlen(path) + 1`. Update that check to the new size (e.g. `lastSize == std::strlen(directory) + 1`) and run `ctest -R application_heap` together with `guest_working_directory` before pushing. I can't push the fix myself (maintainer edits are disabled on the branch), so please push it; everything else looked fine.
QRMOE commented 2026-10-15 07:43:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QRMOE on 2026-10-15T07:43:25Z.


@adriadam10 @oneandonlydean Fixed in 7e34059a4: the existing application-heap check now expects std::strlen(directory) + 1 for getcwd(nullptr, 200), while still checking that the result comes from the replacement allocator.

Tested on Windows, MinGW GCC 15.2, Release, with this PR merged onto main d92a8dad3: the relevant test targets built, and ctest -R "application_heap|guest_working_directory" passed all 9 tests. Restoring the old lastSize == 200 assertion reproduced the failure; restoring the correction passed. Conventions and whitespace checks passed. Linux was not rerun locally.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6187112902 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4927#issuecomment-6187112902). Originally posted by [QRMOE](https://github.com/QRMOE) on 2026-10-15T07:43:25Z. --- @adriadam10 @oneandonlydean Fixed in 7e34059a4: the existing application-heap check now expects `std::strlen(directory) + 1` for `getcwd(nullptr, 200)`, while still checking that the result comes from the replacement allocator. Tested on Windows, MinGW GCC 15.2, Release, with this PR merged onto main `d92a8dad3`: the relevant test targets built, and `ctest -R "application_heap|guest_working_directory"` passed all 9 tests. Restoring the old `lastSize == 200` assertion reproduced the failure; restoring the correction passed. Conventions and whitespace checks passed. Linux was not rerun locally. AI-assisted: yes
adriadam10 commented 2026-10-15 11:04:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:04:28Z.

Review: APPROVED


Checked the new head: application_heap_replacement now expects strlen(directory) + 1 (ApplicationHeap.cpp:375), and application_heap_* plus guest_working_directory all pass here (9/9). Without the General.cpp change, guest_working_directory and application_heap_replacement fail, so both checks guard it. Matches FreeBSD's null-buffer behaviour; merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5532094452 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4927#pullrequestreview-5532094452). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T11:04:28Z. **Review:** APPROVED --- Checked the new head: `application_heap_replacement` now expects `strlen(directory) + 1` (`ApplicationHeap.cpp:375`), and `application_heap_*` plus `guest_working_directory` all pass here (9/9). Without the `General.cpp` change, `guest_working_directory` and `application_heap_replacement` fail, so both checks guard it. Matches FreeBSD's null-buffer behaviour; merging once the full Build is green.
adriadam10 commented 2026-10-15 11:47:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:47:10Z.


Checked the full Build on this head: windows is green. The only red, agc_pending_key_stores, is an AGC test unrelated to getcwd and it fails the same way on main's latest push build; not caused by this PR. Same verdict as before; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191305133 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4927#issuecomment-6191305133). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T11:47:10Z. --- Checked the full Build on this head: `windows` is green. The only red, `agc_pending_key_stores`, is an AGC test unrelated to `getcwd` and it fails the same way on main's latest push build; not caused by this PR. Same verdict as before; merging.
github-actions[bot] commented 2026-10-15 12:07:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:07:07Z.


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 3891 core/libs/prx/libc/src/General.cpp
PR 4156 core/libs/prx/libc/src/General.cpp
PR 4396 core/libs/prx/libc/src/General.cpp
PR 4397 core/libs/prx/libc/src/General.cpp
PR 4422 core/libs/prx/libc/src/General.cpp
PR 4423 core/libs/prx/libc/src/General.cpp
PR 4424 core/libs/prx/libc/src/General.cpp
PR 4520 core/libs/prx/libc/src/General.cpp
PR 4521 core/libs/prx/libc/src/General.cpp
PR 4522 core/libs/prx/libc/src/General.cpp
PR 4523 core/libs/prx/libc/src/General.cpp
PR 4566 core/libs/prx/libc/src/General.cpp
PR 4599 core/libs/prx/libc/src/General.cpp
PR 4718 core/libs/prx/libc/src/General.cpp
PR 4740 core/libs/prx/libc/src/General.cpp
PR 4749 core/libs/prx/libc/src/General.cpp
PR 4760 core/libs/prx/libc/src/General.cpp
PR 4813 core/libs/prx/libc/src/General.cpp
PR 5021 core/libs/prx/libc/src/General.cpp
PR 5835 core/libs/prx/libc/src/General.cpp
PR 6044 core/libs/tests/ApplicationHeap.cpp
core/libs/tests/GuestWorkingDirectory.cpp
PR 6222 core/libs/prx/libc/src/General.cpp
PR 6226 core/libs/prx/libc/src/General.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191658794 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4927#issuecomment-6191658794). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:07:07Z. --- <!-- 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 3891](https://www.github.com/boykopovar/AnyPS5/pull/3891) | `core/libs/prx/libc/src/General.cpp` | | [PR 4156](https://www.github.com/boykopovar/AnyPS5/pull/4156) | `core/libs/prx/libc/src/General.cpp` | | [PR 4396](https://www.github.com/boykopovar/AnyPS5/pull/4396) | `core/libs/prx/libc/src/General.cpp` | | [PR 4397](https://www.github.com/boykopovar/AnyPS5/pull/4397) | `core/libs/prx/libc/src/General.cpp` | | [PR 4422](https://www.github.com/boykopovar/AnyPS5/pull/4422) | `core/libs/prx/libc/src/General.cpp` | | [PR 4423](https://www.github.com/boykopovar/AnyPS5/pull/4423) | `core/libs/prx/libc/src/General.cpp` | | [PR 4424](https://www.github.com/boykopovar/AnyPS5/pull/4424) | `core/libs/prx/libc/src/General.cpp` | | [PR 4520](https://www.github.com/boykopovar/AnyPS5/pull/4520) | `core/libs/prx/libc/src/General.cpp` | | [PR 4521](https://www.github.com/boykopovar/AnyPS5/pull/4521) | `core/libs/prx/libc/src/General.cpp` | | [PR 4522](https://www.github.com/boykopovar/AnyPS5/pull/4522) | `core/libs/prx/libc/src/General.cpp` | | [PR 4523](https://www.github.com/boykopovar/AnyPS5/pull/4523) | `core/libs/prx/libc/src/General.cpp` | | [PR 4566](https://www.github.com/boykopovar/AnyPS5/pull/4566) | `core/libs/prx/libc/src/General.cpp` | | [PR 4599](https://www.github.com/boykopovar/AnyPS5/pull/4599) | `core/libs/prx/libc/src/General.cpp` | | [PR 4718](https://www.github.com/boykopovar/AnyPS5/pull/4718) | `core/libs/prx/libc/src/General.cpp` | | [PR 4740](https://www.github.com/boykopovar/AnyPS5/pull/4740) | `core/libs/prx/libc/src/General.cpp` | | [PR 4749](https://www.github.com/boykopovar/AnyPS5/pull/4749) | `core/libs/prx/libc/src/General.cpp` | | [PR 4760](https://www.github.com/boykopovar/AnyPS5/pull/4760) | `core/libs/prx/libc/src/General.cpp` | | [PR 4813](https://www.github.com/boykopovar/AnyPS5/pull/4813) | `core/libs/prx/libc/src/General.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/src/General.cpp` | | [PR 5835](https://www.github.com/boykopovar/AnyPS5/pull/5835) | `core/libs/prx/libc/src/General.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/ApplicationHeap.cpp`<br>`core/libs/tests/GuestWorkingDirectory.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libc/src/General.cpp` | | [PR 6226](https://www.github.com/boykopovar/AnyPS5/pull/6226) | `core/libs/prx/libc/src/General.cpp` |
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!4927
No description provided.