Skip to content

Commit

Permalink
GH-114847: Raise FileNotFoundError when getcwd() returns '(unreachabl…
Browse files Browse the repository at this point in the history
…e)' (#117481)

On Linux >= 2.6.36 with glibc < 2.27, `getcwd()` can return a relative
pathname starting with '(unreachable)'. We detect this and fail with
ENOENT, matching new glibc behaviour.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
  • Loading branch information
barneygale and encukou committed Apr 3, 2024
1 parent 03f7aaf commit 345194d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`FileNotFoundError` when ``getcwd()`` returns '(unreachable)',
which can happen on Linux >= 2.6.36 with glibc < 2.27.
14 changes: 14 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4106,6 +4106,20 @@ posix_getcwd(int use_bytes)
else {
obj = PyUnicode_DecodeFSDefault(buf);
}
#ifdef __linux__
if (buf[0] != '/') {
/*
* On Linux >= 2.6.36 with glibc < 2.27, getcwd() can return a
* relative pathname starting with '(unreachable)'. We detect this
* and fail with ENOENT, matching newer glibc behaviour.
*/
errno = ENOENT;
path_object_error(obj);
PyMem_RawFree(buf);
return NULL;
}
#endif
assert(buf[0] == '/');
PyMem_RawFree(buf);

return obj;
Expand Down

0 comments on commit 345194d

Please sign in to comment.