comparison Python/pythonrun.c @ 78990:4754c4a710e6

Issue #15895: Fix FILE pointer leak in PyRun_SimpleFileExFlags() when filename points to a pyc/pyo file and closeit is false.
author Christian Heimes <christian@cheimes.de>
date Tue, 11 Sep 2012 14:11:03 +0200
parents 92c6bbf7bb85
children c3539eb02470
comparison
equal deleted inserted replaced
78989:f1a31d9d36e6 78990:4754c4a710e6
1383 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, 1383 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
1384 PyCompilerFlags *flags) 1384 PyCompilerFlags *flags)
1385 { 1385 {
1386 PyObject *m, *d, *v; 1386 PyObject *m, *d, *v;
1387 const char *ext; 1387 const char *ext;
1388 int set_file_name = 0, ret; 1388 int set_file_name = 0, close_own_fp = 0, ret;
1389 size_t len; 1389 size_t len;
1390 1390
1391 m = PyImport_AddModule("__main__"); 1391 m = PyImport_AddModule("__main__");
1392 if (m == NULL) 1392 if (m == NULL)
1393 return -1; 1393 return -1;
1417 if ((fp = fopen(filename, "rb")) == NULL) { 1417 if ((fp = fopen(filename, "rb")) == NULL) {
1418 fprintf(stderr, "python: Can't reopen .pyc file\n"); 1418 fprintf(stderr, "python: Can't reopen .pyc file\n");
1419 ret = -1; 1419 ret = -1;
1420 goto done; 1420 goto done;
1421 } 1421 }
1422 close_own_fp = 1;
1422 /* Turn on optimization if a .pyo file is given */ 1423 /* Turn on optimization if a .pyo file is given */
1423 if (strcmp(ext, ".pyo") == 0) 1424 if (strcmp(ext, ".pyo") == 0)
1424 Py_OptimizeFlag = 1; 1425 Py_OptimizeFlag = 1;
1425 1426
1426 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) { 1427 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
1447 goto done; 1448 goto done;
1448 } 1449 }
1449 Py_DECREF(v); 1450 Py_DECREF(v);
1450 ret = 0; 1451 ret = 0;
1451 done: 1452 done:
1453 if (close_own_fp) {
1454 fclose(fp);
1455 }
1452 if (set_file_name && PyDict_DelItemString(d, "__file__")) 1456 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1453 PyErr_Clear(); 1457 PyErr_Clear();
1454 return ret; 1458 return ret;
1455 } 1459 }
1456 1460