fix(libc): return ERANGE in getcwd when buffer size is 1 #15545

Merged
MarrasFederico merged 1 commit from upstream-pr/15545 into main 2026-11-07 09:01:54 +00:00
MarrasFederico commented 2026-11-03 18:34:56 +00:00 (Migrated from github.com)

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), when buffer != nullptr and size == 1, getcwd called std::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_directory returned false and getcwd returned nullptr with errno = ENOENT (2) instead of ERANGE (34).

FreeBSD 11.4 lib/libc/gen/getcwd.c lines 79-88 performs size validation immediately when buffer != nullptr before inspecting or retrieving the path from the filesystem:

	if (pt) {
		ptsize = 0;
		if (!size) {
			errno = EINVAL;
			return (NULL);
		}
		if (size == 1) {
			errno = ERANGE;
			return (NULL);
		}
		ept = pt + size;
	}

A check if (buffer && size == 1) { errno = 34; return nullptr; } is added at function entry right after the size == 0 validation, ensuring ERANGE is 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, on main 1e658521a0. Windows and macOS were not compiled here. No title was run.

  • New case in guest_working_directory: change into a directory, remove it from disk, then call getcwd with a 1-byte buffer. It must return NULL with ERANGE.
    • With main's General.cpp the test aborts, because getcwd reports ENOENT.
    • With this change it passes. After restoring the change, it passes again.
  • ctest -R 'working_directory|libc_|public_headers': 78 of 78 pass.
  • python3 tools/check_conventions.py --base origin/main passes.
  • tools/pr_overlap.py could not compare #5021, #9371, #13486 and #14235, which share General.cpp. Their diffs were checked by hand: none of them touches getcwd.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5142567062 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15545). Originally posted by [MarrasFederico](https://github.com/MarrasFederico) on 2026-11-03T18:34:56Z. --- ### What In `getcwd_nid_postfix` (`core/libs/prx/libc/src/General.cpp`), when `buffer != nullptr` and `size == 1`, `getcwd` called `std::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_directory` returned false and `getcwd` returned `nullptr` with `errno = ENOENT` (2) instead of `ERANGE` (34). FreeBSD 11.4 [`lib/libc/gen/getcwd.c` lines 79-88](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/gen/getcwd.c#L79-L88) performs size validation immediately when `buffer != nullptr` before inspecting or retrieving the path from the filesystem: ```c if (pt) { ptsize = 0; if (!size) { errno = EINVAL; return (NULL); } if (size == 1) { errno = ERANGE; return (NULL); } ept = pt + size; } ``` A check `if (buffer && size == 1) { errno = 34; return nullptr; }` is added at function entry right after the `size == 0` validation, ensuring `ERANGE` is returned regardless of whether the working directory currently exists on disk. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Ubuntu 26.04.1, kernel 7.0, GCC 15.2.0, Release, `BUILD_TESTING=ON`, on `main` 1e658521a0. Windows and macOS were not compiled here. No title was run. - New case in `guest_working_directory`: change into a directory, remove it from disk, then call `getcwd` with a 1-byte buffer. It must return `NULL` with `ERANGE`. - With `main`'s `General.cpp` the test aborts, because `getcwd` reports `ENOENT`. - With this change it passes. After restoring the change, it passes again. - `ctest -R 'working_directory|libc_|public_headers'`: 78 of 78 pass. - `python3 tools/check_conventions.py --base origin/main` passes. - `tools/pr_overlap.py` could not compare #5021, #9371, #13486 and #14235, which share `General.cpp`. Their diffs were checked by hand: none of them touches `getcwd`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
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!15545
No description provided.