do not leak the FILE * pointer in error cases of fdopen()

This commit is contained in:
Benjamin Peterson 2016-12-03 13:03:18 -08:00
parent cadd22f2f5
commit d3035d5d3d
1 changed files with 8 additions and 6 deletions

View File

@ -6906,6 +6906,11 @@ posix_fdopen(PyObject *self, PyObject *args)
}
}
#endif
/* The dummy filename used here must be kept in sync with the value
tested against in gzip.GzipFile.__init__() - see issue #13781. */
f = PyFile_FromFile(NULL, "<fdopen>", orgmode, fclose);
if (f == NULL)
return NULL;
Py_BEGIN_ALLOW_THREADS
#if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H)
if (mode[0] == 'a') {
@ -6926,13 +6931,10 @@ posix_fdopen(PyObject *self, PyObject *args)
#endif
Py_END_ALLOW_THREADS
PyMem_FREE(mode);
if (fp == NULL)
if (fp == NULL) {
Py_DECREF(f);
return posix_error();
/* The dummy filename used here must be kept in sync with the value
tested against in gzip.GzipFile.__init__() - see issue #13781. */
f = PyFile_FromFile(NULL, "<fdopen>", orgmode, fclose);
if (f == NULL)
return NULL;
}
/* We now know we will succeed, so initialize the file object. */
((PyFileObject *)f)->f_fp = fp;
PyFile_SetBufSize(f, bufsize);