-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-91153: Fix bytearray holding a reference to its internal buffer when calling into potentially mutating __index__ methods #132379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
71ae212
b2f7df5
bb96c2a
d29422b
d3d1974
18e6597
4b7ec9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a crash when a :class:`bytearray` is concurrently mutated during item assignment. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to fully link to it, just the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. picnixz suggested this above, and it's common for the c source to have complete links, so I think it's fine as-is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With complete links, I can open them from my IDE. It's much easier that way IMO |
||||||
|
||||||
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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also hold There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, but I'm not as familiar with this system or C as I would want to be to feel comfortable making that change. Especially since the relevant code is mostly >10 years old and that change is not immediately required to solve the issue. |
||||||
|
||||||
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, | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.