fix: detect virtual inheritance in add_base to prevent pointer offset crash (#6017)

Virtual inheritance places the base subobject at a dynamic offset, but
load_impl Case 2a uses reinterpret_cast which assumes a fixed offset.
This caused segfaults when dispatching inherited methods through virtual
bases (e.g. SftVirtDerived2::name()).

Add an is_static_downcastable SFINAE trait that detects whether
static_cast<Derived*>(Base*) is valid. When it is not (virtual
inheritance), set multiple_inheritance = true in add_base to force the
implicit_casts path, which correctly adjusts pointers at runtime.

Remove the workaround .def("name", &SftVirtDerived2::name) from
test_smart_ptr.cpp that was papering over the issue.

Made-with: Cursor
This commit is contained in:
Ralf W. Grosse-Kunstleve
2026-03-30 10:49:27 +07:00
committed by GitHub
parent 524d72b36d
commit 83f71d8b82
3 changed files with 18 additions and 4 deletions

View File

@@ -553,10 +553,6 @@ TEST_SUBMODULE(smart_ptr, m) {
py::class_<SftVirtDerived2, SftVirtDerived, std::shared_ptr<SftVirtDerived2>>(
m, "SftVirtDerived2")
.def(py::init<>(&SftVirtDerived2::create))
// TODO: Remove this once inherited methods work through virtual bases.
// Without it, d2.name() segfaults because pybind11 uses an incorrect
// pointer offset when dispatching through the virtual inheritance chain.
.def("name", &SftVirtDerived2::name)
.def("call_name", &SftVirtDerived2::call_name, py::arg("d2"));
// test_move_only_holder