fix(libc): ignore getcwd size when allocating a buffer #4927
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4927"
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 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 rejectsgetcwd(nullptr, 1)withERANGEin a non-root directory and passes a large ignored size to the allocator;SIZE_MAXreaches 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 maine51428bf2:getcwd(NULL, size) must ignore size when allocating its result.SIZE_MAXin a non-root guest directory, verifies the returned pathname, and checks allocation/free routing. Existing caller-buffer zero-size andERANGEchecks remain.guest_filesystemseparately fails at its existing open-file rename assertion (line 271, guestEACCES, Win32 error 5). Rebuilding with main's unchangedGeneral.cppreproduces the same failure. This PR does not change rename; the pending rename work in #4532 is a separate topic.Review update (2026-10-15)
Corrected the existing replacement-heap assertion for
getcwd(nullptr, 200)to requirestd::strlen(directory) + 1, preserving the check that the buffer uses the application allocator. Commit7e34059a4changes 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 byapplication_heap|guest_working_directorypassed (7.97 seconds total). Restoring the oldlastSize == 200assertion reproducedapplication_heap_replacementfailing; restoring the correction passed. Conventions and whitespace checks passed. Linux and a full project build were not rerun for this test-only update.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repositoryImported 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-cmakefrom a local checkout. Built frommainea2bbefb2with 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).
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
main7e181fcc3together with #4927, #4903, #4877, #4822 and #4823 (no conflicts):guest_working_directorypasses. With this PR's source change reverted (test kept),guest_working_directoryfails on Linux, so the check guards the change on Linux too.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T01:24:15Z.
With this on current
main,application_heap_replacementaborts withapplication 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 runctest -R application_heapand fix the allocation before this goes in.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_replacementaborts ("application heap test failed") on this head, as @oneandonlydean reported.core/libs/tests/ApplicationHeap.cpp:303still pins the old behaviour:getcwd_nid_postfix(nullptr, 200)is required to allocatelastSize == 200. With this PR the allocation isstrlen(path) + 1. Update that check to the new size (e.g.lastSize == std::strlen(directory) + 1) and runctest -R application_heaptogether withguest_working_directorybefore pushing.I can't push the fix myself (maintainer edits are disabled on the branch), so please push it; everything else looked fine.
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 expectsstd::strlen(directory) + 1forgetcwd(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, andctest -R "application_heap|guest_working_directory"passed all 9 tests. Restoring the oldlastSize == 200assertion reproduced the failure; restoring the correction passed. Conventions and whitespace checks passed. Linux was not rerun locally.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:04:28Z.
Review: APPROVED
Checked the new head:
application_heap_replacementnow expectsstrlen(directory) + 1(ApplicationHeap.cpp:375), andapplication_heap_*plusguest_working_directoryall pass here (9/9). Without theGeneral.cppchange,guest_working_directoryandapplication_heap_replacementfail, so both checks guard it. Matches FreeBSD's null-buffer behaviour; merging once the full Build is green.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:47:10Z.
Checked the full Build on this head:
windowsis green. The only red,agc_pending_key_stores, is an AGC test unrelated togetcwdand it fails the same way on main's latest push build; not caused by this PR. Same verdict as before; merging.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.
core/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cppcore/libs/tests/ApplicationHeap.cppcore/libs/tests/GuestWorkingDirectory.cppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/General.cpp