Skip to content
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
9 changes: 9 additions & 0 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,15 @@ enum class return_value_policy : uint8_t {

PYBIND11_NAMESPACE_BEGIN(detail)

// Py_IsFinalizing() is a public API since 3.13; before that use _Py_IsFinalizing().
inline bool py_is_finalizing() {
#if PY_VERSION_HEX >= 0x030D0000
return Py_IsFinalizing() != 0;
#else
return _Py_IsFinalizing() != 0;
#endif
}

static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); }

// Returns the size as a multiple of sizeof(void *), rounded up.
Expand Down
19 changes: 17 additions & 2 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,11 @@ class cpp_function : public function {
std::free(const_cast<char *>(arg.descr));
}
}
for (auto &arg : rec->args) {
arg.value.dec_ref();
// During finalization, default arg values may already be freed by GC.
if (!detail::py_is_finalizing()) {
for (auto &arg : rec->args) {
arg.value.dec_ref();
}
}
if (rec->def) {
std::free(const_cast<char *>(rec->def->ml_doc));
Expand Down Expand Up @@ -1342,9 +1345,21 @@ PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods)

// This implementation needs the definition of `class cpp_function`.
inline void tp_dealloc_impl(PyObject *self) {
// Skip dealloc during finalization — GC may have already freed objects
// reachable from the function record (e.g. default arg values), causing
// use-after-free in destruct().
if (detail::py_is_finalizing()) {
return;
}
// Save type before PyObject_Free invalidates self.
auto *type = Py_TYPE(self);
auto *py_func_rec = reinterpret_cast<function_record_PyObject *>(self);
cpp_function::destruct(py_func_rec->cpp_func_rec);
py_func_rec->cpp_func_rec = nullptr;
// PyObject_New increments the heap type refcount and allocates via
// PyObject_Malloc; balance both here
PyObject_Free(self);
Py_DECREF(type);
}

PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods)
Expand Down
Loading