Skip to content

Commit c4ee83c

Browse files
authored
fix:: initialize_generic compiler warning about nullptr dereference (#5756)
When compiling an application using pybind11 3.0.0, GCC 13.3.0 and python 3.11.13 the following warning is emitted [1]: In function 'PyObject* PyCFunction_GET_SELF(PyObject*)', inlined from 'void pybind11::cpp_function::initialize_generic(unique_function_record&&, const char*, const std::type_info* const*, pybind11::size_t)' at /opt/conda/lib/python3.11/site-packages/pybind11/include/pybind11/pybind11.h:605:30: /opt/conda/include/python3.11/cpython/methodobject.h:50:16: error: potential null pointer dereference [-Werror=null-dereference] 50 | return _Py_NULL; | ^~~~~~~~ It stems form the fact that PyCFunction_GET_SELF can return a nullptr. Let's fail in this case. [1]: https://gitlab.com/tango-controls/pytango/-/jobs/10671972312#L570
1 parent cc69a37 commit c4ee83c

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

include/pybind11/pybind11.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,10 @@ class cpp_function : public function {
603603
if (rec->sibling) {
604604
if (PyCFunction_Check(rec->sibling.ptr())) {
605605
auto *self = PyCFunction_GET_SELF(rec->sibling.ptr());
606+
if (self == nullptr) {
607+
pybind11_fail(
608+
"initialize_generic: Unexpected nullptr from PyCFunction_GET_SELF");
609+
}
606610
chain = detail::function_record_ptr_from_PyObject(self);
607611
if (chain && !chain->scope.is(rec->scope)) {
608612
/* Never append a method to an overload chain of a parent class;

0 commit comments

Comments
 (0)