summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLarry Hastings <larry@hastings.org>2013-08-02 02:34:46 (GMT)
committerLarry Hastings <larry@hastings.org>2013-08-02 02:34:46 (GMT)
commit2e3e593e3490202757e25a82a8b3420418597cab (patch)
tree6047ae15b385cb48251b26b504210c70a72291d1
parent39668f57f445b8a2db63b0d0dc32c55a90d3f59e (diff)
downloadcpython-2e3e593e3490202757e25a82a8b3420418597cab.zip
cpython-2e3e593e3490202757e25a82a8b3420418597cab.tar.gz
cpython-2e3e593e3490202757e25a82a8b3420418597cab.tar.bz2
Issue #17899: Fix rare file descriptor leak in os.listdir().
(Done as separate patch from trunk as the code has diverged quite a bit.)
-rw-r--r--Misc/NEWS2
-rw-r--r--Modules/posixmodule.c11
2 files changed, 13 insertions, 0 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index 82b1081..825f6b3 100644
--- 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().
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 1ca12f3..56903c4 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -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
}