gh-111178: Avoid calling functions from incompatible pointer types in _tkinter.c (GH-112893)

Fix undefined behavior warnings (UBSan  -fsanitize=function).
This commit is contained in:
Christopher Chavez 2024-01-02 08:51:32 -06:00 committed by GitHub
parent a1eea1d032
commit f637b44dd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 18 deletions

View File

@ -735,8 +735,9 @@ newPyTclObject(Tcl_Obj *arg)
} }
static void static void
PyTclObject_dealloc(PyTclObject *self) PyTclObject_dealloc(PyObject *_self)
{ {
PyTclObject *self = (PyTclObject *)_self;
PyObject *tp = (PyObject *) Py_TYPE(self); PyObject *tp = (PyObject *) Py_TYPE(self);
Tcl_DecrRefCount(self->value); Tcl_DecrRefCount(self->value);
Py_XDECREF(self->string); Py_XDECREF(self->string);
@ -749,8 +750,9 @@ PyDoc_STRVAR(PyTclObject_string__doc__,
"the string representation of this object, either as str or bytes"); "the string representation of this object, either as str or bytes");
static PyObject * static PyObject *
PyTclObject_string(PyTclObject *self, void *ignored) PyTclObject_string(PyObject *_self, void *ignored)
{ {
PyTclObject *self = (PyTclObject *)_self;
if (!self->string) { if (!self->string) {
self->string = unicodeFromTclObj(self->value); self->string = unicodeFromTclObj(self->value);
if (!self->string) if (!self->string)
@ -760,8 +762,9 @@ PyTclObject_string(PyTclObject *self, void *ignored)
} }
static PyObject * static PyObject *
PyTclObject_str(PyTclObject *self) PyTclObject_str(PyObject *_self)
{ {
PyTclObject *self = (PyTclObject *)_self;
if (self->string) { if (self->string) {
return Py_NewRef(self->string); return Py_NewRef(self->string);
} }
@ -770,9 +773,10 @@ PyTclObject_str(PyTclObject *self)
} }
static PyObject * static PyObject *
PyTclObject_repr(PyTclObject *self) PyTclObject_repr(PyObject *_self)
{ {
PyObject *repr, *str = PyTclObject_str(self); PyTclObject *self = (PyTclObject *)_self;
PyObject *repr, *str = PyTclObject_str(_self);
if (str == NULL) if (str == NULL)
return NULL; return NULL;
repr = PyUnicode_FromFormat("<%s object: %R>", repr = PyUnicode_FromFormat("<%s object: %R>",
@ -809,23 +813,24 @@ PyTclObject_richcompare(PyObject *self, PyObject *other, int op)
PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type"); PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type");
static PyObject* static PyObject*
get_typename(PyTclObject* obj, void* ignored) get_typename(PyObject *self, void* ignored)
{ {
PyTclObject *obj = (PyTclObject *)self;
return unicodeFromTclString(obj->value->typePtr->name); return unicodeFromTclString(obj->value->typePtr->name);
} }
static PyGetSetDef PyTclObject_getsetlist[] = { static PyGetSetDef PyTclObject_getsetlist[] = {
{"typename", (getter)get_typename, NULL, get_typename__doc__}, {"typename", get_typename, NULL, get_typename__doc__},
{"string", (getter)PyTclObject_string, NULL, {"string", PyTclObject_string, NULL,
PyTclObject_string__doc__}, PyTclObject_string__doc__},
{0}, {0},
}; };
static PyType_Slot PyTclObject_Type_slots[] = { static PyType_Slot PyTclObject_Type_slots[] = {
{Py_tp_dealloc, (destructor)PyTclObject_dealloc}, {Py_tp_dealloc, PyTclObject_dealloc},
{Py_tp_repr, (reprfunc)PyTclObject_repr}, {Py_tp_repr, PyTclObject_repr},
{Py_tp_str, (reprfunc)PyTclObject_str}, {Py_tp_str, PyTclObject_str},
{Py_tp_getattro, PyObject_GenericGetAttr}, {Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_richcompare, PyTclObject_richcompare}, {Py_tp_richcompare, PyTclObject_richcompare},
{Py_tp_getset, PyTclObject_getsetlist}, {Py_tp_getset, PyTclObject_getsetlist},
@ -1306,8 +1311,9 @@ Tkapp_ObjectResult(TkappObject *self)
hold the Python lock. */ hold the Python lock. */
static int static int
Tkapp_CallProc(Tkapp_CallEvent *e, int flags) Tkapp_CallProc(Tcl_Event *evPtr, int flags)
{ {
Tkapp_CallEvent *e = (Tkapp_CallEvent *)evPtr;
Tcl_Obj *objStore[ARGSZ]; Tcl_Obj *objStore[ARGSZ];
Tcl_Obj **objv; Tcl_Obj **objv;
int objc; int objc;
@ -1385,7 +1391,7 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc; ev->ev.proc = Tkapp_CallProc;
ev->self = self; ev->self = self;
ev->args = args; ev->args = args;
ev->res = &res; ev->res = &res;
@ -1624,8 +1630,9 @@ var_perform(VarEvent *ev)
} }
static int static int
var_proc(VarEvent* ev, int flags) var_proc(Tcl_Event *evPtr, int flags)
{ {
VarEvent *ev = (VarEvent *)evPtr;
ENTER_PYTHON ENTER_PYTHON
var_perform(ev); var_perform(ev);
Tcl_MutexLock(&var_mutex); Tcl_MutexLock(&var_mutex);
@ -1663,7 +1670,7 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
ev->res = &res; ev->res = &res;
ev->exc = &exc; ev->exc = &exc;
ev->cond = &cond; ev->cond = &cond;
ev->ev.proc = (Tcl_EventProc*)var_proc; ev->ev.proc = var_proc;
Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex); Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex);
Tcl_ConditionFinalize(&cond); Tcl_ConditionFinalize(&cond);
if (!res) { if (!res) {
@ -2236,8 +2243,9 @@ typedef struct CommandEvent{
} CommandEvent; } CommandEvent;
static int static int
Tkapp_CommandProc(CommandEvent *ev, int flags) Tkapp_CommandProc(Tcl_Event *evPtr, int flags)
{ {
CommandEvent *ev = (CommandEvent *)evPtr;
if (ev->create) if (ev->create)
*ev->status = Tcl_CreateObjCommand( *ev->status = Tcl_CreateObjCommand(
ev->interp, ev->name, PythonCmd, ev->interp, ev->name, PythonCmd,
@ -2290,7 +2298,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
PyMem_Free(data); PyMem_Free(data);
return NULL; return NULL;
} }
ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; ev->ev.proc = Tkapp_CommandProc;
ev->interp = self->interp; ev->interp = self->interp;
ev->create = 1; ev->create = 1;
ev->name = name; ev->name = name;
@ -2343,7 +2351,7 @@ _tkinter_tkapp_deletecommand_impl(TkappObject *self, const char *name)
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc; ev->ev.proc = Tkapp_CommandProc;
ev->interp = self->interp; ev->interp = self->interp;
ev->create = 0; ev->create = 0;
ev->name = name; ev->name = name;