Visitar URL original
gh-158923: Fix use-after-free in xibufferview_getbuf() by christianaurichzm · Pull Request #159005 · python/cpython · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,21 @@ def test_running(self):
interp.prepare_main({'spam': False})
interp.exec('assert spam is True')

def test_memoryview_obj_buffer(self):
# gh-158923: Exporting a buffer from the object behind a shared
# memoryview must not drop a reference to it.
interp = interpreters.create()
interp.prepare_main(mv=memoryview(bytearray(b'spam')))
interp.exec(dedent("""
obj = mv.obj
for _ in range(10):
assert bytes(obj) == b'spam'
with memoryview(obj) as m:
assert m.tobytes() == b'spam'
del obj
mv.release()
"""))

@requires_test_modules
def test_created_with_capi(self):
with self.interpreter_obj_from_capi() as (interp, interpid):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash when exporting a buffer from the object behind a
:class:`memoryview` shared with another interpreter via
:mod:`concurrent.interpreters`. Patch by Christian Aurich Zanettini Martins.
6 changes: 2 additions & 4 deletions Modules/_interpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,10 @@ xibufferview_dealloc(PyObject *op)
static int
xibufferview_getbuf(PyObject *op, Py_buffer *view, int flags)
{
/* Only PyMemoryView_FromObject() should ever call this,
via _memoryview_from_xid() below. */
xibufferview *self = (xibufferview *)op;
*view = *self->view;
/* This is the workaround mentioned earlier. */
view->obj = op;
view->obj = Py_NewRef(op);
// XXX Should we leave it alone?
view->internal = NULL;
return 0;
Expand Down Expand Up @@ -254,8 +252,8 @@ _memoryview_from_xid(_PyXIData_t *data)
return NULL;
}
PyObject *res = PyMemoryView_FromObject(obj);
Py_DECREF(obj);
if (res == NULL) {
Py_DECREF(obj);
return NULL;
}
view->used = 1;
Expand Down
Loading