Skip to content

Commit fe4b75b

Browse files
chrstphrchvzkulikjak
authored andcommitted
pythongh-111178: Avoid calling functions from incompatible pointer types in _tkinter.c (pythonGH-112893)
Fix undefined behavior warnings (UBSan -fsanitize=function).
1 parent 3522030 commit fe4b75b

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

Modules/_tkinter.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -735,8 +735,9 @@ newPyTclObject(Tcl_Obj *arg)
735735
}
736736

737737
static void
738-
PyTclObject_dealloc(PyTclObject *self)
738+
PyTclObject_dealloc(PyObject *_self)
739739
{
740+
PyTclObject *self = (PyTclObject *)_self;
740741
PyObject *tp = (PyObject *) Py_TYPE(self);
741742
Tcl_DecrRefCount(self->value);
742743
Py_XDECREF(self->string);
@@ -749,8 +750,9 @@ PyDoc_STRVAR(PyTclObject_string__doc__,
749750
"the string representation of this object, either as str or bytes");
750751

751752
static PyObject *
752-
PyTclObject_string(PyTclObject *self, void *ignored)
753+
PyTclObject_string(PyObject *_self, void *ignored)
753754
{
755+
PyTclObject *self = (PyTclObject *)_self;
754756
if (!self->string) {
755757
self->string = unicodeFromTclObj(self->value);
756758
if (!self->string)
@@ -760,8 +762,9 @@ PyTclObject_string(PyTclObject *self, void *ignored)
760762
}
761763

762764
static PyObject *
763-
PyTclObject_str(PyTclObject *self)
765+
PyTclObject_str(PyObject *_self)
764766
{
767+
PyTclObject *self = (PyTclObject *)_self;
765768
if (self->string) {
766769
return Py_NewRef(self->string);
767770
}
@@ -770,9 +773,10 @@ PyTclObject_str(PyTclObject *self)
770773
}
771774

772775
static PyObject *
773-
PyTclObject_repr(PyTclObject *self)
776+
PyTclObject_repr(PyObject *_self)
774777
{
775-
PyObject *repr, *str = PyTclObject_str(self);
778+
PyTclObject *self = (PyTclObject *)_self;
779+
PyObject *repr, *str = PyTclObject_str(_self);
776780
if (str == NULL)
777781
return NULL;
778782
repr = PyUnicode_FromFormat("<%s object: %R>",
@@ -809,23 +813,24 @@ PyTclObject_richcompare(PyObject *self, PyObject *other, int op)
809813
PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type");
810814

811815
static PyObject*
812-
get_typename(PyTclObject* obj, void* ignored)
816+
get_typename(PyObject *self, void* ignored)
813817
{
818+
PyTclObject *obj = (PyTclObject *)self;
814819
return unicodeFromTclString(obj->value->typePtr->name);
815820
}
816821

817822

818823
static PyGetSetDef PyTclObject_getsetlist[] = {
819-
{"typename", (getter)get_typename, NULL, get_typename__doc__},
820-
{"string", (getter)PyTclObject_string, NULL,
824+
{"typename", get_typename, NULL, get_typename__doc__},
825+
{"string", PyTclObject_string, NULL,
821826
PyTclObject_string__doc__},
822827
{0},
823828
};
824829

825830
static PyType_Slot PyTclObject_Type_slots[] = {
826-
{Py_tp_dealloc, (destructor)PyTclObject_dealloc},
827-
{Py_tp_repr, (reprfunc)PyTclObject_repr},
828-
{Py_tp_str, (reprfunc)PyTclObject_str},
831+
{Py_tp_dealloc, PyTclObject_dealloc},
832+
{Py_tp_repr, PyTclObject_repr},
833+
{Py_tp_str, PyTclObject_str},
829834
{Py_tp_getattro, PyObject_GenericGetAttr},
830835
{Py_tp_richcompare, PyTclObject_richcompare},
831836
{Py_tp_getset, PyTclObject_getsetlist},
@@ -1306,8 +1311,9 @@ Tkapp_ObjectResult(TkappObject *self)
13061311
hold the Python lock. */
13071312

13081313
static int
1309-
Tkapp_CallProc(Tkapp_CallEvent *e, int flags)
1314+
Tkapp_CallProc(Tcl_Event *evPtr, int flags)
13101315
{
1316+
Tkapp_CallEvent *e = (Tkapp_CallEvent *)evPtr;
13111317
Tcl_Obj *objStore[ARGSZ];
13121318
Tcl_Obj **objv;
13131319
int objc;
@@ -1385,7 +1391,7 @@ Tkapp_Call(PyObject *selfptr, PyObject *args)
13851391
PyErr_NoMemory();
13861392
return NULL;
13871393
}
1388-
ev->ev.proc = (Tcl_EventProc*)Tkapp_CallProc;
1394+
ev->ev.proc = Tkapp_CallProc;
13891395
ev->self = self;
13901396
ev->args = args;
13911397
ev->res = &res;
@@ -1624,8 +1630,9 @@ var_perform(VarEvent *ev)
16241630
}
16251631

16261632
static int
1627-
var_proc(VarEvent* ev, int flags)
1633+
var_proc(Tcl_Event *evPtr, int flags)
16281634
{
1635+
VarEvent *ev = (VarEvent *)evPtr;
16291636
ENTER_PYTHON
16301637
var_perform(ev);
16311638
Tcl_MutexLock(&var_mutex);
@@ -1663,7 +1670,7 @@ var_invoke(EventFunc func, PyObject *selfptr, PyObject *args, int flags)
16631670
ev->res = &res;
16641671
ev->exc = &exc;
16651672
ev->cond = &cond;
1666-
ev->ev.proc = (Tcl_EventProc*)var_proc;
1673+
ev->ev.proc = var_proc;
16671674
Tkapp_ThreadSend(self, (Tcl_Event*)ev, &cond, &var_mutex);
16681675
Tcl_ConditionFinalize(&cond);
16691676
if (!res) {
@@ -2236,8 +2243,9 @@ typedef struct CommandEvent{
22362243
} CommandEvent;
22372244

22382245
static int
2239-
Tkapp_CommandProc(CommandEvent *ev, int flags)
2246+
Tkapp_CommandProc(Tcl_Event *evPtr, int flags)
22402247
{
2248+
CommandEvent *ev = (CommandEvent *)evPtr;
22412249
if (ev->create)
22422250
*ev->status = Tcl_CreateObjCommand(
22432251
ev->interp, ev->name, PythonCmd,
@@ -2290,7 +2298,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
22902298
PyMem_Free(data);
22912299
return NULL;
22922300
}
2293-
ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
2301+
ev->ev.proc = Tkapp_CommandProc;
22942302
ev->interp = self->interp;
22952303
ev->create = 1;
22962304
ev->name = name;
@@ -2343,7 +2351,7 @@ _tkinter_tkapp_deletecommand_impl(TkappObject *self, const char *name)
23432351
PyErr_NoMemory();
23442352
return NULL;
23452353
}
2346-
ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
2354+
ev->ev.proc = Tkapp_CommandProc;
23472355
ev->interp = self->interp;
23482356
ev->create = 0;
23492357
ev->name = name;

0 commit comments

Comments
 (0)