]> granicus.if.org Git - python/commitdiff
Issue #17899: Fix rare file descriptor leak in os.listdir().
authorLarry Hastings <larry@hastings.org>
Fri, 2 Aug 2013 01:18:56 +0000 (18:18 -0700)
committerLarry Hastings <larry@hastings.org>
Fri, 2 Aug 2013 01:18:56 +0000 (18:18 -0700)
Misc/NEWS
Modules/posixmodule.c

index 2491ef17e5d769e733cff762fd150dd550cf3a0e..04bce96a89c6d0092a2655e96b6130902c63fd7d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 3.4.0 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #17899: Fix rare file descriptor leak in os.listdir().
+
 - Issue #10241: Clear extension module dict copies at interpreter shutdown.
   Patch by Neil Schemenauer, minimally modified.
 
index 06cf1df5d539f60a475e06afa6c5e2f9bf50a0e1..32fbadc447596640ae567816c1966ede64be30e8 100644 (file)
@@ -3420,12 +3420,13 @@ exit:
 static PyObject *
 _posix_listdir(path_t *path, PyObject *list)
 {
-    int fd = -1;
-
     PyObject *v;
     DIR *dirp = NULL;
     struct dirent *ep;
     int return_str; /* if false, return bytes */
+#ifdef HAVE_FDOPENDIR
+    int fd = -1;
+#endif
 
     errno = 0;
 #ifdef HAVE_FDOPENDIR
@@ -3467,6 +3468,13 @@ _posix_listdir(path_t *path, PyObject *list)
 
     if (dirp == NULL) {
         list = path_error(path);
+#ifdef HAVE_FDOPENDIR
+        if (fd != -1) {
+            Py_BEGIN_ALLOW_THREADS
+            close(fd);
+            Py_END_ALLOW_THREADS
+        }
+#endif
         goto exit;
     }
     if ((list = PyList_New(0)) == NULL) {
@@ -3509,8 +3517,10 @@ _posix_listdir(path_t *path, PyObject *list)
 exit:
     if (dirp != NULL) {
         Py_BEGIN_ALLOW_THREADS
+#ifdef HAVE_FDOPENDIR
         if (fd > -1)
             rewinddir(dirp);
+#endif
         closedir(dirp);
         Py_END_ALLOW_THREADS
     }