Skip to content

Commit 4b8c9f5

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

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
@@ -4669,6 +4669,7 @@ static PyObject *
46694669
_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
46704670
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
46714671
{
4672+
FILE *fp = NULL;
46724673
PyObject *mod = NULL;
46734674
PyThreadState *tstate = _PyThreadState_GET();
46744675

@@ -4711,16 +4712,12 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
47114712
/* We would move this (and the fclose() below) into
47124713
* _PyImport_GetModInitFunc(), but it isn't clear if the intervening
47134714
* code relies on fp still being open. */
4714-
FILE *fp;
47154715
if (file != NULL) {
47164716
fp = _Py_fopen_obj(info.filename, "r");
47174717
if (fp == NULL) {
47184718
goto finally;
47194719
}
47204720
}
4721-
else {
4722-
fp = NULL;
4723-
}
47244721

47254722
PyModInitFunction p0 = _PyImport_GetModInitFunc(&info, fp);
47264723
if (p0 == NULL) {
@@ -4744,12 +4741,10 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
47444741
}
47454742
#endif
47464743

4747-
// XXX Shouldn't this happen in the error cases too (i.e. in "finally")?
4748-
if (fp) {
4744+
finally:
4745+
if (fp != NULL) {
47494746
fclose(fp);
47504747
}
4751-
4752-
finally:
47534748
_Py_ext_module_loader_info_clear(&info);
47544749
return mod;
47554750
}

0 commit comments

Comments
 (0)