https://github.com/python/cpython/commit/813498411ac74a58ca79ff12b622e63ba5b7a039
commit: 813498411ac74a58ca79ff12b622e63ba5b7a039
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: encukou <[email protected]>
date: 2026-04-23T16:00:47+02:00
summary:

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

(cherry picked from commit afde75664eb3ff3e147806f027c9da54c7eb77d4)

Co-authored-by: Gleb Popov <[email protected]>

files:
M Modules/arraymodule.c

diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 4c7cc4d47c06d7..7ec632e250ff8c 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -2821,8 +2821,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;
@@ -2831,11 +2833,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);
@@ -2848,6 +2852,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject 
*kwds)
                                           initial);
                 if (v == NULL) {
                     Py_DECREF(a);
+                    Py_XDECREF(it);
                     return NULL;
                 }
                 Py_DECREF(v);
@@ -2858,6 +2863,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;
                     }
 
@@ -2878,6 +2884,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;
                     }
 
@@ -2905,6 +2912,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