Skip to content

Commit 54136b8

Browse files
committed
Add nullptr guards in get_internals() for better crash diagnostics
Add explicit null checks after get_pp() and create_pp_content_once() in get_internals(), calling pybind11_fail() with descriptive messages. These guards convert potential null-pointer dereferences (which produce unhelpful access-violation crashes, especially on Windows) into clear runtime_error messages that can be caught and reported as ImportError by the try-catch added in the previous commit. Made-with: Cursor
1 parent d35af78 commit 54136b8

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

include/pybind11/detail/internals.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,14 +862,21 @@ inline internals_pp_manager<internals> &get_internals_pp_manager() {
862862
/// Return a reference to the current `internals` data
863863
PYBIND11_NOINLINE internals &get_internals() {
864864
auto &ppmgr = get_internals_pp_manager();
865-
auto &internals_ptr = *ppmgr.get_pp();
865+
auto *pp = ppmgr.get_pp();
866+
if (!pp) {
867+
pybind11_fail("get_internals: get_pp() returned nullptr");
868+
}
869+
auto &internals_ptr = *pp;
866870
if (!internals_ptr) {
867871
// Slow path, something needs fetched from the state dict or created
868872
gil_scoped_acquire_simple gil;
869873
error_scope err_scope;
870874

871875
ppmgr.create_pp_content_once(&internals_ptr);
872876

877+
if (!internals_ptr) {
878+
pybind11_fail("get_internals: create_pp_content_once() produced nullptr");
879+
}
873880
if (!internals_ptr->instance_base) {
874881
// This calls get_internals, so cannot be called from within the internals constructor
875882
// called above because internals_ptr must be set before get_internals is called again

0 commit comments

Comments
 (0)