Skip to content

Commit dcbde6b

Browse files
authored
[3.14] gh-142776: Ensure fp file descriptor is closed on all code paths in import.c (GH-142777) (#142988)
gh-142776: Ensure fp file descriptor is closed on all code paths in import.c (GH-142777) (cherry picked from commit 6a4f103)
1 parent dbc7fd6 commit dcbde6b

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
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: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4687,6 +4687,7 @@ static PyObject *
46874687
_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
46884688
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
46894689
{
4690+
FILE *fp = NULL;
46904691
PyObject *mod = NULL;
46914692
PyThreadState *tstate = _PyThreadState_GET();
46924693

@@ -4729,16 +4730,12 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
47294730
/* We would move this (and the fclose() below) into
47304731
* _PyImport_GetModInitFunc(), but it isn't clear if the intervening
47314732
* code relies on fp still being open. */
4732-
FILE *fp;
47334733
if (file != NULL) {
47344734
fp = Py_fopen(info.filename, "r");
47354735
if (fp == NULL) {
47364736
goto finally;
47374737
}
47384738
}
4739-
else {
4740-
fp = NULL;
4741-
}
47424739

47434740
PyModInitFunction p0 = _PyImport_GetModInitFunc(&info, fp);
47444741
if (p0 == NULL) {
@@ -4762,12 +4759,10 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
47624759
}
47634760
#endif
47644761

4765-
// XXX Shouldn't this happen in the error cases too (i.e. in "finally")?
4766-
if (fp) {
4762+
finally:
4763+
if (fp != NULL) {
47674764
fclose(fp);
47684765
}
4769-
4770-
finally:
47714766
_Py_ext_module_loader_info_clear(&info);
47724767
return mod;
47734768
}

0 commit comments

Comments
 (0)