]> 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 02:34:46 +0000 (19:34 -0700)
committerLarry Hastings <larry@hastings.org>
Fri, 2 Aug 2013 02:34:46 +0000 (19:34 -0700)
(Done as separate patch from trunk as the code has diverged quite a bit.)

Misc/NEWS
Modules/posixmodule.c

index 82b1081fda735bf16d16884286161eac60bad617..825f6b3a133fdd1ce5faf063a9ee0e6aaf5269d2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.3.3 release candidate 1?
 Core and Builtins
 -----------------
 
+- Issue #17899: Fix rare file descriptor leak in os.listdir().
+
 - Issue #18552: Check return value of PyArena_AddPyObject() in
   obj2ast_object().
 
index 1ca12f3c70e5fb589ae6db9233dabde22c13d1eb..56903c42f1092838ea964109db2a6c2b6dfc8b14 100644 (file)
@@ -3443,7 +3443,9 @@ posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
     path_t path;
     PyObject *list = NULL;
     static char *keywords[] = {"path", NULL};
+#ifdef HAVE_FDOPENDIR
     int fd = -1;
+#endif /* HAVE_FDOPENDIR */
 
 #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
     PyObject *v;
@@ -3732,6 +3734,13 @@ exit:
 
     if (dirp == NULL) {
         list = path_error("listdir", &path);
+#ifdef HAVE_FDOPENDIR
+        if (fd != -1) {
+            Py_BEGIN_ALLOW_THREADS
+            close(fd);
+            Py_END_ALLOW_THREADS
+        }
+#endif /* HAVE_FDOPENDIR */
         goto exit;
     }
     if ((list = PyList_New(0)) == NULL) {
@@ -3774,8 +3783,10 @@ exit:
 exit:
     if (dirp != NULL) {
         Py_BEGIN_ALLOW_THREADS
+#ifdef HAVE_FDOPENDIR
         if (fd > -1)
             rewinddir(dirp);
+#endif /* HAVE_FDOPENDIR */
         closedir(dirp);
         Py_END_ALLOW_THREADS
     }