From 577de24b6edf902dda53fa55a92c6ae691e113ed Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Thu, 5 Mar 2026 16:02:48 +0100 Subject: [PATCH 01/10] Fix refleak in binascii.c --- Modules/binascii.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/binascii.c b/Modules/binascii.c index e6cd64338064b3..3f3695d50f2754 100644 --- a/Modules/binascii.c +++ b/Modules/binascii.c @@ -924,7 +924,7 @@ binascii_a2b_ascii85_impl(PyObject *module, Py_buffer *data, int foldspaces, } unsigned char *bin_data = PyBytesWriter_GetData(writer); if (bin_data == NULL) { - return NULL; + goto error; } uint32_t leftchar = 0; From e4fa631adcccd4de1411d4345fa8213602a64946 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Mar 2026 19:37:45 +0100 Subject: [PATCH 02/10] Fix refleak in _cursesmodule.c --- Modules/_cursesmodule.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 61464348d6fab8..2455dbc9d2e32e 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1112,6 +1112,7 @@ _curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1, attr_old = getattrs(self->win); if (curses_wattrset(self, attr, "addstr") < 0) { curses_release_wstr(strtype, wstr); + Py_DECREF(bytesobj); return NULL; } } @@ -1210,6 +1211,7 @@ _curses_window_addnstr_impl(PyCursesWindowObject *self, int group_left_1, attr_old = getattrs(self->win); if (curses_wattrset(self, attr, "addnstr") < 0) { curses_release_wstr(strtype, wstr); + Py_DECREF(bytesobj); return NULL; } } @@ -2212,6 +2214,7 @@ _curses_window_insstr_impl(PyCursesWindowObject *self, int group_left_1, attr_old = getattrs(self->win); if (curses_wattrset(self, attr, "insstr") < 0) { curses_release_wstr(strtype, wstr); + Py_DECREF(bytesobj); return NULL; } } From efb497b85ff8c532fd8e8bc87786205fee07ec73 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Mar 2026 19:53:05 +0100 Subject: [PATCH 03/10] Fix refleak in object.c --- Objects/object.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Objects/object.c b/Objects/object.c index b537c0d104e58c..aef8406d2148c3 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -1295,6 +1295,7 @@ _PyObject_SetAttributeErrorContext(PyObject* v, PyObject* name) // Augment the exception with the name and object if (PyObject_SetAttr(exc, &_Py_ID(name), name) || PyObject_SetAttr(exc, &_Py_ID(obj), v)) { + Py_DECREF(exc); return 1; } restore: @@ -3077,9 +3078,9 @@ Py_ReprEnter(PyObject *obj) list = PyList_New(0); if (list == NULL) return -1; - if (PyDict_SetItem(dict, &_Py_ID(Py_Repr), list) < 0) + if (_PyDict_SetItem_Take(dict, &_Py_ID(Py_Repr), list) < 0) { return -1; - Py_DECREF(list); + } } i = PyList_GET_SIZE(list); while (--i >= 0) { From c9d5cacb4761eb45be6aca0014a0726cc6b02d8b Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Mar 2026 19:53:27 +0100 Subject: [PATCH 04/10] Fix refleak in genericaliasobject.c --- Objects/genericaliasobject.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c index 119dd4b5c2dd00..7aef56cf4e93b8 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -299,6 +299,8 @@ subs_tvars(PyObject *obj, PyObject *params, &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg)); if (j < 0) { + Py_DECREF(subparams); + Py_DECREF(subargs); return NULL; } continue; @@ -455,6 +457,7 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje if (is_args_list) { args = tuple_args = PySequence_Tuple(args); if (args == NULL) { + Py_DECREF(item); return NULL; } } From 3bafd1a380944d684609e2d9a02facfd551d3b64 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Mar 2026 19:53:50 +0100 Subject: [PATCH 05/10] Fix refleak in typevarobject.c --- Objects/typevarobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 2ec546aff52c0a..91499acfca46cd 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -2310,6 +2310,7 @@ _Py_subscript_generic(PyThreadState* unused, PyObject *params) PyInterpreterState *interp = _PyInterpreterState_GET(); if (interp->cached_objects.generic_type == NULL) { PyErr_SetString(PyExc_SystemError, "Cannot find Generic type"); + Py_DECREF(params); return NULL; } PyObject *args[2] = {(PyObject *)interp->cached_objects.generic_type, params}; From f0e38ea2c66028925b58f436c2704438091c82d2 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Mar 2026 19:59:37 +0100 Subject: [PATCH 06/10] Fix decref on null in structseq.c --- Objects/structseq.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Objects/structseq.c b/Objects/structseq.c index 7a159338b9ba8a..b8bb041f0cff21 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -515,7 +515,8 @@ initialize_structseq_dict(PyStructSequence_Desc *desc, PyObject* dict, } if (_PyTuple_Resize(&keys, k) == -1) { - goto error; + assert(keys == NULL); + return -1; } if (PyDict_SetItemString(dict, match_args_key, keys) < 0) { From de71a747f7bf777ff98b3c4aacc456d014231cfc Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Mar 2026 19:59:57 +0100 Subject: [PATCH 07/10] Fix refleak in Objects/unicodeobject.c --- Objects/unicodeobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 7aa85a942e449e..7756f1a8482477 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -5220,7 +5220,7 @@ unicode_decode_utf8_impl(_PyUnicodeWriter *writer, } if (_PyUnicodeWriter_Prepare(writer, end - s, 127) < 0) { - return -1; + goto onError; } } } From b128a9de7c98a0391dfa2193be244c6607470fc9 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Mar 2026 20:50:40 +0100 Subject: [PATCH 08/10] missing commit --- Objects/object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/object.c b/Objects/object.c index aef8406d2148c3..e405963614689f 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -3078,7 +3078,7 @@ Py_ReprEnter(PyObject *obj) list = PyList_New(0); if (list == NULL) return -1; - if (_PyDict_SetItem_Take(dict, &_Py_ID(Py_Repr), list) < 0) { + if (_PyDict_SetItem_Take2((PyDictObject *)dict, &_Py_ID(Py_Repr), list) < 0) { return -1; } } From a33769787edbdc7fbcf8092d814f5ef0a043e0da Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Mar 2026 20:57:36 +0100 Subject: [PATCH 09/10] review comment --- Modules/_cursesmodule.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 2455dbc9d2e32e..dd96f9aa62b85b 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1112,12 +1112,13 @@ _curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1, attr_old = getattrs(self->win); if (curses_wattrset(self, attr, "addstr") < 0) { curses_release_wstr(strtype, wstr); - Py_DECREF(bytesobj); + Py_XDECREF(bytesobj); return NULL; } } #ifdef HAVE_NCURSESW if (strtype == 2) { + assert(bytesobj == NULL); if (use_xy) { rtn = mvwaddwstr(self->win,y,x,wstr); funcname = "mvwaddwstr"; @@ -1131,6 +1132,7 @@ _curses_window_addstr_impl(PyCursesWindowObject *self, int group_left_1, else #endif { + assert(wstr == NULL); const char *str = PyBytes_AS_STRING(bytesobj); if (use_xy) { rtn = mvwaddstr(self->win,y,x,str); @@ -1211,7 +1213,7 @@ _curses_window_addnstr_impl(PyCursesWindowObject *self, int group_left_1, attr_old = getattrs(self->win); if (curses_wattrset(self, attr, "addnstr") < 0) { curses_release_wstr(strtype, wstr); - Py_DECREF(bytesobj); + Py_XDECREF(bytesobj); return NULL; } } @@ -2214,7 +2216,7 @@ _curses_window_insstr_impl(PyCursesWindowObject *self, int group_left_1, attr_old = getattrs(self->win); if (curses_wattrset(self, attr, "insstr") < 0) { curses_release_wstr(strtype, wstr); - Py_DECREF(bytesobj); + Py_XDECREF(bytesobj); return NULL; } } From eadf7a18b13842bc4168f6bae05bd4450247d315 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Fri, 6 Mar 2026 21:24:55 +0100 Subject: [PATCH 10/10] review comment --- Objects/typevarobject.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 91499acfca46cd..0a260f4c10278c 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -2305,14 +2305,12 @@ generic_class_getitem(PyObject *cls, PyObject *args, PyObject *kwargs) PyObject * _Py_subscript_generic(PyThreadState* unused, PyObject *params) { - params = unpack_typevartuples(params); - PyInterpreterState *interp = _PyInterpreterState_GET(); if (interp->cached_objects.generic_type == NULL) { PyErr_SetString(PyExc_SystemError, "Cannot find Generic type"); - Py_DECREF(params); return NULL; } + params = unpack_typevartuples(params); PyObject *args[2] = {(PyObject *)interp->cached_objects.generic_type, params}; PyObject *result = call_typing_func_object("_GenericAlias", args, 2); Py_DECREF(params);