From bafe2be6fc146ad2f714094d0cdf5354cb679806 Mon Sep 17 00:00:00 2001 From: Shamil Date: Sat, 18 Oct 2025 12:27:58 +0300 Subject: [PATCH] gh-140272: Fix memory leak in _gdbm.gdbm.clear() (GH-140274) (cherry picked from commit f937468e7c88c768a28ff4e653da051ffa30d86c) Co-authored-by: Shamil --- .../2025-10-17-23-58-11.gh-issue-140272.lhY8uS.rst | 1 + Modules/_gdbmmodule.c | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-10-17-23-58-11.gh-issue-140272.lhY8uS.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-10-17-23-58-11.gh-issue-140272.lhY8uS.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-17-23-58-11.gh-issue-140272.lhY8uS.rst new file mode 100644 index 00000000000000..666a45055f5a58 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-10-17-23-58-11.gh-issue-140272.lhY8uS.rst @@ -0,0 +1 @@ +Fix memory leak in the :meth:`!clear` method of the :mod:`dbm.gnu` database. diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index 9c402e20e513b9..ce8696d9225e72 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -678,8 +678,10 @@ _gdbm_gdbm_clear_impl(gdbmobject *self, PyTypeObject *cls) } if (gdbm_delete(self->di_dbm, key) < 0) { PyErr_SetString(state->gdbm_error, "cannot delete item from database"); + free(key.dptr); return NULL; } + free(key.dptr); } Py_RETURN_NONE; }