Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49ded3e

Browse files
committed
Added check for negative offset for PyBuffer_FromObject and check for
negative size for PyBuffer_FromMemory. Greg Stein.
1 parent 7d5f5dd commit 49ded3e

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

Objects/bufferobject.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ _PyBuffer_FromMemory(base, ptr, size, readonly)
5555
{
5656
PyBufferObject * b;
5757

58+
if ( size < 0 ) {
59+
PyErr_SetString(PyExc_ValueError,
60+
"size must be zero or positive");
61+
return NULL;
62+
}
63+
5864
b = PyObject_NEW(PyBufferObject, &PyBuffer_Type);
5965
if ( b == NULL )
6066
return NULL;
@@ -83,6 +89,12 @@ _PyBuffer_FromObject(base, offset, size, proc, readonly)
8389
void *p;
8490
int count;
8591

92+
if ( offset < 0 ) {
93+
PyErr_SetString(PyExc_ValueError,
94+
"offset must be zero or positive");
95+
return NULL;
96+
}
97+
8698
if ( (*pb->bf_getsegcount)(base, NULL) != 1 )
8799
{
88100
PyErr_SetString(PyExc_TypeError, "single-segment buffer object expected");
@@ -92,7 +104,7 @@ _PyBuffer_FromObject(base, offset, size, proc, readonly)
92104
return NULL;
93105

94106
/* apply constraints to the start/end */
95-
if ( size == Py_END_OF_BUFFER )
107+
if ( size == Py_END_OF_BUFFER || size < 0 )
96108
size = count;
97109
if ( offset > count )
98110
offset = count;

0 commit comments

Comments
 (0)