diff options
| author | Larry Hastings <larry@hastings.org> | 2013-08-02 01:18:56 (GMT) |
|---|---|---|
| committer | Larry Hastings <larry@hastings.org> | 2013-08-02 01:18:56 (GMT) |
| commit | 4dbc95e258d8bd584e4621ac2effb56411f44b04 (patch) | |
| tree | a712bc892f1150c4f95e291cf0863203495d34c0 /Modules/posixmodule.c | |
| parent | ffff7631610c68691a214e5d463f8454b9c2af74 (diff) | |
| download | cpython-4dbc95e258d8bd584e4621ac2effb56411f44b04.zip cpython-4dbc95e258d8bd584e4621ac2effb56411f44b04.tar.gz cpython-4dbc95e258d8bd584e4621ac2effb56411f44b04.tar.bz2 | |
Issue #17899: Fix rare file descriptor leak in os.listdir().
Diffstat (limited to 'Modules/posixmodule.c')
| -rw-r--r-- | Modules/posixmodule.c | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 06cf1df..32fbadc 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 } |
