gh-111178: Fix function signatures to fix undefined behavior (#131191)

This commit is contained in:
Victor Stinner 2025-03-14 10:52:15 +01:00 committed by GitHub
parent e4ac196aaa
commit a5776639c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 28 additions and 20 deletions

View file

@ -461,8 +461,9 @@ start_failed:
} }
static int static int
join_thread(ThreadHandle *handle) join_thread(void *arg)
{ {
ThreadHandle *handle = (ThreadHandle*)arg;
assert(get_thread_handle_state(handle) == THREAD_HANDLE_RUNNING); assert(get_thread_handle_state(handle) == THREAD_HANDLE_RUNNING);
PyThread_handle_t os_handle; PyThread_handle_t os_handle;
if (ThreadHandle_get_os_handle(handle, &os_handle)) { if (ThreadHandle_get_os_handle(handle, &os_handle)) {
@ -536,8 +537,7 @@ ThreadHandle_join(ThreadHandle *self, PyTime_t timeout_ns)
} }
} }
if (_PyOnceFlag_CallOnce(&self->once, (_Py_once_fn_t *)join_thread, if (_PyOnceFlag_CallOnce(&self->once, join_thread, self) == -1) {
self) == -1) {
return -1; return -1;
} }
assert(get_thread_handle_state(self) == THREAD_HANDLE_DONE); assert(get_thread_handle_state(self) == THREAD_HANDLE_DONE);

View file

