mirror of
https://github.com/python/cpython.git
synced 2025-12-15 21:44:50 +00:00
Issue #17899: Fix rare file descriptor leak in os.listdir().
(Done as separate patch from trunk as the code has diverged quite a bit.)
This commit is contained in:
parent
39668f57f4
commit
2e3e593e34
2 changed files with 13 additions and 0 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue