Skip to content

Commit 640bc25

Browse files
[3.13] pythongh-140474: Fix memory leak in array.array (pythonGH-140478) (pythonGH-140499)
pythongh-140474: Fix memory leak in `array.array` (pythonGH-140478) (cherry picked from commit aa9d0a6) Co-authored-by: Stan Ulbrych <[email protected]>
1 parent 30c2661 commit 640bc25

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/test/test_array.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,14 @@ def test_typecode_u_deprecation(self):
12551255
with self.assertWarns(DeprecationWarning):
12561256
array.array("u")
12571257

1258+
def test_empty_string_mem_leak_gh140474(self):
1259+
with warnings.catch_warnings():
1260+
warnings.simplefilter('ignore', DeprecationWarning)
1261+
for _ in range(1000):
1262+
a = array.array('u', '')
1263+
self.assertEqual(len(a), 0)
1264+
self.assertEqual(a.typecode, 'u')
1265+
12581266

12591267
class UCS4Test(UnicodeTest):
12601268
typecode = 'w'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix memory leak in :class:`array.array` when creating arrays from an empty
2+
:class:`str` and the ``u`` type code.

Modules/arraymodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,6 +2806,9 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
28062806
Py_SET_SIZE(self, n);
28072807
self->allocated = n;
28082808
}
2809+
else {
2810+
PyMem_Free(ustr);
2811+
}
28092812
}
28102813
else { // c == 'w'
28112814
Py_ssize_t n = PyUnicode_GET_LENGTH(initial);

0 commit comments

Comments
 (0)