From 43cd7aa8cd88624f7211e47b98bc1e8e63e7660f Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 19 Sep 2024 00:11:50 +0200 Subject: [PATCH] gh-120754: Fix memory leak in FileIO.__init__() (#124225) Free 'self->stat_atopen' before assigning it, since io.FileIO.__init__() can be called multiple times manually (especially by test_io). --- Modules/_io/fileio.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 865b0e3634f3..8dae465fd20f 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -457,6 +457,7 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, #endif } + PyMem_Free(self->stat_atopen); self->stat_atopen = PyMem_New(struct _Py_stat_struct, 1); if (self->stat_atopen == NULL) { PyErr_NoMemory();