]> granicus.if.org Git - python/commitdiff
Issue #9185: On Solaris and OpenBSD, posix_getcwd() could loop indefinitely
authorStefan Krah <stefan@bytereef.org>
Tue, 13 Jul 2010 19:17:08 +0000 (19:17 +0000)
committerStefan Krah <stefan@bytereef.org>
Tue, 13 Jul 2010 19:17:08 +0000 (19:17 +0000)
if the path length exceeded PATH_MAX.

Lib/test/test_posix.py
Modules/posixmodule.c

index afeb616ddca7217ec5d54e95121712d87ad3a6d3..d2c768afca4b04bb0c2bccc156a14c54f45d4bf8 100644 (file)
@@ -10,6 +10,7 @@ import time
 import os
 import pwd
 import shutil
+import sys
 import unittest
 import warnings
 
@@ -345,8 +346,13 @@ class PosixTester(unittest.TestCase):
                     os.chdir(dirname)
                     try:
                         os.getcwd()
-                        if current_path_length < 1027:
+                        if current_path_length < 4099:
                             _create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
+                    except OSError as e:
+                        expected_errno = errno.ENAMETOOLONG
+                        if 'sunos' in sys.platform or 'openbsd' in sys.platform:
+                            expected_errno = errno.ERANGE # Issue 9185
+                        self.assertEqual(e.errno, expected_errno)
                     finally:
                         os.chdir('..')
                         os.rmdir(dirname)
index 8a23e658f74fbb6ba5117471226ac1425da8675e..17af5086613d9c37e7641beb58adc666c3cf71ae 100644 (file)
@@ -1957,6 +1957,28 @@ PyDoc_STRVAR(posix_getcwd__doc__,
 "getcwd() -> path\n\n\
 Return a string representing the current working directory.");
 
+#if (defined(__sun) && defined(__SVR4)) || defined(__OpenBSD__)
+/* Issue 9185: getcwd() returns NULL/ERANGE indefinitely. */
+static PyObject *
+posix_getcwd(PyObject *self, PyObject *noargs)
+{
+    char buf[PATH_MAX+2];
+    char *res;
+
+    Py_BEGIN_ALLOW_THREADS
+#if defined(PYOS_OS2) && defined(PYCC_GCC)
+        res = _getcwd2(buf, sizeof buf);
+#else
+        res = getcwd(buf, sizeof buf);
+#endif
+    Py_END_ALLOW_THREADS
+
+    if (res == NULL)
+        return posix_error();
+
+    return PyString_FromString(buf);
+}
+#else
 static PyObject *
 posix_getcwd(PyObject *self, PyObject *noargs)
 {
@@ -1993,6 +2015,7 @@ posix_getcwd(PyObject *self, PyObject *noargs)
 
     return dynamic_return;
 }
+#endif /* getcwd() NULL/ERANGE workaround. */
 
 #ifdef Py_USING_UNICODE
 PyDoc_STRVAR(posix_getcwdu__doc__,