https://github.com/python/cpython/commit/afde75664eb3ff3e147806f027c9da54c7eb77d4
commit: afde75664eb3ff3e147806f027c9da54c7eb77d4
branch: main
author: Gleb Popov <[email protected]>
committer: encukou <[email protected]>
date: 2026-04-17T14:13:44+02:00
summary:

gh-148484: Fix memory leak of iterator in array.array constructor (GH-148523)

files:
M Modules/arraymodule.c

diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index a86a7561271b87..b01e92eb8873ba 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -3053,8 +3053,10 @@ array_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
                 len = 0;
 
             a = newarrayobject(type, len, descr);
-            if (a == NULL)
+            if (a == NULL) {
+                Py_XDECREF(it);
                 return NULL;
+            }
 
             if (len > 0 && !array_Check(initial, state)) {
                 Py_ssize_t i;
@@ -3063,11 +3065,13 @@ array_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
                         PySequence_GetItem(initial, i);
                     if (v == NULL) {
                         Py_DECREF(a);
+                        Py_XDECREF(it);
                         return NULL;
                     }
                     if (setarrayitem(a, i, v) != 0) {
                         Py_DECREF(v);
                         Py_DECREF(a);
+                        Py_XDECREF(it);
                         return NULL;
                     }
                     Py_DECREF(v);
@@ -3079,6 +3083,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
                 v = array_array_frombytes((PyObject *)a, initial);
                 if (v == NULL) {
                     Py_DECREF(a);
+                    Py_XDECREF(it);
                     return NULL;
                 }
                 Py_DECREF(v);
@@ -3089,6 +3094,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
                     wchar_t *ustr = PyUnicode_AsWideCharString(initial, &n);
                     if (ustr == NULL) {
                         Py_DECREF(a);
+                        Py_XDECREF(it);
                         return NULL;
                     }
 
@@ -3109,6 +3115,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
                     Py_UCS4 *ustr = PyUnicode_AsUCS4Copy(initial);
                     if (ustr == NULL) {
                         Py_DECREF(a);
+                        Py_XDECREF(it);
                         return NULL;
                     }
 
@@ -3136,6 +3143,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
             return a;
         }
     }
+    Py_XDECREF(it);
     PyErr_SetString(PyExc_ValueError,
         "bad typecode (must be b, B, u, w, h, H, i, I, l, L, q, Q, f or d)");
     return NULL;

_______________________________________________
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