Skip to content

Commit 3013b16

Browse files
committed
Merged revisions 88097 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r88097 | antoine.pitrou | 2011-01-18 19:57:52 +0100 (mar., 18 janv. 2011) | 4 lines Issue #10451: memoryview objects could allow to mutate a readable buffer. Initial patch by Ross Lagerwall. ........
1 parent 4a70550 commit 3013b16

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

Lib/test/test_memoryview.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import weakref
1010
import array
1111
from test import test_support
12+
import io
1213

1314

1415
class AbstractMemoryTests:
@@ -230,6 +231,16 @@ class MyObject:
230231
gc.collect()
231232
self.assertTrue(wr() is None, wr())
232233

234+
def test_writable_readonly(self):
235+
# Issue #10451: memoryview incorrectly exposes a readonly
236+
# buffer as writable causing a segfault if using mmap
237+
tp = self.ro_type
238+
if tp is None:
239+
return
240+
b = tp(self._source)
241+
m = self._view(b)
242+
i = io.BytesIO(b'ZZZZ')
243+
self.assertRaises(TypeError, i.readinto, m)
233244

234245
# Variations on source objects for the buffer: bytes-like objects, then arrays
235246
# with itemsize > 1.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ What's New in Python 2.7.2?
99
Core and Builtins
1010
-----------------
1111

12+
- Issue #10451: memoryview objects could allow to mutate a readable buffer.
13+
Initial patch by Ross Lagerwall.
14+
1215
- Issue #10892: Don't segfault when trying to delete __abstractmethods__ from a
1316
class.
1417

Objects/memoryobject.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ static int
3434
memory_getbuf(PyMemoryViewObject *self, Py_buffer *view, int flags)
3535
{
3636
int res = 0;
37-
/* XXX for whatever reason fixing the flags seems necessary */
38-
if (self->view.readonly)
39-
flags &= ~PyBUF_WRITABLE;
4037
if (self->view.obj != NULL)
4138
res = PyObject_GetBuffer(self->view.obj, view, flags);
4239
if (view)
@@ -411,7 +408,7 @@ memory_tobytes(PyMemoryViewObject *self, PyObject *noargs)
411408
Py_buffer view;
412409
PyObject *res;
413410

414-
if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_FULL) < 0)
411+
if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_SIMPLE) < 0)
415412
return NULL;
416413

417414
res = PyBytes_FromStringAndSize(NULL, view.len);

0 commit comments

Comments
 (0)