Skip to content

Commit 4214d56

Browse files
committed
chore: resolve comment
1 parent 87225ad commit 4214d56

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Fix a potential use‑after‑free in bytearray search‑like methods by exporting the buffer during the call. bytearray.split() and bytearray.rsplit() now also export the buffer; subclasses overriding the buffer protocol may observe behavior changes.
1+
Fix use-after-free in :class:`bytearray` search-like methods (:func:`~bytearray.find`, :func:`~bytearray.count`, :func:`~bytearray.index`, :func:`~bytearray.rindex`, and :func:`~bytearray.rfind`) by marking the storage as
2+
exported which causes reallocation attempts to raise :exc:`BufferError`. For :func:`~bytearray.contains`, :func:`~bytearray.split`, and :func:`~bytearray.rsplit` the :ref:`buffer protocol <bufferobjects>` is used for this.

Objects/bytearrayobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ _bytearray_with_buffer(PyByteArrayObject *self, PyObject *sub,
102102

103103
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
104104

105+
/* Increase exports to prevent bytearray storage from changing during op. */
105106
self->ob_exports++;
106107
res = op(PyByteArray_AS_STRING(self), Py_SIZE(self), sub, start, end);
107108
self->ob_exports--;
@@ -1358,10 +1359,9 @@ bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,
13581359
static int
13591360
bytearray_contains(PyObject *self, PyObject *arg)
13601361
{
1361-
int ret;
1362+
int ret = -1;
13621363
Py_buffer selfbuf;
13631364
Py_BEGIN_CRITICAL_SECTION(self);
1364-
ret = -1;
13651365
if (PyObject_GetBuffer((PyObject *)self, &selfbuf, PyBUF_SIMPLE) == 0) {
13661366
ret = _Py_bytes_contains((const char *)selfbuf.buf, selfbuf.len, arg);
13671367
PyBuffer_Release(&selfbuf);

0 commit comments

Comments
 (0)