summaryrefslogtreecommitdiff
path: root/Modules
diff options
context:
space:
mode:
authorLarry Hastings <larry@hastings.org>2013-08-01 18:18:56 -0700
committerLarry Hastings <larry@hastings.org>2013-08-01 18:18:56 -0700
commit5f4ed2aba162971332a76036fd45df3a3c601193 (patch)
treefc3ee49f38729b5c99653a7bc5a8e00c3315a615 /Modules
parent584333959b1921f52f684f5f48632613c3a508e6 (diff)
downloadcpython-5f4ed2aba162971332a76036fd45df3a3c601193.tar.gz
Issue #17899: Fix rare file descriptor leak in os.listdir().
Diffstat (limited to 'Modules')
-rw-r--r--Modules/posixmodule.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 06cf1df5d5..32fbadc447 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -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
}