]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-140474: Fix memory leak in `array.array` (GH-140478) (GH-140498)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 23 Oct 2025 10:20:16 +0000 (12:20 +0200)
committerGitHub <noreply@github.com>
Thu, 23 Oct 2025 10:20:16 +0000 (10:20 +0000)
gh-140474: Fix memory leak in `array.array` (GH-140478)
(cherry picked from commit aa9d0a61d5c48717454f36351f0aabe4cc532de5)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Lib/test/test_array.py
Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst [new file with mode: 0644]
Modules/arraymodule.c

index 58ea89c4fac833bbe0a9c61b1e9d837b434f6a7a..83b3c978da3581d6b4255b392174223fa0b9978a 100755 (executable)
@@ -1255,6 +1255,14 @@ class UnicodeTest(StringTest, unittest.TestCase):
         with self.assertWarns(DeprecationWarning):
             array.array("u")
 
+    def test_empty_string_mem_leak_gh140474(self):
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', DeprecationWarning)
+            for _ in range(1000):
+                a = array.array('u', '')
+                self.assertEqual(len(a), 0)
+                self.assertEqual(a.typecode, 'u')
+
 
 class UCS4Test(UnicodeTest):
     typecode = 'w'
diff --git a/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst b/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst
new file mode 100644 (file)
index 0000000..aca4e68
--- /dev/null
@@ -0,0 +1,2 @@
+Fix memory leak in :class:`array.array` when creating arrays from an empty
+:class:`str` and the ``u`` type code.
index 5d07de2fba952653d81f739da76d0e5e0530f692..39d505c02596b8a47224acb2018f76fc902e9ac4 100644 (file)
@@ -2838,6 +2838,9 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
                         Py_SET_SIZE(self, n);
                         self->allocated = n;
                     }
+                    else {
+                        PyMem_Free(ustr);
+                    }
                 }
                 else { // c == 'w'
                     Py_ssize_t n = PyUnicode_GET_LENGTH(initial);