From bcb790fa7be7ed143f988946f576e82f3d70d877 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 18 Sep 2024 23:26:43 +0200 Subject: [PATCH] gh-120754: Fix memory leak in FileIO.__init__() 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 865b0e3634f3b4..8dae465fd20f8b 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();