#1608818: errno can get set by every call to readdir().
authorGeorg Brandl <georg@python.org>
Wed, 16 Jul 2008 21:31:41 +0000 (21:31 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 16 Jul 2008 21:31:41 +0000 (21:31 +0000)
Misc/NEWS
Modules/posixmodule.c

index 90e42c780c9c0f92fb6da4c99d1a71380eb4f92e..357ea501c2ac799772c90f4d7e14746389735f36 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #1608818: Fix misbehavior in os.listdir() if readdir() fails.
+
 - Issue #3125: Remove copy_reg in multiprocessing and replace it with
   ForkingPickler.register() to resolve conflict with ctypes.
 
index d8c81b6a581717d4d70516f243be4966ceba8a1e..6af6e513d5e45c723be078fdda57024c03b5c063 100644 (file)
@@ -2322,11 +2322,19 @@ posix_listdir(PyObject *self, PyObject *args)
                return NULL;
        }
        for (;;) {
+               errno = 0;
                Py_BEGIN_ALLOW_THREADS
                ep = readdir(dirp);
                Py_END_ALLOW_THREADS
-               if (ep == NULL)
-                       break;
+               if (ep == NULL) {
+                       if (errno == 0) {
+                               break;
+                       } else {
+                               closedir(dirp);
+                               Py_DECREF(d);
+                               return posix_error_with_allocated_filename(name);
+                       }
+               }
                if (ep->d_name[0] == '.' &&
                    (NAMLEN(ep) == 1 ||
                     (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
@@ -2363,12 +2371,6 @@ posix_listdir(PyObject *self, PyObject *args)
                }
                Py_DECREF(v);
        }
-       if (errno != 0 && d != NULL) {
-               /* readdir() returned NULL and set errno */
-               closedir(dirp);
-               Py_DECREF(d);
-               return posix_error_with_allocated_filename(name); 
-       }
        closedir(dirp);
        PyMem_Free(name);