Skip to content

Commit 71ae212

Browse files
committed
pythonGH-91153: Handle _getbytevalue potentially invalidating bytearray allocation during item assignment
1 parent fccf9ab commit 71ae212

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Objects/bytearrayobject.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,8 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
709709
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
710710
PyByteArrayObject *self = _PyByteArray_CAST(op);
711711
Py_ssize_t start, stop, step, slicelen;
712-
char *buf = PyByteArray_AS_STRING(self);
712+
// GH-91153: we cannot store a reference to the internal buffer here, as _getbytevalue might call into python code
713+
// that could then invalidate it.
713714

714715
if (_PyIndex_Check(index)) {
715716
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
@@ -744,7 +745,7 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
744745
}
745746
else {
746747
assert(0 <= ival && ival < 256);
747-
buf[i] = (char)ival;
748+
PyByteArray_AS_STRING(self)[i] = (char)ival;
748749
return 0;
749750
}
750751
}
@@ -805,6 +806,7 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
805806
/* Delete slice */
806807
size_t cur;
807808
Py_ssize_t i;
809+
char* buf = PyByteArray_AS_STRING(self);
808810

809811
if (!_canresize(self))
810812
return -1;
@@ -845,6 +847,7 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
845847
/* Assign slice */
846848
Py_ssize_t i;
847849
size_t cur;
850+
char* buf = PyByteArray_AS_STRING(self);
848851

849852
if (needed != slicelen) {
850853
PyErr_Format(PyExc_ValueError,

0 commit comments

Comments
 (0)