GH-135763: AC: Use `Py_ssize_t(allow_negative=False)` (#138394)

This commit is contained in:
Adam Turner 2025-09-02 22:29:05 +02:00 committed by GitHub
parent 32032ee376
commit 98b4cd6fe9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 152 additions and 126 deletions

View file

@ -859,7 +859,7 @@ _ctypes.CDataType.from_buffer as CDataType_from_buffer
type: self
cls: defining_class
obj: object
offset: Py_ssize_t = 0
offset: Py_ssize_t(allow_negative=False) = 0
/
C.from_buffer(object, offset=0) -> C instance
@ -870,7 +870,7 @@ Create a C instance from a writeable buffer.
static PyObject *
CDataType_from_buffer_impl(PyObject *type, PyTypeObject *cls, PyObject *obj,
Py_ssize_t offset)
/*[clinic end generated code: output=57604e99635abd31 input=0f36cedd105ca28d]*/
/*[clinic end generated code: output=57604e99635abd31 input=8f43e6bc44373180]*/
{
PyObject *mv;
PyObject *result;
@ -906,13 +906,6 @@ CDataType_from_buffer_impl(PyObject *type, PyTypeObject *cls, PyObject *obj,
return NULL;
}
if (offset < 0) {
PyErr_SetString(PyExc_ValueError,
"offset cannot be negative");
Py_DECREF(mv);
return NULL;
}
if (info->size > buffer->len - offset) {
PyErr_Format(PyExc_ValueError,
"Buffer size too small "
@ -955,7 +948,7 @@ _ctypes.CDataType.from_buffer_copy as CDataType_from_buffer_copy
type: self
cls: defining_class
buffer: Py_buffer
offset: Py_ssize_t = 0
offset: Py_ssize_t(allow_negative=False) = 0
/
C.from_buffer_copy(object, offset=0) -> C instance
@ -966,7 +959,7 @@ Create a C instance from a readable buffer.
static PyObject *
CDataType_from_buffer_copy_impl(PyObject *type, PyTypeObject *cls,
Py_buffer *buffer, Py_ssize_t offset)
/*[clinic end generated code: output=c8fc62b03e5cc6fa input=2a81e11b765a6253]*/
/*[clinic end generated code: output=c8fc62b03e5cc6fa input=41f97f512295ceec]*/
{
PyObject *result;
@ -980,12 +973,6 @@ CDataType_from_buffer_copy_impl(PyObject *type, PyTypeObject *cls,
return NULL;
}
if (offset < 0) {
PyErr_SetString(PyExc_ValueError,
"offset cannot be negative");
return NULL;
}
if (info->size > buffer->len - offset) {
PyErr_Format(PyExc_ValueError,
"Buffer size too small (%zd instead of at least %zd bytes)",

View file

@ -176,6 +176,11 @@ CDataType_from_buffer(PyObject *type, PyTypeObject *cls, PyObject *const *args,
goto exit;
}
offset = ival;
if (offset < 0) {
PyErr_SetString(PyExc_ValueError,
"offset cannot be negative");
goto exit;
}
}
skip_optional_posonly:
return_value = CDataType_from_buffer_impl(type, cls, obj, offset);
@ -242,6 +247,11 @@ CDataType_from_buffer_copy(PyObject *type, PyTypeObject *cls, PyObject *const *a
goto exit;
}
offset = ival;
if (offset < 0) {
PyErr_SetString(PyExc_ValueError,
"offset cannot be negative");
goto exit;
}
}
skip_optional_posonly:
return_value = CDataType_from_buffer_copy_impl(type, cls, &buffer, offset);
@ -1042,4 +1052,4 @@ Simple_from_outparm(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py
}
return Simple_from_outparm_impl(self, cls);
}
/*[clinic end generated code: output=536c9bcf4e05913e input=a9049054013a1b77]*/
/*[clinic end generated code: output=22105663d71237ca input=a9049054013a1b77]*/