diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-04-09 15:40:18 -0400 |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-04-09 15:40:18 -0400 |
commit | 02ab7a84ef86cfe043bbfbbd2912dcc8c8c67793 (patch) | |
tree | 8e49d11fc2469920a5e0814d2ca90b251963c9f3 /Modules/posixmodule.c | |
parent | d5460096778dcb700f850c16c6ad63531430d433 (diff) | |
download | cpython-git-02ab7a84ef86cfe043bbfbbd2912dcc8c8c67793.tar.gz |
make sure fdopen always closes the fd in error cases (closes #21191)
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 21a6cef3f7..168f7f4917 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -6841,16 +6841,21 @@ posix_fdopen(PyObject *self, PyObject *args) /* Sanitize mode. See fileobject.c */ mode = PyMem_MALLOC(strlen(orgmode)+3); if (!mode) { + close(fd); PyErr_NoMemory(); return NULL; } strcpy(mode, orgmode); if (_PyFile_SanitizeMode(mode)) { + close(fd); PyMem_FREE(mode); return NULL; } - if (!_PyVerify_fd(fd)) - return posix_error(); + if (!_PyVerify_fd(fd)) { + posix_error(); + close(fd); + return NULL; + } Py_BEGIN_ALLOW_THREADS #if !defined(MS_WINDOWS) && defined(HAVE_FCNTL_H) if (mode[0] == 'a') { @@ -6871,8 +6876,11 @@ posix_fdopen(PyObject *self, PyObject *args) #endif Py_END_ALLOW_THREADS PyMem_FREE(mode); - if (fp == NULL) - return posix_error(); + if (fp == NULL) { + posix_error(); + close(fd); + return NULL; + } /* 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(fp, "<fdopen>", orgmode, fclose); |