mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-91153: prevent a crash in bytearray.__setitem__(ind, ...) when ind.__index__ has side-effects (GH-132379) (#136581)
gh-91153: prevent a crash in `bytearray.__setitem__(ind, ...)` when `ind.__index__` has side-effects (GH-132379)
(cherry picked from commit 5e1e21dee3)
Co-authored-by: Bast <52266665+bast0006@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
c71ecd1418
commit
d5866a8e84
3 changed files with 42 additions and 2 deletions
|
|
@ -709,7 +709,9 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
|
|||
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
|
||||
PyByteArrayObject *self = _PyByteArray_CAST(op);
|
||||
Py_ssize_t start, stop, step, slicelen;
|
||||
char *buf = PyByteArray_AS_STRING(self);
|
||||
// Do not store a reference to the internal buffer since
|
||||
// index.__index__() or _getbytevalue() may alter 'self'.
|
||||
// See https://github.com/python/cpython/issues/91153.
|
||||
|
||||
if (_PyIndex_Check(index)) {
|
||||
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
|
||||
|
|
@ -744,7 +746,7 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
|
|||
}
|
||||
else {
|
||||
assert(0 <= ival && ival < 256);
|
||||
buf[i] = (char)ival;
|
||||
PyByteArray_AS_STRING(self)[i] = (char)ival;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -805,6 +807,7 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
|
|||
/* Delete slice */
|
||||
size_t cur;
|
||||
Py_ssize_t i;
|
||||
char *buf = PyByteArray_AS_STRING(self);
|
||||
|
||||
if (!_canresize(self))
|
||||
return -1;
|
||||
|
|
@ -845,6 +848,7 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
|
|||
/* Assign slice */
|
||||
Py_ssize_t i;
|
||||
size_t cur;
|
||||
char *buf = PyByteArray_AS_STRING(self);
|
||||
|
||||
if (needed != slicelen) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue