Skip to content

Conversation

@t-b
Copy link
Contributor

@t-b t-b commented Jul 17, 2025

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.

Description

Close #5755

Suggested changelog entry:

Avoid nullptr dereference warning with GCC 13.3.0 and python 3.11.13.

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
@t-b t-b force-pushed the fix-null-dereference-warning branch from 8cfe51b to f87a86b Compare July 17, 2025 14:22
@henryiii henryiii changed the title initialize_generic: Fix compiler warning about nullptr dereference fix: initialize_generic compiler warning about nullptr dereference Jul 18, 2025
@henryiii henryiii merged commit c4ee83c into pybind:master Jul 18, 2025
82 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Jul 18, 2025
@t-b t-b deleted the fix-null-dereference-warning branch July 18, 2025 17:59
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Aug 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Potential null pointer dereference after update to pybind11 3.0

2 participants