mirror of
https://codeberg.org/ziglang/zig
synced 2026-11-18 07:05:05 +00:00
The assumption currentPathAlloc was making (that the path can never exceed max_path_bytes) was incorrect.
What exactly that means depends on (1) the system, (2) whether or not libc is linked, and (3) (if libc is linked) the libc implementation of `getcwd`.
The POSIX spec says that ERANGE is returned when the buffer is smaller than necessary.
https://pubs.opengroup.org/onlinepubs/9799919799/
However, on Linux, the getcwd *syscall* is limited by PATH_MAX, so the getcwd syscall will return ENAMETOOLONG if the syscall is unable to provide the full cwd path.
e4b2755318/fs/d_path.c (L406-L407)
That doesn't mean the full cwd path can't be gotten, but it means it would need to be reconstructed piece-by-piece by walking the filesystem.
Many libc implementations will do that filesystem walking automatically (musl doesn't; gnu libc, freebsd, etc do), so the libc being linked also determines whether or not ENAMETOOLONG is possible.
FreeBSD: https://cgit.freebsd.org/src/tree/lib/libc/gen/getcwd.c
GNU libc: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/posix/getcwd.c;h=91c6ffd4d8208a0a8efb9c3fcb9dca0ca1007f33;hb=HEAD
Instead of adding recovery to these APIs (e.g. expanding the buffer and retrying, or walking the filesystem to construct the path independently), `currentPathAlloc` is now explicitly a convenience function, and the error sets of both `currentPath` and `currentPathAlloc` have been expanded to give the caller the ability to disambiguate between the possible ways they may be able to recover and let them handle recovery if deeply nested cwd paths are something they want to support.
Closes #36928
1126 lines
42 KiB
1126 lines
42 KiB