From 60f9b34a77cf6cb0602e3f684e64d97d911ea3af Mon Sep 17 00:00:00 2001 From: Alessio Quaglino Date: Mon, 21 Jul 2025 06:22:50 -0700 Subject: [PATCH] Validate the MjModel pointer passed to MjData constructor. Fixes #2426. PiperOrigin-RevId: 785411895 Change-Id: If698120b5a4ab4ac457d35760380c140b1b5a208 --- python/mujoco/structs.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/mujoco/structs.cc b/python/mujoco/structs.cc index 4501d593..0c0e5958 100644 --- a/python/mujoco/structs.cc +++ b/python/mujoco/structs.cc @@ -728,7 +728,12 @@ This is useful for example when the MJB is not available as a file on disk.)")); // ==================== MJDATA =============================================== py::class_ mjData(m, "MjData"); - mjData.def(py::init()); + mjData.def(py::init([](MjModelWrapper* m) { + if (!m) { + throw py::type_error("MjModel cannot be None"); + } + return MjDataWrapper(m); + })); mjData.def_property_readonly("_address", [](const MjDataWrapper& d) { return reinterpret_cast(d.get()); });