Skip to content

Commit cf74c19

Browse files
Issue python#24613: Calling array.fromstring() with self is no longer allowed
to prevent the use-after-free error. Patch by John Leitch.
1 parent 7f18ac4 commit cf74c19

3 files changed

Lines changed: 9 additions & 0 deletions

File tree

‎Lib/test/test_array.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ def test_tofromstring(self):
247247
self.assertRaises(TypeError, a.tostring, 42)
248248
self.assertRaises(TypeError, b.fromstring)
249249
self.assertRaises(TypeError, b.fromstring, 42)
250+
self.assertRaises(ValueError, a.fromstring, a)
250251
b.fromstring(a.tostring())
251252
self.assertEqual(a, b)
252253
if a.itemsize>1:

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Core and Builtins
3434
Library
3535
-------
3636

37+
- Issue #24613: Calling array.fromstring() with self is no longer allowed
38+
to prevent the use-after-free error. Patch by John Leitch.
39+
3740
- Issue #24708: Fix possible integer overflow in strop.replace().
3841

3942
- Issue #24620: Random.setstate() now validates the value of state last element.

‎Modules/arraymodule.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,11 @@ array_fromstring(arrayobject *self, PyObject *args)
13801380
int itemsize = self->ob_descr->itemsize;
13811381
if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n))
13821382
return NULL;
1383+
if (str == self->ob_item) {
1384+
PyErr_SetString(PyExc_ValueError,
1385+
"array.fromstring(x): x cannot be self");
1386+
return NULL;
1387+
}
13831388
if (n % itemsize != 0) {
13841389
PyErr_SetString(PyExc_ValueError,
13851390
"string length not a multiple of item size");

0 commit comments

Comments
 (0)