[Python-checkins] cpython (2.7): do not leak the FILE * pointer in error cases of fdopen()

benjamin.peterson python-checkins at python.org
Sat Dec 3 16:05:03 EST 2016


https://hg.python.org/cpython/rev/50d4c8b80932
changeset:   105441:50d4c8b80932
branch:      2.7
user:        Benjamin Peterson <benjamin at python.org>
date:        Sat Dec 03 13:03:18 2016 -0800
summary:
  do not leak the FILE * pointer in error cases of fdopen()

files:
  Modules/posixmodule.c |  14 ++++++++------
  1 files changed, 8 insertions(+), 6 deletions(-)


diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -6906,6 +6906,11 @@
         }
     }
 #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 @@
 #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);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list