From 2683a2512c389bbc13f2854cd6982efd0d043da7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 13:48:37 +0100 Subject: [PATCH 01/13] fix UBSan failures for `PyBaseExceptionObject` --- Objects/exceptions.c | 116 ++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 52 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 6880c24196cbb8..b04567edccb503 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -37,6 +37,13 @@ get_exc_state(void) * Lib/test/exception_hierarchy.txt */ +static inline PyBaseExceptionObject * +_PyBaseExceptionObject_CAST(PyObject *exc) +{ + assert(PyExceptionInstance_Check(exc)); + return (PyBaseExceptionObject *)exc; +} + /* * BaseException */ @@ -69,8 +76,9 @@ BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } static int -BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds) +BaseException_init(PyObject *op, PyObject *args, PyObject *kwds) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) return -1; @@ -113,8 +121,9 @@ BaseException_vectorcall(PyObject *type_obj, PyObject * const*args, static int -BaseException_clear(PyBaseExceptionObject *self) +BaseException_clear(PyObject *op) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); Py_CLEAR(self->dict); Py_CLEAR(self->args); Py_CLEAR(self->notes); @@ -125,21 +134,23 @@ BaseException_clear(PyBaseExceptionObject *self) } static void -BaseException_dealloc(PyBaseExceptionObject *self) +BaseException_dealloc(PyObject *op) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); PyObject_GC_UnTrack(self); // bpo-44348: The trashcan mechanism prevents stack overflow when deleting // long chains of exceptions. For example, exceptions can be chained // through the __context__ attributes or the __traceback__ attribute. Py_TRASHCAN_BEGIN(self, BaseException_dealloc) - BaseException_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + (void)BaseException_clear(op); + Py_TYPE(self)->tp_free(self); Py_TRASHCAN_END } static int -BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg) +BaseException_traverse(PyObject *op, visitproc visit, void *arg) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); Py_VISIT(self->dict); Py_VISIT(self->args); Py_VISIT(self->notes); @@ -150,8 +161,9 @@ BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg) } static PyObject * -BaseException_str(PyBaseExceptionObject *self) +BaseException_str(PyObject *op) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); switch (PyTuple_GET_SIZE(self->args)) { case 0: return Py_GetConstant(Py_CONSTANT_EMPTY_STR); @@ -163,8 +175,9 @@ BaseException_str(PyBaseExceptionObject *self) } static PyObject * -BaseException_repr(PyBaseExceptionObject *self) +BaseException_repr(PyObject *op) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); const char *name = _PyType_Name(Py_TYPE(self)); if (PyTuple_GET_SIZE(self->args) == 1) return PyUnicode_FromFormat("%s(%R)", name, @@ -175,8 +188,9 @@ BaseException_repr(PyBaseExceptionObject *self) /* Pickling support */ static PyObject * -BaseException_reduce(PyBaseExceptionObject *self, PyObject *Py_UNUSED(ignored)) +BaseException_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); if (self->args && self->dict) return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict); else @@ -225,13 +239,6 @@ PyDoc_STRVAR(with_traceback_doc, "Exception.with_traceback(tb) --\n\ set self.__traceback__ to tb and return self."); -static inline PyBaseExceptionObject* -_PyBaseExceptionObject_cast(PyObject *exc) -{ - assert(PyExceptionInstance_Check(exc)); - return (PyBaseExceptionObject *)exc; -} - static PyObject * BaseException_add_note(PyObject *self, PyObject *note) { @@ -274,18 +281,18 @@ PyDoc_STRVAR(add_note_doc, add a note to the exception"); static PyMethodDef BaseException_methods[] = { - {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS }, - {"__setstate__", (PyCFunction)BaseException_setstate, METH_O }, - {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O, + {"__reduce__", BaseException_reduce, METH_NOARGS}, + {"__setstate__", BaseException_setstate, METH_O}, + {"with_traceback", BaseException_with_traceback, METH_O, with_traceback_doc}, - {"add_note", (PyCFunction)BaseException_add_note, METH_O, - add_note_doc}, + {"add_note", BaseException_add_note, METH_O, add_note_doc}, {NULL, NULL, 0, NULL}, }; static PyObject * -BaseException_get_args(PyBaseExceptionObject *self, void *Py_UNUSED(ignored)) +BaseException_get_args(PyObject *op, void *Py_UNUSED(ignored)) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); if (self->args == NULL) { Py_RETURN_NONE; } @@ -293,7 +300,7 @@ BaseException_get_args(PyBaseExceptionObject *self, void *Py_UNUSED(ignored)) } static int -BaseException_set_args(PyBaseExceptionObject *self, PyObject *val, void *Py_UNUSED(ignored)) +BaseException_set_args(PyObject *op, PyObject *val, void *Py_UNUSED(ignored)) { PyObject *seq; if (val == NULL) { @@ -303,13 +310,15 @@ BaseException_set_args(PyBaseExceptionObject *self, PyObject *val, void *Py_UNUS seq = PySequence_Tuple(val); if (!seq) return -1; + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); Py_XSETREF(self->args, seq); return 0; } static PyObject * -BaseException_get_tb(PyBaseExceptionObject *self, void *Py_UNUSED(ignored)) +BaseException_get_tb(PyObject *op, void *Py_UNUSED(ignored)) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); if (self->traceback == NULL) { Py_RETURN_NONE; } @@ -317,8 +326,9 @@ BaseException_get_tb(PyBaseExceptionObject *self, void *Py_UNUSED(ignored)) } static int -BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb, void *Py_UNUSED(ignored)) +BaseException_set_tb(PyObject *op, PyObject *tb, void *Py_UNUSED(ignored)) { + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); if (tb == NULL) { PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted"); return -1; @@ -398,8 +408,8 @@ BaseException_set_cause(PyObject *self, PyObject *arg, void *Py_UNUSED(ignored)) static PyGetSetDef BaseException_getset[] = { {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, - {"args", (getter)BaseException_get_args, (setter)BaseException_set_args}, - {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb}, + {"args", BaseException_get_args, BaseException_set_args}, + {"__traceback__", BaseException_get_tb, BaseException_set_tb}, {"__context__", BaseException_get_context, BaseException_set_context, PyDoc_STR("exception context")}, {"__cause__", BaseException_get_cause, @@ -411,59 +421,61 @@ static PyGetSetDef BaseException_getset[] = { PyObject * PyException_GetTraceback(PyObject *self) { - PyBaseExceptionObject *base_self = _PyBaseExceptionObject_cast(self); - return Py_XNewRef(base_self->traceback); + PyBaseExceptionObject *exc = _PyBaseExceptionObject_CAST(self); + return Py_XNewRef(exc->traceback); } int PyException_SetTraceback(PyObject *self, PyObject *tb) { - return BaseException_set_tb(_PyBaseExceptionObject_cast(self), tb, NULL); + return BaseException_set_tb(self, tb, NULL); } PyObject * PyException_GetCause(PyObject *self) { - PyObject *cause = _PyBaseExceptionObject_cast(self)->cause; - return Py_XNewRef(cause); + PyBaseExceptionObject *exc = _PyBaseExceptionObject_CAST(self); + return Py_XNewRef(exc->cause); } /* Steals a reference to cause */ void PyException_SetCause(PyObject *self, PyObject *cause) { - PyBaseExceptionObject *base_self = _PyBaseExceptionObject_cast(self); - base_self->suppress_context = 1; - Py_XSETREF(base_self->cause, cause); + PyBaseExceptionObject *exc = _PyBaseExceptionObject_CAST(self); + exc->suppress_context = 1; + Py_XSETREF(exc->cause, cause); } PyObject * PyException_GetContext(PyObject *self) { - PyObject *context = _PyBaseExceptionObject_cast(self)->context; - return Py_XNewRef(context); + PyBaseExceptionObject *exc = _PyBaseExceptionObject_CAST(self); + return Py_XNewRef(exc->context); } /* Steals a reference to context */ void PyException_SetContext(PyObject *self, PyObject *context) { - Py_XSETREF(_PyBaseExceptionObject_cast(self)->context, context); + PyBaseExceptionObject *exc = _PyBaseExceptionObject_CAST(self); + Py_XSETREF(exc->context, context); } PyObject * PyException_GetArgs(PyObject *self) { - PyObject *args = _PyBaseExceptionObject_cast(self)->args; - return Py_NewRef(args); + PyBaseExceptionObject *exc = _PyBaseExceptionObject_CAST(self); + return Py_NewRef(exc->args); } void PyException_SetArgs(PyObject *self, PyObject *args) { Py_INCREF(args); - Py_XSETREF(_PyBaseExceptionObject_cast(self)->args, args); + PyBaseExceptionObject *exc = _PyBaseExceptionObject_CAST(self); + Py_XSETREF(exc->args, args); } const char * @@ -485,26 +497,26 @@ static PyTypeObject _PyExc_BaseException = { "BaseException", /*tp_name*/ sizeof(PyBaseExceptionObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ - (destructor)BaseException_dealloc, /*tp_dealloc*/ + BaseException_dealloc, /*tp_dealloc*/ 0, /*tp_vectorcall_offset*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_as_async*/ - (reprfunc)BaseException_repr, /*tp_repr*/ + BaseException_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ - (reprfunc)BaseException_str, /*tp_str*/ + BaseException_str, /*tp_str*/ PyObject_GenericGetAttr, /*tp_getattro*/ PyObject_GenericSetAttr, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/ PyDoc_STR("Common base class for all exceptions"), /* tp_doc */ - (traverseproc)BaseException_traverse, /* tp_traverse */ - (inquiry)BaseException_clear, /* tp_clear */ + BaseException_traverse, /* tp_traverse */ + BaseException_clear, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ @@ -517,7 +529,7 @@ static PyTypeObject _PyExc_BaseException = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */ - (initproc)BaseException_init, /* tp_init */ + BaseException_init, /* tp_init */ 0, /* tp_alloc */ BaseException_new, /* tp_new */ .tp_vectorcall = BaseException_vectorcall, @@ -535,13 +547,13 @@ static PyTypeObject _PyExc_ ## EXCNAME = { \ PyVarObject_HEAD_INIT(NULL, 0) \ # EXCNAME, \ sizeof(PyBaseExceptionObject), \ - 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \ + 0, BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \ 0, 0, 0, 0, 0, 0, 0, \ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ - PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \ - (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ + PyDoc_STR(EXCDOC), BaseException_traverse, \ + BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \ - (initproc)BaseException_init, 0, BaseException_new,\ + BaseException_init, 0, BaseException_new,\ }; \ PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME @@ -1378,7 +1390,7 @@ _PyExc_PrepReraiseStar(PyObject *orig, PyObject *excs) { /* orig must be a raised & caught exception, so it has a traceback */ assert(PyExceptionInstance_Check(orig)); - assert(_PyBaseExceptionObject_cast(orig)->traceback != NULL); + assert(_PyBaseExceptionObject_CAST(orig)->traceback != NULL); assert(PyList_Check(excs)); From 3f1196b125d07197d378ea04f567db46883d0bf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:02:49 +0100 Subject: [PATCH 02/13] fix UBSan failures for `PyStopIterationObject` --- Objects/exceptions.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index b04567edccb503..a04aaf2ad27389 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -626,14 +626,22 @@ static PyMemberDef StopIteration_members[] = { {NULL} /* Sentinel */ }; +static inline PyStopIterationObject * +_PyStopIteration_CAST(PyObject *self) +{ + assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_StopIteration)); + return (PyStopIterationObject *)self; +} + static int -StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds) +StopIteration_init(PyObject *op, PyObject *args, PyObject *kwds) { Py_ssize_t size = PyTuple_GET_SIZE(args); PyObject *value; - if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) + if (BaseException_init(op, args, kwds) == -1) return -1; + PyStopIterationObject *self = _PyStopIteration_CAST(op); Py_CLEAR(self->value); if (size > 0) value = PyTuple_GET_ITEM(args, 0); @@ -644,25 +652,27 @@ StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds) } static int -StopIteration_clear(PyStopIterationObject *self) +StopIteration_clear(PyObject *op) { + PyStopIterationObject *self = _PyStopIteration_CAST(op); Py_CLEAR(self->value); - return BaseException_clear((PyBaseExceptionObject *)self); + return BaseException_clear(op); } static void -StopIteration_dealloc(PyStopIterationObject *self) +StopIteration_dealloc(PyObject *self) { PyObject_GC_UnTrack(self); - StopIteration_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + (void)StopIteration_clear(self); + Py_TYPE(self)->tp_free(self); } static int -StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg) +StopIteration_traverse(PyObject *op, visitproc visit, void *arg) { + PyStopIterationObject *self = _PyStopIteration_CAST(op); Py_VISIT(self->value); - return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); + return BaseException_traverse(op, visit, arg); } ComplexExtendsException(PyExc_Exception, StopIteration, StopIteration, From 1886b073ec594a9c3a209c26c44967beff3a1128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:08:58 +0100 Subject: [PATCH 03/13] fix UBSan failures for `PySystemExitObject` --- Objects/exceptions.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index a04aaf2ad27389..b8d15a216e753b 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -691,14 +691,22 @@ SimpleExtendsException(PyExc_BaseException, GeneratorExit, * SystemExit extends BaseException */ +static inline PySystemExitObject * +_PySystemExit_CAST(PyObject *self) +{ + assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_SystemExit)); + return (PySystemExitObject *)self; +} + static int -SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds) +SystemExit_init(PyObject *op, PyObject *args, PyObject *kwds) { Py_ssize_t size = PyTuple_GET_SIZE(args); - if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) + if (BaseException_init(op, args, kwds) == -1) return -1; + PySystemExitObject *self = _PySystemExit_CAST(op); if (size == 0) return 0; if (size == 1) { @@ -711,25 +719,27 @@ SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds) } static int -SystemExit_clear(PySystemExitObject *self) +SystemExit_clear(PyObject *op) { + PySystemExitObject *self = _PySystemExit_CAST(op); Py_CLEAR(self->code); - return BaseException_clear((PyBaseExceptionObject *)self); + return BaseException_clear(op); } static void -SystemExit_dealloc(PySystemExitObject *self) +SystemExit_dealloc(PyObject *self) { _PyObject_GC_UNTRACK(self); - SystemExit_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + (void)SystemExit_clear(self); + Py_TYPE(self)->tp_free(self); } static int -SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg) +SystemExit_traverse(PyObject *op, visitproc visit, void *arg) { + PySystemExitObject *self = _PySystemExit_CAST(op); Py_VISIT(self->code); - return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); + return BaseException_traverse(op, visit, arg); } static PyMemberDef SystemExit_members[] = { From 3df93a5f2f4b12e76c651f536945c8a578915f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:13:07 +0100 Subject: [PATCH 04/13] fix UBSan failures for `PyImportErrorObject` --- Objects/exceptions.c | 51 ++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index b8d15a216e753b..f8e71532a8d166 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1589,8 +1589,15 @@ SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt, * ImportError extends Exception */ +static inline PyImportErrorObject * +_PyImportError_CAST(PyObject *self) +{ + assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_ImportError)); + return (PyImportErrorObject *)self; +} + static int -ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds) +ImportError_init(PyObject *op, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"name", "path", "name_from", 0}; PyObject *empty_tuple; @@ -1599,9 +1606,10 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds) PyObject *path = NULL; PyObject *name_from = NULL; - if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) + if (BaseException_init(op, args, NULL) == -1) return -1; + PyImportErrorObject *self = _PyImportError_CAST(op); empty_tuple = PyTuple_New(0); if (!empty_tuple) return -1; @@ -1625,48 +1633,50 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds) } static int -ImportError_clear(PyImportErrorObject *self) +ImportError_clear(PyObject *op) { + PyImportErrorObject *self = _PyImportError_CAST(op); Py_CLEAR(self->msg); Py_CLEAR(self->name); Py_CLEAR(self->path); Py_CLEAR(self->name_from); - return BaseException_clear((PyBaseExceptionObject *)self); + return BaseException_clear(op); } static void -ImportError_dealloc(PyImportErrorObject *self) +ImportError_dealloc(PyObject *self) { _PyObject_GC_UNTRACK(self); - ImportError_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + (void)ImportError_clear(self); + Py_TYPE(self)->tp_free(self); } static int -ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg) +ImportError_traverse(PyObject *op, visitproc visit, void *arg) { + PyImportErrorObject *self = _PyImportError_CAST(op); Py_VISIT(self->msg); Py_VISIT(self->name); Py_VISIT(self->path); Py_VISIT(self->name_from); - return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); + return BaseException_traverse(op, visit, arg); } static PyObject * -ImportError_str(PyImportErrorObject *self) +ImportError_str(PyObject *op) { + PyImportErrorObject *self = _PyImportError_CAST(op); if (self->msg && PyUnicode_CheckExact(self->msg)) { return Py_NewRef(self->msg); } - else { - return BaseException_str((PyBaseExceptionObject *)self); - } + return BaseException_str(op); } static PyObject * -ImportError_getstate(PyImportErrorObject *self) +ImportError_getstate(PyObject *op) { - PyObject *dict = ((PyBaseExceptionObject *)self)->dict; + PyImportErrorObject *self = _PyImportError_CAST(op); + PyObject *dict = self->dict; if (self->name || self->path || self->name_from) { dict = dict ? PyDict_Copy(dict) : PyDict_New(); if (dict == NULL) @@ -1695,18 +1705,17 @@ ImportError_getstate(PyImportErrorObject *self) /* Pickling support */ static PyObject * -ImportError_reduce(PyImportErrorObject *self, PyObject *Py_UNUSED(ignored)) +ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(igno+red)) { PyObject *res; - PyObject *args; PyObject *state = ImportError_getstate(self); if (state == NULL) return NULL; - args = ((PyBaseExceptionObject *)self)->args; + PyBaseExceptionObject *exc = _PyBaseExceptionObject_CAST(self); if (state == Py_None) - res = PyTuple_Pack(2, Py_TYPE(self), args); + res = PyTuple_Pack(2, Py_TYPE(self), exc->args); else - res = PyTuple_Pack(3, Py_TYPE(self), args, state); + res = PyTuple_Pack(3, Py_TYPE(self), exc->args, state); Py_DECREF(state); return res; } @@ -1724,7 +1733,7 @@ static PyMemberDef ImportError_members[] = { }; static PyMethodDef ImportError_methods[] = { - {"__reduce__", (PyCFunction)ImportError_reduce, METH_NOARGS}, + {"__reduce__", ImportError_reduce, METH_NOARGS}, {NULL} }; From 96f97a5a9a53bf493dd547bfd1366b5981f821f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:17:08 +0100 Subject: [PATCH 05/13] fix UBSan failures for `PyOSErrorObject` --- Objects/exceptions.c | 51 +++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index f8e71532a8d166..a2bc128b36e797 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1705,7 +1705,7 @@ ImportError_getstate(PyObject *op) /* Pickling support */ static PyObject * -ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(igno+red)) +ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *res; PyObject *state = ImportError_getstate(self); @@ -1755,6 +1755,13 @@ MiddlingExtendsException(PyExc_ImportError, ModuleNotFoundError, ImportError, * OSError extends Exception */ +static inline PyOSErrorObject * +_PyOSError_CAST(PyObject *self) +{ + assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_OSError)); + return (PyOSErrorObject *)self; +} + #ifdef MS_WINDOWS #include "errmap.h" #endif @@ -1891,7 +1898,7 @@ oserror_init(PyOSErrorObject *self, PyObject **p_args, static PyObject * OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds); static int -OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds); +OSError_init(PyObject *self, PyObject *args, PyObject *kwds); static int oserror_use_init(PyTypeObject *type) @@ -1983,8 +1990,9 @@ OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } static int -OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds) +OSError_init(PyObject *op, PyObject *args, PyObject *kwds) { + PyOSErrorObject *self = _PyOSError_CAST(op); PyObject *myerrno = NULL, *strerror = NULL; PyObject *filename = NULL, *filename2 = NULL; #ifdef MS_WINDOWS @@ -2021,8 +2029,9 @@ OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds) } static int -OSError_clear(PyOSErrorObject *self) +OSError_clear(PyObject *op) { + PyOSErrorObject *self = _PyOSError_CAST(op); Py_CLEAR(self->myerrno); Py_CLEAR(self->strerror); Py_CLEAR(self->filename); @@ -2030,21 +2039,21 @@ OSError_clear(PyOSErrorObject *self) #ifdef MS_WINDOWS Py_CLEAR(self->winerror); #endif - return BaseException_clear((PyBaseExceptionObject *)self); + return BaseException_clear(op); } static void -OSError_dealloc(PyOSErrorObject *self) +OSError_dealloc(PyObject *self) { _PyObject_GC_UNTRACK(self); - OSError_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + (void)OSError_clear(self); + Py_TYPE(self)->tp_free(self); } static int -OSError_traverse(PyOSErrorObject *self, visitproc visit, - void *arg) +OSError_traverse(PyObject *op, visitproc visit, void *arg) { + PyOSErrorObject *self = _PyOSError_CAST(op); Py_VISIT(self->myerrno); Py_VISIT(self->strerror); Py_VISIT(self->filename); @@ -2052,12 +2061,13 @@ OSError_traverse(PyOSErrorObject *self, visitproc visit, #ifdef MS_WINDOWS Py_VISIT(self->winerror); #endif - return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); + return BaseException_traverse(op, visit, arg); } static PyObject * -OSError_str(PyOSErrorObject *self) +OSError_str(PyObject *op) { + PyOSErrorObject *self = _PyOSError_CAST(op); #define OR_NONE(x) ((x)?(x):Py_None) #ifdef MS_WINDOWS /* If available, winerror has the priority over myerrno */ @@ -2097,12 +2107,13 @@ OSError_str(PyOSErrorObject *self) if (self->myerrno && self->strerror) return PyUnicode_FromFormat("[Errno %S] %S", self->myerrno, self->strerror); - return BaseException_str((PyBaseExceptionObject *)self); + return BaseException_str(op); } static PyObject * -OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored)) +OSError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) { + PyOSErrorObject *self = _PyOSError_CAST(op); PyObject *args = self->args; PyObject *res = NULL; @@ -2141,8 +2152,9 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored)) } static PyObject * -OSError_written_get(PyOSErrorObject *self, void *context) +OSError_written_get(PyObject *op, void *context) { + PyOSErrorObject *self = _PyOSError_CAST(op); if (self->written == -1) { PyErr_SetString(PyExc_AttributeError, "characters_written"); return NULL; @@ -2151,8 +2163,9 @@ OSError_written_get(PyOSErrorObject *self, void *context) } static int -OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context) +OSError_written_set(PyObject *op, PyObject *arg, void *context) { + PyOSErrorObject *self = _PyOSError_CAST(op); if (arg == NULL) { if (self->written == -1) { PyErr_SetString(PyExc_AttributeError, "characters_written"); @@ -2186,13 +2199,13 @@ static PyMemberDef OSError_members[] = { }; static PyMethodDef OSError_methods[] = { - {"__reduce__", (PyCFunction)OSError_reduce, METH_NOARGS}, + {"__reduce__", OSError_reduce, METH_NOARGS}, {NULL} }; static PyGetSetDef OSError_getset[] = { - {"characters_written", (getter) OSError_written_get, - (setter) OSError_written_set, NULL}, + {"characters_written", OSError_written_get, + OSError_written_set, NULL}, {NULL} }; From 09514a93648bdcbb6e7e7cc30d0a4ae63014c908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:18:39 +0100 Subject: [PATCH 06/13] fix UBSan failures for `PyNameErrorObject` --- Objects/exceptions.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index a2bc128b36e797..6b5664e94b1d79 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -2284,13 +2284,20 @@ SimpleExtendsException(PyExc_RuntimeError, NotImplementedError, * NameError extends Exception */ +static inline PyNameErrorObject * +_PyNameError_CAST(PyObject *self) +{ + assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_NameError)); + return (PyNameErrorObject *)self; +} + static int -NameError_init(PyNameErrorObject *self, PyObject *args, PyObject *kwds) +NameError_init(PyObject *op, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"name", NULL}; PyObject *name = NULL; - if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) { + if (BaseException_init(op, args, NULL) == -1) { return -1; } @@ -2305,31 +2312,34 @@ NameError_init(PyNameErrorObject *self, PyObject *args, PyObject *kwds) } Py_DECREF(empty_tuple); + PyNameErrorObject *self = _PyNameError_CAST(op); Py_XSETREF(self->name, Py_XNewRef(name)); return 0; } static int -NameError_clear(PyNameErrorObject *self) +NameError_clear(PyObject *op) { + PyNameErrorObject *self = _PyNameError_CAST(op); Py_CLEAR(self->name); - return BaseException_clear((PyBaseExceptionObject *)self); + return BaseException_clear(op); } static void -NameError_dealloc(PyNameErrorObject *self) +NameError_dealloc(PyObject *self) { _PyObject_GC_UNTRACK(self); - NameError_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + (void)NameError_clear(self); + Py_TYPE(self)->tp_free(self); } static int -NameError_traverse(PyNameErrorObject *self, visitproc visit, void *arg) +NameError_traverse(PyObject *op, visitproc visit, void *arg) { + PyNameErrorObject *self = _PyNameError_CAST(op); Py_VISIT(self->name); - return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); + return BaseException_traverse(op, visit, arg); } static PyMemberDef NameError_members[] = { From 9d3c8b81a0908e280c1abeb1aef91bee99fc1186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:20:59 +0100 Subject: [PATCH 07/13] fix UBSan failures for `PyAttributeErrorObject` --- Objects/exceptions.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 6b5664e94b1d79..fa4cca7bdfaffe 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -2367,14 +2367,21 @@ MiddlingExtendsException(PyExc_NameError, UnboundLocalError, NameError, * AttributeError extends Exception */ +static inline PyAttributeErrorObject * +_PyAttributeError_CAST(PyObject *self) +{ + assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_AttributeError)); + return (PyAttributeErrorObject *)self; +} + static int -AttributeError_init(PyAttributeErrorObject *self, PyObject *args, PyObject *kwds) +AttributeError_init(PyObject *op, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"name", "obj", NULL}; PyObject *name = NULL; PyObject *obj = NULL; - if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) { + if (BaseException_init(op, args, NULL) == -1) { return -1; } @@ -2389,6 +2396,7 @@ AttributeError_init(PyAttributeErrorObject *self, PyObject *args, PyObject *kwds } Py_DECREF(empty_tuple); + PyAttributeErrorObject *self = _PyAttributeError_CAST(op); Py_XSETREF(self->name, Py_XNewRef(name)); Py_XSETREF(self->obj, Py_XNewRef(obj)); @@ -2396,34 +2404,37 @@ AttributeError_init(PyAttributeErrorObject *self, PyObject *args, PyObject *kwds } static int -AttributeError_clear(PyAttributeErrorObject *self) +AttributeError_clear(PyObject *op) { + PyAttributeErrorObject *self = _PyAttributeError_CAST(op); Py_CLEAR(self->obj); Py_CLEAR(self->name); - return BaseException_clear((PyBaseExceptionObject *)self); + return BaseException_clear(op); } static void -AttributeError_dealloc(PyAttributeErrorObject *self) +AttributeError_dealloc(PyObject *self) { _PyObject_GC_UNTRACK(self); - AttributeError_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + (void)AttributeError_clear(self); + Py_TYPE(self)->tp_free(self); } static int -AttributeError_traverse(PyAttributeErrorObject *self, visitproc visit, void *arg) +AttributeError_traverse(PyObject *op, visitproc visit, void *arg) { + PyAttributeErrorObject *self = _PyAttributeError_CAST(op); Py_VISIT(self->obj); Py_VISIT(self->name); - return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); + return BaseException_traverse(op, visit, arg); } /* Pickling support */ static PyObject * -AttributeError_getstate(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignored)) +AttributeError_getstate(PyObject *op, PyObject *Py_UNUSED(ignored)) { - PyObject *dict = ((PyAttributeErrorObject *)self)->dict; + PyAttributeErrorObject *self = _PyAttributeError_CAST(op); + PyObject *dict = self->dict; if (self->name || self->args) { dict = dict ? PyDict_Copy(dict) : PyDict_New(); if (dict == NULL) { @@ -2449,13 +2460,14 @@ AttributeError_getstate(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignore } static PyObject * -AttributeError_reduce(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignored)) +AttributeError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) { - PyObject *state = AttributeError_getstate(self, NULL); + PyObject *state = AttributeError_getstate(op, NULL); if (state == NULL) { return NULL; } + PyAttributeErrorObject *self = _PyAttributeError_CAST(op); PyObject *return_value = PyTuple_Pack(3, Py_TYPE(self), self->args, state); Py_DECREF(state); return return_value; @@ -2468,8 +2480,8 @@ static PyMemberDef AttributeError_members[] = { }; static PyMethodDef AttributeError_methods[] = { - {"__getstate__", (PyCFunction)AttributeError_getstate, METH_NOARGS}, - {"__reduce__", (PyCFunction)AttributeError_reduce, METH_NOARGS }, + {"__getstate__", AttributeError_getstate, METH_NOARGS}, + {"__reduce__", AttributeError_reduce, METH_NOARGS }, {NULL} }; From 2640a348058c82c11a63c91c091abf4dc93a5aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:25:52 +0100 Subject: [PATCH 08/13] fix UBSan failures for `PySyntaxErrorObject` --- Objects/exceptions.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index fa4cca7bdfaffe..d5e979d9ac22f2 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -2494,15 +2494,23 @@ ComplexExtendsException(PyExc_Exception, AttributeError, * SyntaxError extends Exception */ +static inline PySyntaxErrorObject * +_PySyntaxError_CAST(PyObject *self) +{ + assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_SyntaxError)); + return (PySyntaxErrorObject *)self; +} + static int -SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds) +SyntaxError_init(PyObject *op, PyObject *args, PyObject *kwds) { PyObject *info = NULL; Py_ssize_t lenargs = PyTuple_GET_SIZE(args); - if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) + if (BaseException_init(op, args, kwds) == -1) return -1; + PySyntaxErrorObject *self = _PySyntaxError_CAST(op); if (lenargs >= 1) { Py_XSETREF(self->msg, Py_NewRef(PyTuple_GET_ITEM(args, 0))); } @@ -2540,8 +2548,9 @@ SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds) } static int -SyntaxError_clear(PySyntaxErrorObject *self) +SyntaxError_clear(PyObject *op) { + PySyntaxErrorObject *self = _PySyntaxError_CAST(op); Py_CLEAR(self->msg); Py_CLEAR(self->filename); Py_CLEAR(self->lineno); @@ -2550,20 +2559,21 @@ SyntaxError_clear(PySyntaxErrorObject *self) Py_CLEAR(self->end_offset); Py_CLEAR(self->text); Py_CLEAR(self->print_file_and_line); - return BaseException_clear((PyBaseExceptionObject *)self); + return BaseException_clear(op); } static void -SyntaxError_dealloc(PySyntaxErrorObject *self) +SyntaxError_dealloc(PyObject *self) { _PyObject_GC_UNTRACK(self); - SyntaxError_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + (void)SyntaxError_clear(self); + Py_TYPE(self)->tp_free(self); } static int -SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg) +SyntaxError_traverse(PyObject *op, visitproc visit, void *arg) { + PySyntaxErrorObject *self = _PySyntaxError_CAST(op); Py_VISIT(self->msg); Py_VISIT(self->filename); Py_VISIT(self->lineno); @@ -2572,7 +2582,7 @@ SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg) Py_VISIT(self->end_offset); Py_VISIT(self->text); Py_VISIT(self->print_file_and_line); - return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); + return BaseException_traverse(op, visit, arg); } /* This is called "my_basename" instead of just "basename" to avoid name @@ -2604,8 +2614,9 @@ my_basename(PyObject *name) static PyObject * -SyntaxError_str(PySyntaxErrorObject *self) +SyntaxError_str(PyObject *op) { + PySyntaxErrorObject *self = _PySyntaxError_CAST(op); int have_lineno = 0; PyObject *filename; PyObject *result; From 150f34ef45a1b6f2b4fc0ef938e07dfef449ea41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:27:30 +0100 Subject: [PATCH 09/13] fix UBSan failures for `KeyError` --- Objects/exceptions.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index d5e979d9ac22f2..d09bcceff44459 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -2719,8 +2719,9 @@ SimpleExtendsException(PyExc_LookupError, IndexError, /* * KeyError extends LookupError */ + static PyObject * -KeyError_str(PyBaseExceptionObject *self) +KeyError_str(PyObject *op) { /* If args is a tuple of exactly one item, apply repr to args[0]. This is done so that e.g. the exception raised by {}[''] prints @@ -2731,10 +2732,11 @@ KeyError_str(PyBaseExceptionObject *self) string, that string will be displayed in quotes. Too bad. If args is anything else, use the default BaseException__str__(). */ + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); if (PyTuple_GET_SIZE(self->args) == 1) { return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0)); } - return BaseException_str(self); + return BaseException_str(op); } ComplexExtendsException(PyExc_LookupError, KeyError, BaseException, From 11d9e31800d6b15f8ca74a9683ae615f0977b04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:28:35 +0100 Subject: [PATCH 10/13] remove un-necessary casts for `UnicodeError*` This also align the naming convention `_Py[...]_CAST(op)` where only an assert-only type check is performed. --- Objects/exceptions.c | 63 ++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index d09bcceff44459..ba7850b5c79f3d 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -2837,11 +2837,10 @@ unicode_error_adjust_end(Py_ssize_t end, Py_ssize_t objlen) return end; } -#define _PyUnicodeError_CAST(PTR) ((PyUnicodeErrorObject *)(PTR)) #define PyUnicodeError_Check(PTR) \ PyObject_TypeCheck((PTR), (PyTypeObject *)PyExc_UnicodeError) -#define PyUnicodeError_CAST(PTR) \ - (assert(PyUnicodeError_Check(PTR)), _PyUnicodeError_CAST(PTR)) +#define _PyUnicodeError_CAST(op) \ + (assert(PyUnicodeError_Check(op)), ((PyUnicodeErrorObject *)(op))) static inline int @@ -2860,7 +2859,7 @@ static inline PyUnicodeErrorObject * as_unicode_error(PyObject *self, const char *expect_type) { int rc = check_unicode_error_type(self, expect_type); - return rc < 0 ? NULL : _PyUnicodeError_CAST(self); + return rc < 0 ? NULL : (PyUnicodeErrorObject *)self; } PyObject * @@ -3123,11 +3122,11 @@ PyUnicodeTranslateError_SetReason(PyObject *self, const char *reason) static int UnicodeError_clear(PyObject *self) { - PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); Py_CLEAR(exc->encoding); Py_CLEAR(exc->object); Py_CLEAR(exc->reason); - return BaseException_clear((PyBaseExceptionObject *)self); + return BaseException_clear(self); } static void @@ -3142,11 +3141,11 @@ UnicodeError_dealloc(PyObject *self) static int UnicodeError_traverse(PyObject *self, visitproc visit, void *arg) { - PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); Py_VISIT(exc->encoding); Py_VISIT(exc->object); Py_VISIT(exc->reason); - return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); + return BaseException_traverse(self, visit, arg); } static PyMemberDef UnicodeError_members[] = { @@ -3171,7 +3170,7 @@ static PyMemberDef UnicodeError_members[] = { static int UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds) { - if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) { + if (BaseException_init(self, args, kwds) == -1) { return -1; } @@ -3184,7 +3183,7 @@ UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds) return -1; } - PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); Py_XSETREF(exc->encoding, Py_NewRef(encoding)); Py_XSETREF(exc->object, Py_NewRef(object)); exc->start = start; @@ -3196,7 +3195,7 @@ UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds) static PyObject * UnicodeEncodeError_str(PyObject *self) { - PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); PyObject *result = NULL; PyObject *reason_str = NULL; PyObject *encoding_str = NULL; @@ -3257,13 +3256,13 @@ static PyTypeObject _PyExc_UnicodeEncodeError = { PyVarObject_HEAD_INIT(NULL, 0) "UnicodeEncodeError", sizeof(PyUnicodeErrorObject), 0, - (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - (reprfunc)UnicodeEncodeError_str, 0, 0, 0, + UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + UnicodeEncodeError_str, 0, 0, 0, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, - PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse, - (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, + PyDoc_STR("Unicode encoding error."), UnicodeError_traverse, + UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), - (initproc)UnicodeEncodeError_init, 0, BaseException_new, + UnicodeEncodeError_init, 0, BaseException_new, }; PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError; @@ -3275,7 +3274,7 @@ PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError; static int UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) { - if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) { + if (BaseException_init(self, args, kwds) == -1) { return -1; } @@ -3304,7 +3303,7 @@ UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) } } - PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); Py_XSETREF(exc->encoding, Py_NewRef(encoding)); Py_XSETREF(exc->object, object /* already a strong reference */); exc->start = start; @@ -3316,7 +3315,7 @@ UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) static PyObject * UnicodeDecodeError_str(PyObject *self) { - PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); PyObject *result = NULL; PyObject *reason_str = NULL; PyObject *encoding_str = NULL; @@ -3367,13 +3366,13 @@ static PyTypeObject _PyExc_UnicodeDecodeError = { PyVarObject_HEAD_INIT(NULL, 0) "UnicodeDecodeError", sizeof(PyUnicodeErrorObject), 0, - (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - (reprfunc)UnicodeDecodeError_str, 0, 0, 0, + UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + UnicodeDecodeError_str, 0, 0, 0, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, - PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse, - (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, + PyDoc_STR("Unicode decoding error."), UnicodeError_traverse, + UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), - (initproc)UnicodeDecodeError_init, 0, BaseException_new, + UnicodeDecodeError_init, 0, BaseException_new, }; PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError; @@ -3394,7 +3393,7 @@ PyUnicodeDecodeError_Create( static int UnicodeTranslateError_init(PyObject *self, PyObject *args, PyObject *kwds) { - if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) { + if (BaseException_init(self, args, kwds) == -1) { return -1; } @@ -3405,7 +3404,7 @@ UnicodeTranslateError_init(PyObject *self, PyObject *args, PyObject *kwds) return -1; } - PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); Py_XSETREF(exc->object, Py_NewRef(object)); exc->start = start; exc->end = end; @@ -3417,7 +3416,7 @@ UnicodeTranslateError_init(PyObject *self, PyObject *args, PyObject *kwds) static PyObject * UnicodeTranslateError_str(PyObject *self) { - PyUnicodeErrorObject *exc = PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); PyObject *result = NULL; PyObject *reason_str = NULL; @@ -3470,13 +3469,13 @@ static PyTypeObject _PyExc_UnicodeTranslateError = { PyVarObject_HEAD_INIT(NULL, 0) "UnicodeTranslateError", sizeof(PyUnicodeErrorObject), 0, - (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - (reprfunc)UnicodeTranslateError_str, 0, 0, 0, + UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + UnicodeTranslateError_str, 0, 0, 0, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, - PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse, - (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, + PyDoc_STR("Unicode translation error."), UnicodeError_traverse, + UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), - (initproc)UnicodeTranslateError_init, 0, BaseException_new, + UnicodeTranslateError_init, 0, BaseException_new, }; PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError; From 96430d961a63c4d4038302b0d88137951623c714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:29:48 +0100 Subject: [PATCH 11/13] remove un-necessary casts for `MemoryError` --- Objects/exceptions.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index ba7850b5c79f3d..c54485b6bd1da3 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -3606,23 +3606,23 @@ _PyErr_NoMemory(PyThreadState *tstate) } static void -MemoryError_dealloc(PyObject *obj) +MemoryError_dealloc(PyObject *op) { - PyBaseExceptionObject *self = (PyBaseExceptionObject *)obj; + PyBaseExceptionObject *self = _PyBaseExceptionObject_CAST(op); _PyObject_GC_UNTRACK(self); - BaseException_clear(self); + (void)BaseException_clear(op); /* If this is a subclass of MemoryError, we don't need to * do anything in the free-list*/ if (!Py_IS_TYPE(self, (PyTypeObject *) PyExc_MemoryError)) { - Py_TYPE(self)->tp_free((PyObject *)self); + Py_TYPE(self)->tp_free(op); return; } struct _Py_exc_state *state = get_exc_state(); if (state->memerrors_numfree >= MEMERRORS_SAVE) { - Py_TYPE(self)->tp_free((PyObject *)self); + Py_TYPE(self)->tp_free(op); } else { self->dict = (PyObject *) state->memerrors_freelist; @@ -3658,7 +3658,7 @@ free_preallocated_memerrors(struct _Py_exc_state *state) while (state->memerrors_freelist != NULL) { PyObject *self = (PyObject *) state->memerrors_freelist; state->memerrors_freelist = (PyBaseExceptionObject *)state->memerrors_freelist->dict; - Py_TYPE(self)->tp_free((PyObject *)self); + Py_TYPE(self)->tp_free(self); } } @@ -3670,10 +3670,10 @@ PyTypeObject _PyExc_MemoryError = { 0, MemoryError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, - PyDoc_STR("Out of memory."), (traverseproc)BaseException_traverse, - (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception, + PyDoc_STR("Out of memory."), BaseException_traverse, + BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception, 0, 0, 0, offsetof(PyBaseExceptionObject, dict), - (initproc)BaseException_init, 0, MemoryError_new + BaseException_init, 0, MemoryError_new }; PyObject *PyExc_MemoryError = (PyObject *) &_PyExc_MemoryError; From 0437291c052f4799184c13481083e35c7fd99155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:45:36 +0100 Subject: [PATCH 12/13] fix UBSan failures for `PyBaseExceptionGroupObject` --- Objects/exceptions.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index c54485b6bd1da3..5529ca9b876fdf 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -759,7 +759,7 @@ ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit, static inline PyBaseExceptionGroupObject* -_PyBaseExceptionGroupObject_cast(PyObject *exc) +_PyBaseExceptionGroup_CAST(PyObject *exc) { assert(_PyBaseExceptionGroup_Check(exc)); return (PyBaseExceptionGroupObject *)exc; @@ -865,7 +865,7 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds) cls = (PyTypeObject*)PyExc_BaseExceptionGroup; } PyBaseExceptionGroupObject *self = - _PyBaseExceptionGroupObject_cast(BaseException_new(cls, args, kwds)); + _PyBaseExceptionGroup_CAST(BaseException_new(cls, args, kwds)); if (!self) { goto error; } @@ -896,46 +896,47 @@ _PyExc_CreateExceptionGroup(const char *msg_str, PyObject *excs) } static int -BaseExceptionGroup_init(PyBaseExceptionGroupObject *self, - PyObject *args, PyObject *kwds) +BaseExceptionGroup_init(PyObject *self, PyObject *args, PyObject *kwds) { if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) { return -1; } - if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) { + if (BaseException_init(self, args, kwds) == -1) { return -1; } return 0; } static int -BaseExceptionGroup_clear(PyBaseExceptionGroupObject *self) +BaseExceptionGroup_clear(PyObject *op) { + PyBaseExceptionGroupObject *self = _PyBaseExceptionGroup_CAST(op); Py_CLEAR(self->msg); Py_CLEAR(self->excs); - return BaseException_clear((PyBaseExceptionObject *)self); + return BaseException_clear(op); } static void -BaseExceptionGroup_dealloc(PyBaseExceptionGroupObject *self) +BaseExceptionGroup_dealloc(PyObject *self) { _PyObject_GC_UNTRACK(self); - BaseExceptionGroup_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + (void)BaseExceptionGroup_clear(self); + Py_TYPE(self)->tp_free(self); } static int -BaseExceptionGroup_traverse(PyBaseExceptionGroupObject *self, - visitproc visit, void *arg) +BaseExceptionGroup_traverse(PyObject *op, visitproc visit, void *arg) { + PyBaseExceptionGroupObject *self = _PyBaseExceptionGroup_CAST(op); Py_VISIT(self->msg); Py_VISIT(self->excs); - return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); + return BaseException_traverse(op, visit, arg); } static PyObject * -BaseExceptionGroup_str(PyBaseExceptionGroupObject *self) +BaseExceptionGroup_str(PyObject *op) { + PyBaseExceptionGroupObject *self = _PyBaseExceptionGroup_CAST(op); assert(self->msg); assert(PyUnicode_Check(self->msg)); @@ -949,7 +950,7 @@ BaseExceptionGroup_str(PyBaseExceptionGroupObject *self) static PyObject * BaseExceptionGroup_derive(PyObject *self_, PyObject *excs) { - PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_cast(self_); + PyBaseExceptionGroupObject *self = _PyBaseExceptionGroup_CAST(self_); PyObject *init_args = PyTuple_Pack(2, self->msg, excs); if (!init_args) { return NULL; @@ -1162,7 +1163,7 @@ exceptiongroup_split_recursive(PyObject *exc, /* Partial match */ - PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroupObject_cast(exc); + PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroup_CAST(exc); assert(PyTuple_CheckExact(eg->excs)); Py_ssize_t num_excs = PyTuple_Size(eg->excs); if (num_excs < 0) { @@ -1313,7 +1314,7 @@ collect_exception_group_leaf_ids(PyObject *exc, PyObject *leaf_ids) Py_DECREF(exc_id); return res; } - PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroupObject_cast(exc); + PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroup_CAST(exc); Py_ssize_t num_excs = PyTuple_GET_SIZE(eg->excs); /* recursive calls */ for (Py_ssize_t i = 0; i < num_excs; i++) { @@ -1545,9 +1546,9 @@ static PyMemberDef BaseExceptionGroup_members[] = { static PyMethodDef BaseExceptionGroup_methods[] = { {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, - {"derive", (PyCFunction)BaseExceptionGroup_derive, METH_O}, - {"split", (PyCFunction)BaseExceptionGroup_split, METH_O}, - {"subgroup", (PyCFunction)BaseExceptionGroup_subgroup, METH_O}, + {"derive", BaseExceptionGroup_derive, METH_O}, + {"split", BaseExceptionGroup_split, METH_O}, + {"subgroup", BaseExceptionGroup_subgroup, METH_O}, {NULL} }; From d17a9b417aaa6d2f6f38e24428d364011c9df189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:48:42 +0100 Subject: [PATCH 13/13] unify naming for cast functions --- Objects/exceptions.c | 112 +++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 5529ca9b876fdf..9e9f85868edf8b 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -627,7 +627,7 @@ static PyMemberDef StopIteration_members[] = { }; static inline PyStopIterationObject * -_PyStopIteration_CAST(PyObject *self) +_PyStopIterationObject_CAST(PyObject *self) { assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_StopIteration)); return (PyStopIterationObject *)self; @@ -641,7 +641,7 @@ StopIteration_init(PyObject *op, PyObject *args, PyObject *kwds) if (BaseException_init(op, args, kwds) == -1) return -1; - PyStopIterationObject *self = _PyStopIteration_CAST(op); + PyStopIterationObject *self = _PyStopIterationObject_CAST(op); Py_CLEAR(self->value); if (size > 0) value = PyTuple_GET_ITEM(args, 0); @@ -654,7 +654,7 @@ StopIteration_init(PyObject *op, PyObject *args, PyObject *kwds) static int StopIteration_clear(PyObject *op) { - PyStopIterationObject *self = _PyStopIteration_CAST(op); + PyStopIterationObject *self = _PyStopIterationObject_CAST(op); Py_CLEAR(self->value); return BaseException_clear(op); } @@ -670,7 +670,7 @@ StopIteration_dealloc(PyObject *self) static int StopIteration_traverse(PyObject *op, visitproc visit, void *arg) { - PyStopIterationObject *self = _PyStopIteration_CAST(op); + PyStopIterationObject *self = _PyStopIterationObject_CAST(op); Py_VISIT(self->value); return BaseException_traverse(op, visit, arg); } @@ -692,7 +692,7 @@ SimpleExtendsException(PyExc_BaseException, GeneratorExit, */ static inline PySystemExitObject * -_PySystemExit_CAST(PyObject *self) +_PySystemExitObject_CAST(PyObject *self) { assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_SystemExit)); return (PySystemExitObject *)self; @@ -706,7 +706,7 @@ SystemExit_init(PyObject *op, PyObject *args, PyObject *kwds) if (BaseException_init(op, args, kwds) == -1) return -1; - PySystemExitObject *self = _PySystemExit_CAST(op); + PySystemExitObject *self = _PySystemExitObject_CAST(op); if (size == 0) return 0; if (size == 1) { @@ -721,7 +721,7 @@ SystemExit_init(PyObject *op, PyObject *args, PyObject *kwds) static int SystemExit_clear(PyObject *op) { - PySystemExitObject *self = _PySystemExit_CAST(op); + PySystemExitObject *self = _PySystemExitObject_CAST(op); Py_CLEAR(self->code); return BaseException_clear(op); } @@ -737,7 +737,7 @@ SystemExit_dealloc(PyObject *self) static int SystemExit_traverse(PyObject *op, visitproc visit, void *arg) { - PySystemExitObject *self = _PySystemExit_CAST(op); + PySystemExitObject *self = _PySystemExitObject_CAST(op); Py_VISIT(self->code); return BaseException_traverse(op, visit, arg); } @@ -759,7 +759,7 @@ ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit, static inline PyBaseExceptionGroupObject* -_PyBaseExceptionGroup_CAST(PyObject *exc) +_PyBaseExceptionGroupObject_CAST(PyObject *exc) { assert(_PyBaseExceptionGroup_Check(exc)); return (PyBaseExceptionGroupObject *)exc; @@ -865,7 +865,7 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds) cls = (PyTypeObject*)PyExc_BaseExceptionGroup; } PyBaseExceptionGroupObject *self = - _PyBaseExceptionGroup_CAST(BaseException_new(cls, args, kwds)); + _PyBaseExceptionGroupObject_CAST(BaseException_new(cls, args, kwds)); if (!self) { goto error; } @@ -910,7 +910,7 @@ BaseExceptionGroup_init(PyObject *self, PyObject *args, PyObject *kwds) static int BaseExceptionGroup_clear(PyObject *op) { - PyBaseExceptionGroupObject *self = _PyBaseExceptionGroup_CAST(op); + PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_CAST(op); Py_CLEAR(self->msg); Py_CLEAR(self->excs); return BaseException_clear(op); @@ -927,7 +927,7 @@ BaseExceptionGroup_dealloc(PyObject *self) static int BaseExceptionGroup_traverse(PyObject *op, visitproc visit, void *arg) { - PyBaseExceptionGroupObject *self = _PyBaseExceptionGroup_CAST(op); + PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_CAST(op); Py_VISIT(self->msg); Py_VISIT(self->excs); return BaseException_traverse(op, visit, arg); @@ -936,7 +936,7 @@ BaseExceptionGroup_traverse(PyObject *op, visitproc visit, void *arg) static PyObject * BaseExceptionGroup_str(PyObject *op) { - PyBaseExceptionGroupObject *self = _PyBaseExceptionGroup_CAST(op); + PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_CAST(op); assert(self->msg); assert(PyUnicode_Check(self->msg)); @@ -950,7 +950,7 @@ BaseExceptionGroup_str(PyObject *op) static PyObject * BaseExceptionGroup_derive(PyObject *self_, PyObject *excs) { - PyBaseExceptionGroupObject *self = _PyBaseExceptionGroup_CAST(self_); + PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_CAST(self_); PyObject *init_args = PyTuple_Pack(2, self->msg, excs); if (!init_args) { return NULL; @@ -1163,7 +1163,7 @@ exceptiongroup_split_recursive(PyObject *exc, /* Partial match */ - PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroup_CAST(exc); + PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroupObject_CAST(exc); assert(PyTuple_CheckExact(eg->excs)); Py_ssize_t num_excs = PyTuple_Size(eg->excs); if (num_excs < 0) { @@ -1314,7 +1314,7 @@ collect_exception_group_leaf_ids(PyObject *exc, PyObject *leaf_ids) Py_DECREF(exc_id); return res; } - PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroup_CAST(exc); + PyBaseExceptionGroupObject *eg = _PyBaseExceptionGroupObject_CAST(exc); Py_ssize_t num_excs = PyTuple_GET_SIZE(eg->excs); /* recursive calls */ for (Py_ssize_t i = 0; i < num_excs; i++) { @@ -1591,7 +1591,7 @@ SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt, */ static inline PyImportErrorObject * -_PyImportError_CAST(PyObject *self) +_PyImportErrorObject_CAST(PyObject *self) { assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_ImportError)); return (PyImportErrorObject *)self; @@ -1610,7 +1610,7 @@ ImportError_init(PyObject *op, PyObject *args, PyObject *kwds) if (BaseException_init(op, args, NULL) == -1) return -1; - PyImportErrorObject *self = _PyImportError_CAST(op); + PyImportErrorObject *self = _PyImportErrorObject_CAST(op); empty_tuple = PyTuple_New(0); if (!empty_tuple) return -1; @@ -1636,7 +1636,7 @@ ImportError_init(PyObject *op, PyObject *args, PyObject *kwds) static int ImportError_clear(PyObject *op) { - PyImportErrorObject *self = _PyImportError_CAST(op); + PyImportErrorObject *self = _PyImportErrorObject_CAST(op); Py_CLEAR(self->msg); Py_CLEAR(self->name); Py_CLEAR(self->path); @@ -1655,7 +1655,7 @@ ImportError_dealloc(PyObject *self) static int ImportError_traverse(PyObject *op, visitproc visit, void *arg) { - PyImportErrorObject *self = _PyImportError_CAST(op); + PyImportErrorObject *self = _PyImportErrorObject_CAST(op); Py_VISIT(self->msg); Py_VISIT(self->name); Py_VISIT(self->path); @@ -1666,7 +1666,7 @@ ImportError_traverse(PyObject *op, visitproc visit, void *arg) static PyObject * ImportError_str(PyObject *op) { - PyImportErrorObject *self = _PyImportError_CAST(op); + PyImportErrorObject *self = _PyImportErrorObject_CAST(op); if (self->msg && PyUnicode_CheckExact(self->msg)) { return Py_NewRef(self->msg); } @@ -1676,7 +1676,7 @@ ImportError_str(PyObject *op) static PyObject * ImportError_getstate(PyObject *op) { - PyImportErrorObject *self = _PyImportError_CAST(op); + PyImportErrorObject *self = _PyImportErrorObject_CAST(op); PyObject *dict = self->dict; if (self->name || self->path || self->name_from) { dict = dict ? PyDict_Copy(dict) : PyDict_New(); @@ -1757,7 +1757,7 @@ MiddlingExtendsException(PyExc_ImportError, ModuleNotFoundError, ImportError, */ static inline PyOSErrorObject * -_PyOSError_CAST(PyObject *self) +_PyOSErrorObject_CAST(PyObject *self) { assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_OSError)); return (PyOSErrorObject *)self; @@ -1993,7 +1993,7 @@ OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static int OSError_init(PyObject *op, PyObject *args, PyObject *kwds) { - PyOSErrorObject *self = _PyOSError_CAST(op); + PyOSErrorObject *self = _PyOSErrorObject_CAST(op); PyObject *myerrno = NULL, *strerror = NULL; PyObject *filename = NULL, *filename2 = NULL; #ifdef MS_WINDOWS @@ -2032,7 +2032,7 @@ OSError_init(PyObject *op, PyObject *args, PyObject *kwds) static int OSError_clear(PyObject *op) { - PyOSErrorObject *self = _PyOSError_CAST(op); + PyOSErrorObject *self = _PyOSErrorObject_CAST(op); Py_CLEAR(self->myerrno); Py_CLEAR(self->strerror); Py_CLEAR(self->filename); @@ -2054,7 +2054,7 @@ OSError_dealloc(PyObject *self) static int OSError_traverse(PyObject *op, visitproc visit, void *arg) { - PyOSErrorObject *self = _PyOSError_CAST(op); + PyOSErrorObject *self = _PyOSErrorObject_CAST(op); Py_VISIT(self->myerrno); Py_VISIT(self->strerror); Py_VISIT(self->filename); @@ -2068,7 +2068,7 @@ OSError_traverse(PyObject *op, visitproc visit, void *arg) static PyObject * OSError_str(PyObject *op) { - PyOSErrorObject *self = _PyOSError_CAST(op); + PyOSErrorObject *self = _PyOSErrorObject_CAST(op); #define OR_NONE(x) ((x)?(x):Py_None) #ifdef MS_WINDOWS /* If available, winerror has the priority over myerrno */ @@ -2114,7 +2114,7 @@ OSError_str(PyObject *op) static PyObject * OSError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) { - PyOSErrorObject *self = _PyOSError_CAST(op); + PyOSErrorObject *self = _PyOSErrorObject_CAST(op); PyObject *args = self->args; PyObject *res = NULL; @@ -2155,7 +2155,7 @@ OSError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) static PyObject * OSError_written_get(PyObject *op, void *context) { - PyOSErrorObject *self = _PyOSError_CAST(op); + PyOSErrorObject *self = _PyOSErrorObject_CAST(op); if (self->written == -1) { PyErr_SetString(PyExc_AttributeError, "characters_written"); return NULL; @@ -2166,7 +2166,7 @@ OSError_written_get(PyObject *op, void *context) static int OSError_written_set(PyObject *op, PyObject *arg, void *context) { - PyOSErrorObject *self = _PyOSError_CAST(op); + PyOSErrorObject *self = _PyOSErrorObject_CAST(op); if (arg == NULL) { if (self->written == -1) { PyErr_SetString(PyExc_AttributeError, "characters_written"); @@ -2286,7 +2286,7 @@ SimpleExtendsException(PyExc_RuntimeError, NotImplementedError, */ static inline PyNameErrorObject * -_PyNameError_CAST(PyObject *self) +_PyNameErrorObject_CAST(PyObject *self) { assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_NameError)); return (PyNameErrorObject *)self; @@ -2313,7 +2313,7 @@ NameError_init(PyObject *op, PyObject *args, PyObject *kwds) } Py_DECREF(empty_tuple); - PyNameErrorObject *self = _PyNameError_CAST(op); + PyNameErrorObject *self = _PyNameErrorObject_CAST(op); Py_XSETREF(self->name, Py_XNewRef(name)); return 0; @@ -2322,7 +2322,7 @@ NameError_init(PyObject *op, PyObject *args, PyObject *kwds) static int NameError_clear(PyObject *op) { - PyNameErrorObject *self = _PyNameError_CAST(op); + PyNameErrorObject *self = _PyNameErrorObject_CAST(op); Py_CLEAR(self->name); return BaseException_clear(op); } @@ -2338,7 +2338,7 @@ NameError_dealloc(PyObject *self) static int NameError_traverse(PyObject *op, visitproc visit, void *arg) { - PyNameErrorObject *self = _PyNameError_CAST(op); + PyNameErrorObject *self = _PyNameErrorObject_CAST(op); Py_VISIT(self->name); return BaseException_traverse(op, visit, arg); } @@ -2369,7 +2369,7 @@ MiddlingExtendsException(PyExc_NameError, UnboundLocalError, NameError, */ static inline PyAttributeErrorObject * -_PyAttributeError_CAST(PyObject *self) +_PyAttributeErrorObject_CAST(PyObject *self) { assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_AttributeError)); return (PyAttributeErrorObject *)self; @@ -2397,7 +2397,7 @@ AttributeError_init(PyObject *op, PyObject *args, PyObject *kwds) } Py_DECREF(empty_tuple); - PyAttributeErrorObject *self = _PyAttributeError_CAST(op); + PyAttributeErrorObject *self = _PyAttributeErrorObject_CAST(op); Py_XSETREF(self->name, Py_XNewRef(name)); Py_XSETREF(self->obj, Py_XNewRef(obj)); @@ -2407,7 +2407,7 @@ AttributeError_init(PyObject *op, PyObject *args, PyObject *kwds) static int AttributeError_clear(PyObject *op) { - PyAttributeErrorObject *self = _PyAttributeError_CAST(op); + PyAttributeErrorObject *self = _PyAttributeErrorObject_CAST(op); Py_CLEAR(self->obj); Py_CLEAR(self->name); return BaseException_clear(op); @@ -2424,7 +2424,7 @@ AttributeError_dealloc(PyObject *self) static int AttributeError_traverse(PyObject *op, visitproc visit, void *arg) { - PyAttributeErrorObject *self = _PyAttributeError_CAST(op); + PyAttributeErrorObject *self = _PyAttributeErrorObject_CAST(op); Py_VISIT(self->obj); Py_VISIT(self->name); return BaseException_traverse(op, visit, arg); @@ -2434,7 +2434,7 @@ AttributeError_traverse(PyObject *op, visitproc visit, void *arg) static PyObject * AttributeError_getstate(PyObject *op, PyObject *Py_UNUSED(ignored)) { - PyAttributeErrorObject *self = _PyAttributeError_CAST(op); + PyAttributeErrorObject *self = _PyAttributeErrorObject_CAST(op); PyObject *dict = self->dict; if (self->name || self->args) { dict = dict ? PyDict_Copy(dict) : PyDict_New(); @@ -2468,7 +2468,7 @@ AttributeError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) return NULL; } - PyAttributeErrorObject *self = _PyAttributeError_CAST(op); + PyAttributeErrorObject *self = _PyAttributeErrorObject_CAST(op); PyObject *return_value = PyTuple_Pack(3, Py_TYPE(self), self->args, state); Py_DECREF(state); return return_value; @@ -2496,7 +2496,7 @@ ComplexExtendsException(PyExc_Exception, AttributeError, */ static inline PySyntaxErrorObject * -_PySyntaxError_CAST(PyObject *self) +_PySyntaxErrorObject_CAST(PyObject *self) { assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_SyntaxError)); return (PySyntaxErrorObject *)self; @@ -2511,7 +2511,7 @@ SyntaxError_init(PyObject *op, PyObject *args, PyObject *kwds) if (BaseException_init(op, args, kwds) == -1) return -1; - PySyntaxErrorObject *self = _PySyntaxError_CAST(op); + PySyntaxErrorObject *self = _PySyntaxErrorObject_CAST(op); if (lenargs >= 1) { Py_XSETREF(self->msg, Py_NewRef(PyTuple_GET_ITEM(args, 0))); } @@ -2551,7 +2551,7 @@ SyntaxError_init(PyObject *op, PyObject *args, PyObject *kwds) static int SyntaxError_clear(PyObject *op) { - PySyntaxErrorObject *self = _PySyntaxError_CAST(op); + PySyntaxErrorObject *self = _PySyntaxErrorObject_CAST(op); Py_CLEAR(self->msg); Py_CLEAR(self->filename); Py_CLEAR(self->lineno); @@ -2574,7 +2574,7 @@ SyntaxError_dealloc(PyObject *self) static int SyntaxError_traverse(PyObject *op, visitproc visit, void *arg) { - PySyntaxErrorObject *self = _PySyntaxError_CAST(op); + PySyntaxErrorObject *self = _PySyntaxErrorObject_CAST(op); Py_VISIT(self->msg); Py_VISIT(self->filename); Py_VISIT(self->lineno); @@ -2617,7 +2617,7 @@ my_basename(PyObject *name) static PyObject * SyntaxError_str(PyObject *op) { - PySyntaxErrorObject *self = _PySyntaxError_CAST(op); + PySyntaxErrorObject *self = _PySyntaxErrorObject_CAST(op); int have_lineno = 0; PyObject *filename; PyObject *result; @@ -2840,7 +2840,7 @@ unicode_error_adjust_end(Py_ssize_t end, Py_ssize_t objlen) #define PyUnicodeError_Check(PTR) \ PyObject_TypeCheck((PTR), (PyTypeObject *)PyExc_UnicodeError) -#define _PyUnicodeError_CAST(op) \ +#define _PyUnicodeErrorObject_CAST(op) \ (assert(PyUnicodeError_Check(op)), ((PyUnicodeErrorObject *)(op))) @@ -2955,7 +2955,7 @@ PyUnicodeTranslateError_GetStart(PyObject *self, Py_ssize_t *start) static inline int unicode_error_set_start_impl(PyObject *self, Py_ssize_t start) { - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); exc->start = start; return 0; } @@ -3042,7 +3042,7 @@ PyUnicodeTranslateError_GetEnd(PyObject *self, Py_ssize_t *end) static inline int unicode_error_set_end_impl(PyObject *self, Py_ssize_t end) { - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); exc->end = end; return 0; } @@ -3123,7 +3123,7 @@ PyUnicodeTranslateError_SetReason(PyObject *self, const char *reason) static int UnicodeError_clear(PyObject *self) { - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); Py_CLEAR(exc->encoding); Py_CLEAR(exc->object); Py_CLEAR(exc->reason); @@ -3142,7 +3142,7 @@ UnicodeError_dealloc(PyObject *self) static int UnicodeError_traverse(PyObject *self, visitproc visit, void *arg) { - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); Py_VISIT(exc->encoding); Py_VISIT(exc->object); Py_VISIT(exc->reason); @@ -3184,7 +3184,7 @@ UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds) return -1; } - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); Py_XSETREF(exc->encoding, Py_NewRef(encoding)); Py_XSETREF(exc->object, Py_NewRef(object)); exc->start = start; @@ -3196,7 +3196,7 @@ UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds) static PyObject * UnicodeEncodeError_str(PyObject *self) { - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); PyObject *result = NULL; PyObject *reason_str = NULL; PyObject *encoding_str = NULL; @@ -3304,7 +3304,7 @@ UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) } } - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); Py_XSETREF(exc->encoding, Py_NewRef(encoding)); Py_XSETREF(exc->object, object /* already a strong reference */); exc->start = start; @@ -3316,7 +3316,7 @@ UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds) static PyObject * UnicodeDecodeError_str(PyObject *self) { - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); PyObject *result = NULL; PyObject *reason_str = NULL; PyObject *encoding_str = NULL; @@ -3405,7 +3405,7 @@ UnicodeTranslateError_init(PyObject *self, PyObject *args, PyObject *kwds) return -1; } - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); Py_XSETREF(exc->object, Py_NewRef(object)); exc->start = start; exc->end = end; @@ -3417,7 +3417,7 @@ UnicodeTranslateError_init(PyObject *self, PyObject *args, PyObject *kwds) static PyObject * UnicodeTranslateError_str(PyObject *self) { - PyUnicodeErrorObject *exc = _PyUnicodeError_CAST(self); + PyUnicodeErrorObject *exc = _PyUnicodeErrorObject_CAST(self); PyObject *result = NULL; PyObject *reason_str = NULL;