]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-114847: Raise FileNotFoundError when getcwd() returns '(unreachable)' (#117481)
authorBarney Gale <barney.gale@gmail.com>
Wed, 3 Apr 2024 15:39:40 +0000 (16:39 +0100)
committerGitHub <noreply@github.com>
Wed, 3 Apr 2024 15:39:40 +0000 (16:39 +0100)
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>
Misc/NEWS.d/next/Library/2024-04-02-20-30-12.gh-issue-114848.YX4pEc.rst [new file with mode: 0644]
Modules/posixmodule.c

diff --git a/Misc/NEWS.d/next/Library/2024-04-02-20-30-12.gh-issue-114848.YX4pEc.rst b/Misc/NEWS.d/next/Library/2024-04-02-20-30-12.gh-issue-114848.YX4pEc.rst
new file mode 100644 (file)
index 0000000..30b1a50
--- /dev/null
@@ -0,0 +1,2 @@
+Raise :exc:`FileNotFoundError` when ``getcwd()`` returns '(unreachable)',
+which can happen on Linux >= 2.6.36 with glibc < 2.27.
index a4b635ef5bf62905136728cc205a7536d67cb34d..fcac3dbe3553ef6162b77270d4b803ea3ddd457a 100644 (file)
@@ -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;