@ -1361,7 +1361,8 @@ lineiter_dealloc(PyObject *self)
} }
static PyObject * static PyObject *
_source_offset_converter(int *value) { _source_offset_converter(void *arg) {
int *value = (int*)arg;
if (*value == -1) { if (*value == -1) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }

View file

@ -590,8 +590,9 @@ failed_throw:
static PyObject * static PyObject *
gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs) gen_throw(PyObject *op, PyObject *const *args, Py_ssize_t nargs)
{ {
PyGenObject *gen = _PyGen_CAST(op);
PyObject *typ; PyObject *typ;
PyObject *tb = NULL; PyObject *tb = NULL;
PyObject *val = NULL; PyObject *val = NULL;
@ -821,8 +822,9 @@ static PyMemberDef gen_memberlist[] = {
}; };
static PyObject * static PyObject *
gen_sizeof(PyGenObject *gen, PyObject *Py_UNUSED(ignored)) gen_sizeof(PyObject *op, PyObject *Py_UNUSED(ignored))
{ {
PyGenObject *gen = _PyGen_CAST(op);
Py_ssize_t res; Py_ssize_t res;
res = offsetof(PyGenObject, gi_iframe) + offsetof(_PyInterpreterFrame, localsplus); res = offsetof(PyGenObject, gi_iframe) + offsetof(_PyInterpreterFrame, localsplus);
PyCodeObject *code = _PyGen_GetCode(gen); PyCodeObject *code = _PyGen_GetCode(gen);
@ -837,7 +839,7 @@ static PyMethodDef gen_methods[] = {
{"send", gen_send, METH_O, send_doc}, {"send", gen_send, METH_O, send_doc},
{"throw", _PyCFunction_CAST(gen_throw), METH_FASTCALL, throw_doc}, {"throw", _PyCFunction_CAST(gen_throw), METH_FASTCALL, throw_doc},
{"close", gen_close, METH_NOARGS, close_doc}, {"close", gen_close, METH_NOARGS, close_doc},
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, {"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
@ -1197,7 +1199,7 @@ static PyMethodDef coro_methods[] = {
{"send", gen_send, METH_O, coro_send_doc}, {"send", gen_send, METH_O, coro_send_doc},
{"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, coro_throw_doc}, {"throw",_PyCFunction_CAST(gen_throw), METH_FASTCALL, coro_throw_doc},
{"close", gen_close, METH_NOARGS, coro_close_doc}, {"close", gen_close, METH_NOARGS, coro_close_doc},
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, {"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
@ -1288,7 +1290,7 @@ static PyObject *
coro_wrapper_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs) coro_wrapper_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
{ {
PyCoroWrapper *cw = _PyCoroWrapper_CAST(self); PyCoroWrapper *cw = _PyCoroWrapper_CAST(self);
return gen_throw((PyGenObject *)cw->cw_coroutine, args, nargs); return gen_throw((PyObject*)cw->cw_coroutine, args, nargs);
} }
static PyObject * static PyObject *
@ -1625,7 +1627,7 @@ static PyMethodDef async_gen_methods[] = {
{"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc}, {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc},
{"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc}, {"athrow",(PyCFunction)async_gen_athrow, METH_VARARGS, async_athrow_doc},
{"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc}, {"aclose", (PyCFunction)async_gen_aclose, METH_NOARGS, async_aclose_doc},
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, {"__sizeof__", gen_sizeof, METH_NOARGS, sizeof__doc__},
{"__class_getitem__", Py_GenericAlias, {"__class_getitem__", Py_GenericAlias,
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
@ -1842,7 +1844,7 @@ async_gen_asend_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
o->ags_gen->ag_running_async = 1; o->ags_gen->ag_running_async = 1;
} }
PyObject *result = gen_throw((PyGenObject*)o->ags_gen, args, nargs); PyObject *result = gen_throw((PyObject*)o->ags_gen, args, nargs);
result = async_gen_unwrap_value(o->ags_gen, result); result = async_gen_unwrap_value(o->ags_gen, result);
if (result == NULL) { if (result == NULL) {
@ -2249,7 +2251,7 @@ async_gen_athrow_throw(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
o->agt_gen->ag_running_async = 1; o->agt_gen->ag_running_async = 1;
} }
PyObject *retval = gen_throw((PyGenObject*)o->agt_gen, args, nargs); PyObject *retval = gen_throw((PyObject*)o->agt_gen, args, nargs);
if (o->agt_args) { if (o->agt_args) {
retval = async_gen_unwrap_value(o->agt_gen, retval); retval = async_gen_unwrap_value(o->agt_gen, retval);
if (retval == NULL) { if (retval == NULL) {

View file

@ -200,8 +200,9 @@ PyCallIter_New(PyObject *callable, PyObject *sentinel)
return (PyObject *)it; return (PyObject *)it;
} }
static void static void
calliter_dealloc(calliterobject *it) calliter_dealloc(PyObject *op)
{ {
calliterobject *it = (calliterobject*)op;
_PyObject_GC_UNTRACK(it); _PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_callable); Py_XDECREF(it->it_callable);
Py_XDECREF(it->it_sentinel); Py_XDECREF(it->it_sentinel);
@ -217,8 +218,9 @@ calliter_traverse(calliterobject *it, visitproc visit, void *arg)
} }
static PyObject * static PyObject *
calliter_iternext(calliterobject *it) calliter_iternext(PyObject *op)
{ {
calliterobject *it = (calliterobject*)op;
PyObject *result; PyObject *result;
if (it->it_callable == NULL) { if (it->it_callable == NULL) {
@ -249,8 +251,9 @@ calliter_iternext(calliterobject *it)
} }
static PyObject * static PyObject *
calliter_reduce(calliterobject *it, PyObject *Py_UNUSED(ignored)) calliter_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
{ {
calliterobject *it = (calliterobject*)op;
PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter)); PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
/* _PyEval_GetBuiltin can invoke arbitrary code, /* _PyEval_GetBuiltin can invoke arbitrary code,
@ -264,7 +267,7 @@ calliter_reduce(calliterobject *it, PyObject *Py_UNUSED(ignored))
} }
static PyMethodDef calliter_methods[] = { static PyMethodDef calliter_methods[] = {
{"__reduce__", (PyCFunction)calliter_reduce, METH_NOARGS, reduce_doc}, {"__reduce__", calliter_reduce, METH_NOARGS, reduce_doc},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
@ -274,7 +277,7 @@ PyTypeObject PyCallIter_Type = {
sizeof(calliterobject), /* tp_basicsize */ sizeof(calliterobject), /* tp_basicsize */
0, /* tp_itemsize */ 0, /* tp_itemsize */
/* methods */ /* methods */
(destructor)calliter_dealloc, /* tp_dealloc */ calliter_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */ 0, /* tp_vectorcall_offset */
0, /* tp_getattr */ 0, /* tp_getattr */
0, /* tp_setattr */ 0, /* tp_setattr */
@ -296,7 +299,7 @@ PyTypeObject PyCallIter_Type = {
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */ PyObject_SelfIter, /* tp_iter */
(iternextfunc)calliter_iternext, /* tp_iternext */ calliter_iternext, /* tp_iternext */
calliter_methods, /* tp_methods */ calliter_methods, /* tp_methods */
}; };

View file

@ -137,8 +137,10 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
} }
static void static void
mutex_unpark(PyMutex *m, struct mutex_entry *entry, int has_more_waiters) mutex_unpark(void *arg, void *park_arg, int has_more_waiters)
{ {
PyMutex *m = (PyMutex*)arg;
struct mutex_entry *entry = (struct mutex_entry*)park_arg;
uint8_t v = 0; uint8_t v = 0;
if (entry) { if (entry) {
PyTime_t now; PyTime_t now;
@ -168,7 +170,7 @@ _PyMutex_TryUnlock(PyMutex *m)
} }
else if ((v & _Py_HAS_PARKED)) { else if ((v & _Py_HAS_PARKED)) {
// wake up a single thread // wake up a single thread
_PyParkingLot_Unpark(&m->_bits, (_Py_unpark_fn_t *)mutex_unpark, m); _PyParkingLot_Unpark(&m->_bits, mutex_unpark, m);
return 0; return 0;
} }
else if (_Py_atomic_compare_exchange_uint8(&m->_bits, &v, _Py_UNLOCKED)) { else if (_Py_atomic_compare_exchange_uint8(&m->_bits, &v, _Py_UNLOCKED)) {