Skip to content

Commit 6a4f103

Browse files
authored
gh-142776: Ensure fp file descriptor is closed on all code paths in import.c (GH-142777)
1 parent 786f464 commit 6a4f103

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a file descriptor leak in import.c

Python/import.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4762,6 +4762,7 @@ static PyObject *
47624762
_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
47634763
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
47644764
{
4765+
FILE *fp = NULL;
47654766
PyObject *mod = NULL;
47664767
PyThreadState *tstate = _PyThreadState_GET();
47674768

@@ -4804,16 +4805,12 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
48044805
/* We would move this (and the fclose() below) into
48054806
* _PyImport_GetModuleExportHooks(), but it isn't clear if the intervening
48064807
* code relies on fp still being open. */
4807-
FILE *fp;
48084808
if (file != NULL) {
48094809
fp = Py_fopen(info.filename, "r");
48104810
if (fp == NULL) {
48114811
goto finally;
48124812
}
48134813
}
4814-
else {
4815-
fp = NULL;
4816-
}
48174814

48184815
PyModInitFunction p0 = NULL;
48194816
PyModExportFunction ex0 = NULL;
@@ -4822,7 +4819,7 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
48224819
mod = import_run_modexport(tstate, ex0, &info, spec);
48234820
// Modules created from slots handle GIL enablement (Py_mod_gil slot)
48244821
// when they're created.
4825-
goto cleanup;
4822+
goto finally;
48264823
}
48274824
if (p0 == NULL) {
48284825
goto finally;
@@ -4845,13 +4842,10 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
48454842
}
48464843
#endif
48474844

4848-
cleanup:
4849-
// XXX Shouldn't this happen in the error cases too (i.e. in "finally")?
4850-
if (fp) {
4845+
finally:
4846+
if (fp != NULL) {
48514847
fclose(fp);
48524848
}
4853-
4854-
finally:
48554849
_Py_ext_module_loader_info_clear(&info);
48564850
return mod;
48574851
}

0 commit comments

Comments
 (0)