Skip to content

Commit 54d58bd

Browse files
author
nascheme
committed
Fix buffer offset calculation (need to compute it before changing
'base'). Fixes SF bug #1033720. Move offset sanity checking to buffer_from_memory(). git-svn-id: http://svn.python.org/projects/python/trunk@37423 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 7b4b94b commit 54d58bd

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

Objects/bufferobject.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ buffer_from_memory(PyObject *base, int size, int offset, void *ptr,
6666
"size must be zero or positive");
6767
return NULL;
6868
}
69+
if (offset < 0) {
70+
PyErr_SetString(PyExc_ValueError,
71+
"offset must be zero or positive");
72+
return NULL;
73+
}
6974

7075
b = PyObject_NEW(PyBufferObject, &PyBuffer_Type);
7176
if ( b == NULL )
@@ -85,20 +90,11 @@ buffer_from_memory(PyObject *base, int size, int offset, void *ptr,
8590
static PyObject *
8691
buffer_from_object(PyObject *base, int size, int offset, int readonly)
8792
{
88-
if ( offset < 0 ) {
89-
PyErr_SetString(PyExc_ValueError,
90-
"offset must be zero or positive");
91-
return NULL;
92-
}
93-
94-
/* if the base object is another buffer, then try to refer to the
95-
* base object.
96-
*/
9793
if ( PyBuffer_Check(base) && (((PyBufferObject *)base)->b_base) ) {
94+
/* another buffer, refer to the base object */
95+
offset += ((PyBufferObject *)base)->b_offset;
9896
base = ((PyBufferObject *)base)->b_base;
99-
offset = ((PyBufferObject *)base)->b_offset + offset;
10097
}
101-
10298
return buffer_from_memory(base, size, offset, NULL, readonly);
10399
}
104100

0 commit comments

Comments
 (0)