fix(libc): return ERANGE in getcwd when buffer size is 1 #15545
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15545"
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 MarrasFederico on 2026-11-03T18:34:56Z.
What
In
getcwd_nid_postfix(core/libs/prx/libc/src/General.cpp), whenbuffer != nullptrandsize == 1,getcwdcalledstd::filesystem::is_directory(state.current, error)before checking whether the 1-byte buffer could hold even the shortest valid path ("/", requiring 2 bytes including the NUL terminator). When the guest working directory was unlinked or removed from disk,is_directoryreturned false andgetcwdreturnednullptrwitherrno = ENOENT(2) instead ofERANGE(34).FreeBSD 11.4
lib/libc/gen/getcwd.clines 79-88 performs size validation immediately whenbuffer != nullptrbefore inspecting or retrieving the path from the filesystem:A check
if (buffer && size == 1) { errno = 34; return nullptr; }is added at function entry right after thesize == 0validation, ensuringERANGEis returned regardless of whether the working directory currently exists on disk.Tested
Ubuntu 26.04.1, kernel 7.0, GCC 15.2.0, Release,
BUILD_TESTING=ON, onmain1e658521a0. Windows and macOS were not compiled here. No title was run.guest_working_directory: change into a directory, remove it from disk, then callgetcwdwith a 1-byte buffer. It must returnNULLwithERANGE.main'sGeneral.cppthe test aborts, becausegetcwdreportsENOENT.ctest -R 'working_directory|libc_|public_headers': 78 of 78 pass.python3 tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.pycould not compare #5021, #9371, #13486 and #14235, which shareGeneral.cpp. Their diffs were checked by hand: none of them touchesgetcwd.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 repository (attach them to this PR)