diff options
author | Stefan Krah <skrah@bytereef.org> | 2015-02-01 15:10:35 (GMT) |
---|---|---|
committer | Stefan Krah <skrah@bytereef.org> | 2015-02-01 15:10:35 (GMT) |
commit | 0a7fc53be297b718af0c02de3abbd1f94f78f9c1 (patch) | |
tree | d5b6918011aac1872d6a7ddcb564038ba486cdc8 /Modules/_testcapimodule.c | |
parent | caaaa1413a38f2e96511186c18ae3af02c86abaa (diff) | |
download | cpython-0a7fc53be297b718af0c02de3abbd1f94f78f9c1.zip cpython-0a7fc53be297b718af0c02de3abbd1f94f78f9c1.tar.gz cpython-0a7fc53be297b718af0c02de3abbd1f94f78f9c1.tar.bz2 |
Issue #23370: Fix off-by-one error for non-contiguous buffers.
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r-- | Modules/_testcapimodule.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 31f3a17..5062237 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -424,6 +424,58 @@ test_to_contiguous(PyObject* self, PyObject *noargs) Py_RETURN_NONE; } +static PyObject * +test_from_contiguous(PyObject* self, PyObject *noargs) +{ + int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; + int init[5] = {0, 1, 2, 3, 4}; + Py_ssize_t itemsize = sizeof(int); + Py_ssize_t shape = 5; + Py_ssize_t strides = 2 * itemsize; + Py_buffer view = { + data, + NULL, + 5 * itemsize, + itemsize, + 1, + 1, + NULL, + &shape, + &strides, + NULL, + {0, 0}, + NULL + }; + int *ptr; + int i; + + PyBuffer_FromContiguous(&view, init, view.len, 'C'); + ptr = view.buf; + for (i = 0; i < 5; i++) { + if (ptr[2*i] != i) { + PyErr_SetString(TestError, + "test_from_contiguous: incorrect result"); + return NULL; + } + } + + view.buf = &data[8]; + view.strides[0] = -2 * itemsize; + + PyBuffer_FromContiguous(&view, init, view.len, 'C'); + ptr = view.buf; + for (i = 0; i < 5; i++) { + if (*(ptr-2*i) != i) { + PyErr_SetString(TestError, + "test_from_contiguous: incorrect result"); + return NULL; + } + } + + Py_RETURN_NONE; +} + + /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) PyLong_{As, From}{Unsigned,}LongLong(). @@ -1833,6 +1885,7 @@ static PyMethodDef TestMethods[] = { {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS}, {"test_to_contiguous", (PyCFunction)test_to_contiguous, METH_NOARGS}, + {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, METH_NOARGS}, |