gh-140474: Fix memory leak in array.array (GH-140478)

This commit is contained in:
Stan Ulbrych 2025-10-23 10:49:27 +01:00 committed by GitHub
parent bd2c7e8c8b
commit aa9d0a61d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 0 deletions

View file

@ -1255,6 +1255,14 @@ def test_typecode_u_deprecation(self):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
array.array("u") 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): class UCS4Test(UnicodeTest):
typecode = 'w' typecode = 'w'

View file

@ -0,0 +1,2 @@
Fix memory leak in :class:`array.array` when creating arrays from an empty
:class:`str` and the ``u`` type code.

View file

@ -2833,6 +2833,9 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_SET_SIZE(self, n); Py_SET_SIZE(self, n);
self->allocated = n; self->allocated = n;
} }
else {
PyMem_Free(ustr);
}
} }
else { // c == 'w' else { // c == 'w'
Py_ssize_t n = PyUnicode_GET_LENGTH(initial); Py_ssize_t n = PyUnicode_GET_LENGTH(initial);