From 6a77af690fc5022ecd218771960d15af2dc74977 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 11 Sep 2012 14:11:03 +0200 Subject: [PATCH] Issue #15895: Fix FILE pointer leak in PyRun_SimpleFileExFlags() when filename points to a pyc/pyo file and closeit is false. --- Misc/NEWS | 3 +++ Python/pythonrun.c | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS b/Misc/NEWS index 53d20f4a74e..068c4b1307e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.3.1 Core and Builtins ----------------- +- Issue #15895: Fix FILE pointer leak in PyRun_SimpleFileExFlags() when + filename points to a pyc/pyo file and closeit is false. + - Issue #15900: Fix reference leak in PyUnicode_TranslateCharmap(). - Issue #15839: Convert SystemErrors in super() to RuntimeErrors. diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 05dfb8e1d0a..7e9f6545e26 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1385,7 +1385,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, { PyObject *m, *d, *v; const char *ext; - int set_file_name = 0, ret; + int set_file_name = 0, close_own_fp = 0, ret; size_t len; m = PyImport_AddModule("__main__"); @@ -1419,6 +1419,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, ret = -1; goto done; } + close_own_fp = 1; /* Turn on optimization if a .pyo file is given */ if (strcmp(ext, ".pyo") == 0) Py_OptimizeFlag = 1; @@ -1449,6 +1450,9 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, Py_DECREF(v); ret = 0; done: + if (close_own_fp) { + fclose(fp); + } if (set_file_name && PyDict_DelItemString(d, "__file__")) PyErr_Clear(); return ret;