8000 gh-111178: fix UBSan failures in `Objects/codeobject.c` (GH-128240) · python/cpython@4533036 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4533036

Browse files
authored
gh-111178: fix UBSan failures in Objects/codeobject.c (GH-128240)
1 parent 8dfc743 commit 4533036

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Objects/codeobject.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ PyCode_ClearWatcher(int watcher_id)
108108
* generic helpers
109109
******************/
110110

111+
#define _PyCodeObject_CAST(op) (assert(PyCode_Check(op)), (PyCodeObject *)(op))
112+
111113
static int
112114
should_intern_string(PyObject *o)
113115
{
@@ -1865,11 +1867,12 @@ free_monitoring_data(_PyCoMonitoringData *data)
18651867
}
18661868

18671869
static void
1868-
code_dealloc(PyCodeObject *co)
1870+
code_dealloc(PyObject *self)
18691871
{
1870-
_PyObject_ResurrectStart((PyObject *)co);
1872+
PyCodeObject *co = _PyCodeObject_CAST(self);
1873+
_PyObject_ResurrectStart(self);
18711874
notify_code_watchers(PY_CODE_EVENT_DESTROY, co);
1872-
if (_PyObject_ResurrectEnd((PyObject *)co)) {
1875+
if (_PyObject_ResurrectEnd(self)) {
18731876
return;
18741877
}
18751878

@@ -1918,7 +1921,7 @@ code_dealloc(PyCodeObject *co)
19181921
PyMem_Free(co->_co_cached);
19191922
}
19201923
if (co->co_weakreflist != NULL) {
1921-
PyObject_ClearWeakRefs((PyObject*)co);
1924+
PyObject_ClearWeakRefs(self);
19221925
}
19231926
free_monitoring_data(co->_co_monitoring);
19241927
#ifdef Py_GIL_DISABLED
@@ -1939,7 +1942,7 @@ code_dealloc(PyCodeObject *co)
19391942
static int
19401943
code_traverse(PyObject *self, visitproc visit, void *arg)
19411944
{
1942-
PyCodeObject *co = (PyCodeObject*)self;
1945+
PyCodeObject *co = _PyCodeObject_CAST(self);
19431946
Py_VISIT(co->co_consts);
19441947
return 0;
19451948
}
@@ -1948,7 +1951,7 @@ code_traverse(PyObject *self, visitproc visit, void *arg)
19481951
static PyObject *
19491952
code_repr(PyObject *self)
19501953
{
1951-
PyCodeObject *co = (PyCodeObject*)self;
1954+
PyCodeObject *co = _PyCodeObject_CAST(self);
19521955
int lineno;
19531956
if (co->co_firstlineno != 0)
19541957
lineno = co->co_firstlineno;
@@ -2057,7 +2060,7 @@ code_richcompare(PyObject *self, PyObject *other, int op)
20572060
static Py_hash_t
20582061
code_hash(PyObject *self)
20592062
{
2060-
PyCodeObject *co = (PyCodeObject*)self;
2063+
PyCodeObject *co = _PyCodeObject_CAST(self);
20612064
Py_uhash_t uhash = 20221211;
20622065
#define SCRAMBLE_IN(H) do { \
20632066
uhash ^= (Py_uhash_t)(H); \
@@ -2120,7 +2123,7 @@ static PyMemberDef code_memberlist[] = {
21202123
static PyObject *
21212124
code_getlnotab(PyObject *self, void *closure)
21222125
{
2123-
PyCodeObject *code = (PyCodeObject*)self;
2126+
PyCodeObject *code = _PyCodeObject_CAST(self);
21242127
if (PyErr_WarnEx(PyExc_DeprecationWarning,
21252128
"co_lnotab is deprecated, use co_lines instead.",
21262129
1) < 0) {
@@ -2132,36 +2135,36 @@ code_getlnotab(PyObject *self, void *closure)
21322135
static PyObject *
21332136
code_getvarnames(PyObject *self, void *closure)
21342137
{
2135-
PyCodeObject *code = (PyCodeObject*)self;
2138+
PyCodeObject *code = _PyCodeObject_CAST(self);
21362139
return _PyCode_GetVarnames(code);
21372140
}
21382141

21392142
static PyObject *
21402143
code_getcellvars(PyObject *self, void *closure)
21412144
{
2142-
PyCodeObject *code = (PyCodeObject*)self;
2145+
PyCodeObject *code = _PyCodeObject_CAST(self);
21432146
return _PyCode_GetCellvars(code);
21442147
}
21452148

21462149
static PyObject *
21472150
code_getfreevars(PyObject *self, void *closure)
21482151
{
2149-
PyCodeObject *code = (PyCodeObject*)self;
2152+
PyCodeObject *code = _PyCodeObject_CAST(self);
21502153
return _PyCode_GetFreevars(code);
21512154
}
21522155

21532156
static PyObject *
21542157
code_getcodeadaptive(PyObject *self, void *closure)
21552158
{
2156-
PyCodeObject *code = (PyCodeObject*)self;
2159+
PyCodeObject *code = _PyCodeObject_CAST(self);
21572160
return PyBytes_FromStringAndSize(code->co_code_adaptive,
21582161
_PyCode_NBYTES(code));
21592162
}
21602163

21612164
static PyObject *
21622165
code_getcode(PyObject *self, void *closure)
21632166
{
2164-
PyCodeObject *code = (PyCodeObject*)self;
2167+
PyCodeObject *code = _PyCodeObject_CAST(self);
21652168
return _PyCode_GetCode(code);
21662169
}
21672170

@@ -2180,7 +2183,7 @@ static PyGetSetDef code_getsetlist[] = {
21802183
static PyObject *
21812184
code_sizeof(PyObject *self, PyObject *Py_UNUSED(args))
21822185
{
2183-
PyCodeObject *co = (PyCodeObject*)self;
2186+
PyCodeObject *co = _PyCodeObject_CAST(self);
21842187
size_t res = _PyObject_VAR_SIZE(Py_TYPE(co), Py_SIZE(co));
21852188
_PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra*) co->co_extra;
21862189
if (co_extra != NULL) {
@@ -2193,13 +2196,14 @@ code_sizeof(PyObject *self, PyObject *Py_UNUSED(args))
21932196
static PyObject *
21942197
code_linesiterator(PyObject *self, PyObject *Py_UNUSED(args))
21952198
{
2196-
PyCodeObject *code = (PyCodeObject*)self;
2199+
PyCodeObject *code = _PyCodeObject_CAST(self);
21972200
return (PyObject *)new_linesiterator(code);
21982201
}
21992202

22002203
static PyObject *
2201-
code_branchesiterator(PyCodeObject *code, PyObject *Py_UNUSED(args))
2204+
code_branchesiterator(PyObject *self, PyObject *Py_UNUSED(args))
22022205
{
2206+
PyCodeObject *code = _PyCodeObject_CAST(self);
22032207
return _PyInstrumentation_BranchesIterator(code);
22042208
}
22052209

@@ -2343,7 +2347,7 @@ code__varname_from_oparg_impl(PyCodeObject *self, int oparg)
23432347
static struct PyMethodDef code_methods[] = {
23442348
{"__sizeof__", code_sizeof, METH_NOARGS},
23452349
{"co_lines", code_linesiterator, METH_NOARGS},
2346-
{"co_branches", (PyCFunction)code_branchesiterator, METH_NOARGS},
2350+
{"co_branches", code_branchesiterator, METH_NOARGS},
23472351
{"co_positions", code_positionsiterator, METH_NOARGS},
23482352
CODE_REPLACE_METHODDEF
23492353
CODE__VARNAME_FROM_OPARG_METHODDEF
@@ -2358,7 +2362,7 @@ PyTypeObject PyCode_Type = {
23582362
"code",
23592363
offsetof(PyCodeObject, co_code_adaptive),
23602364
sizeof(_Py_CODEUNIT),
2361-
(destructor)code_dealloc, /* tp_dealloc */
2365+
code_dealloc, /* tp_dealloc */
23622366
0, /* tp_vectorcall_offset */
23632367
0, /* tp_getattr */
23642368
0, /* tp_setattr */

0 commit comments

Comments
 (0)
0