8000 fix UBSan failures for `PyAttributeErrorObject` · python/cpython@9d3c8b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d3c8b8

Browse files
committed
fix UBSan failures for PyAttributeErrorObject
1 parent 09514a9 commit 9d3c8b8

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

Objects/exceptions.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,14 +2367,21 @@ MiddlingExtendsException(PyExc_NameError, UnboundLocalError, NameError,
23672367
* AttributeError extends Exception
23682368
*/
23692369

2370+
static inline PyAttributeErrorObject *
2371+
_PyAttributeError_CAST(PyObject *self)
2372+
{
2373+
assert(PyObject_TypeCheck(self, (PyTypeObject *)PyExc_AttributeError));
2374+
return (PyAttributeErrorObject *)self;
2375+
}
2376+
23702377
static int
2371-
AttributeError_init(PyAttributeErrorObject *self, PyObject *args, PyObject *kwds)
2378+
AttributeError_init(PyObject *op, PyObject *args, PyObject *kwds)
23722379
{
23732380
static char *kwlist[] = {"name", "obj", NULL};
23742381
PyObject *name = NULL;
23752382
PyObject *obj = NULL;
23762383

2377-
if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) {
2384+
if (BaseException_init(op, args, NULL) == -1) {
23782385
return -1;
23792386
}
23802387

@@ -2389,41 +2396,45 @@ AttributeError_init(PyAttributeErrorObject *self, PyObject *args, PyObject *kwds
23892396
}
23902397
Py_DECREF(empty_tuple);
23912398

2399+
PyAttributeErrorObject *self = _PyAttributeError_CAST(op);
23922400
Py_XSETREF(self->name, Py_XNewRef(name));
23932401
Py_XSETREF(self->obj, Py_XNewRef(obj));
23942402

23952403
return 0;
23962404
}
23972405

23982406
static int
2399-
AttributeError_clear(PyAttributeErrorObject *self)
2407+
AttributeError_clear(PyObject *op)
24002408
{
2409+
PyAttributeErrorObject *self = _PyAttributeError_CAST(op);
24012410
Py_CLEAR(self->obj);
24022411
Py_CLEAR(self->name);
2403-
return BaseException_clear((PyBaseExceptionObject *)self);
2412+
return BaseException_clear(op);
24042413
}
24052414

24062415
static void
2407-
AttributeError_dealloc(PyAttributeErrorObject *self)
2416+
AttributeError_dealloc(PyObject *self)
24082417
{
24092418
_PyObject_GC_UNTRACK(self);
2410-
AttributeError_clear(self);
2411-
Py_TYPE(self)->tp_free((PyObject *)self);
2419+
(void)AttributeError_clear(self);
2420+
Py_TYPE(self)->tp_free(self);
24122421
}
24132422

24142423
static int
2415-
AttributeError_traverse(PyAttributeErrorObject *self, visitproc visit, void *arg)
2424+
AttributeError_traverse(PyObject *op, visitproc visit, void *arg)
24162425
{
2426+
PyAttributeErrorObject *self = _PyAttributeError_CAST(op);
24172427
Py_VISIT(self->obj);
24182428
Py_VISIT(self->name);
2419-
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
2429+
return BaseException_traverse(op, visit, arg);
24202430
}
24212431

24222432
/* Pickling support */
24232433
static PyObject *
2424-
AttributeError_getstate(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignored))
2434+
AttributeError_getstate(PyObject *op, PyObject *Py_UNUSED(ignored))
24252435
{
2426-
PyObject *dict = ((PyAttributeErrorObject *)self)->dict;
2436+
PyAttributeErrorObject *self = _PyAttributeError_CAST(op);
2437+
PyObject *dict = self->dict;
24272438
if (self->name || self->args) {
24282439
dict = dict ? PyDict_Copy(dict) : PyDict_New();
24292440
if (dict == NULL) {
@@ -2449,13 +2460,14 @@ AttributeError_getstate(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignore
24492460
}
24502461

24512462
static PyObject *
2452-
AttributeError_reduce(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignored))
2463+
AttributeError_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
24532464
{
2454-
PyObject *state = AttributeError_getstate(self, NULL);
2465+
PyObject *state = AttributeError_getstate(op, NULL);
24552466
if (state == NULL) {
24562467
return NULL;
24572468
}
24582469

2470+
PyAttributeErrorObject *self = _PyAttributeError_CAST(op);
24592471
PyObject *return_value = PyTuple_Pack(3, Py_TYPE(self), self->args, state);
24602472
Py_DECREF(state);
24612473
return return_value;
@@ -2468,8 +2480,8 @@ static PyMemberDef AttributeError_members[] = {
24682480
};
24692481

24702482
static PyMethodDef AttributeError_methods[] = {
2471-
{"__getstate__", (PyCFunction)AttributeError_getstate, METH_NOARGS},
2472-
{"__reduce__", (PyCFunction)AttributeError_reduce, METH_NOARGS },
2483+
{"__getstate__", AttributeError_getstate, METH_NOARGS},
2484+
{"__reduce__", AttributeError_reduce, METH_NOARGS },
24732485
{NULL}
24742486
};
24752487

0 commit comments

Comments
 (0)
0