Skip to content

Commit ff711ad

Browse files
author
nascheme
committed
Ensure negative offsets cannot be passed to buffer(). When composing
buffers, compute the new buffer size based on the old buffer size. Fixes SF bug #1034242. git-svn-id: http://svn.python.org/projects/python/trunk@37424 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 54d58bd commit ff711ad

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

Objects/bufferobject.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,23 @@ buffer_from_memory(PyObject *base, int size, int offset, void *ptr,
9090
static PyObject *
9191
buffer_from_object(PyObject *base, int size, int offset, int readonly)
9292
{
93+
if (offset < 0) {
94+
PyErr_SetString(PyExc_ValueError,
95+
"offset must be zero or positive");
96+
return NULL;
97+
}
9398
if ( PyBuffer_Check(base) && (((PyBufferObject *)base)->b_base) ) {
9499
/* another buffer, refer to the base object */
95-
offset += ((PyBufferObject *)base)->b_offset;
96-
base = ((PyBufferObject *)base)->b_base;
100+
PyBufferObject *b = (PyBufferObject *)base;
101+
if (b->b_size != Py_END_OF_BUFFER) {
102+
int base_size = b->b_size - offset;
103+
if (base_size < 0)
104+
base_size = 0;
105+
if (size == Py_END_OF_BUFFER || size > base_size)
106+
size = base_size;
107+
}
108+
offset += b->b_offset;
109+
base = b->b_base;
97110
}
98111
return buffer_from_memory(base, size, offset, NULL, readonly);
99112
}

0 commit comments

Comments
 (0)