https://github.com/python/cpython/commit/8060aa5d7dd02c22a987e23cf5dbb7049b50b042
commit: 8060aa5d7dd02c22a987e23cf5dbb7049b50b042
branch: main
author: Pieter Eendebak <[email protected]>
committer: vstinner <[email protected]>
date: 2026-03-09T14:17:27+01:00
summary:

gh-145376: Fix various refleaks in Objects/ (#145609)

files:
M Modules/_cursesmodule.c
M Modules/binascii.c
M Objects/genericaliasobject.c
M Objects/object.c
M Objects/structseq.c
M Objects/typevarobject.c
M Objects/unicodeobject.c

diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index 61464348d6fab8..dd96f9aa62b85b 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -1112,11 +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_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";
@@ -1130,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);
@@ -1210,6 +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_XDECREF(bytesobj);
             return NULL;
         }
     }
@@ -2212,6 +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_XDECREF(bytesobj);
             return NULL;
         }
     }
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;
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;
         }
     }
diff --git a/Objects/object.c b/Objects/object.c
index b537c0d104e58c..e405963614689f 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_Take2((PyDictObject *)dict, &_Py_ID(Py_Repr), 
list) < 0) {
             return -1;
-        Py_DECREF(list);
+        }
     }
     i = PyList_GET_SIZE(list);
     while (--i >= 0) {
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) {
diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c
index 2ec546aff52c0a..0a260f4c10278c 100644
--- a/Objects/typevarobject.c
+++ b/Objects/typevarobject.c
@@ -2305,13 +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");
         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);
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;
             }
         }
     }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to