Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-42015: Reorder dereferencing calls in meth_dealloc, to make sure m_self is kept alive long enough #22670

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix potential crash in deallocating method objects when dynamically
allocated `PyMethodDef`'s lifetime is managed through the ``self``
argument of a `PyCFunction`.
6 changes: 4 additions & 2 deletions Objects/methodobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ meth_dealloc(PyCFunctionObject *m)
if (m->m_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*) m);
}
// Dereference class before m_self: PyCFunction_GET_CLASS accesses
// PyMethodDef m_ml, which could be kept alive by m_self
Py_XDECREF(PyCFunction_GET_CLASS(m));
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
Py_XDECREF(m->m_self);
Py_XDECREF(m->m_module);
Py_XDECREF(PyCFunction_GET_CLASS(m));
PyObject_GC_Del(m);
}

Expand Down Expand Up @@ -243,9 +245,9 @@ meth_get__qualname__(PyCFunctionObject *m, void *closure)
static int
meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
{
Py_VISIT(PyCFunction_GET_CLASS(m));
Py_VISIT(m->m_self);
Py_VISIT(m->m_module);
Py_VISIT(PyCFunction_GET_CLASS(m));
return 0;
}

Expand Down