From 99a5b2e2be60c961fa2167c61c48736272844976 Mon Sep 17 00:00:00 2001 From: ems Date: Thu, 25 Jun 2026 23:23:22 +0200 Subject: [PATCH 1/4] fix: prevent segfault in ResolvePlugin when instance is missing --- src/user/user_model.cc | 3 +-- test/user/CMakeLists.txt | 5 ++++- test/user/user_model_test.cc | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/user/user_model.cc b/src/user/user_model.cc index e3867a41..c9890cc1 100644 --- a/src/user/user_model.cc +++ b/src/user/user_model.cc @@ -5952,15 +5952,14 @@ void mjCModel::ResolvePlugin(mjCBase* obj, const std::string& plugin_name, else if (!*plugin_instance) { *plugin_instance = static_cast(FindObject(mjOBJ_PLUGIN, plugin_instance_name)); - (*plugin_instance)->plugin_slot = plugin_slot; if (!*plugin_instance) { throw mjCError( obj, "unrecognized name '%s' for plugin instance", plugin_instance_name.c_str()); } + (*plugin_instance)->plugin_slot = plugin_slot; if (plugin_slot != -1 && plugin_slot != (*plugin_instance)->plugin_slot) { throw mjCError( obj, "'plugin' attribute does not match that of the instance"); } - plugin_slot = (*plugin_instance)->plugin_slot; } } diff --git a/test/user/CMakeLists.txt b/test/user/CMakeLists.txt index 3b073c3d..2cac43e5 100644 --- a/test/user/CMakeLists.txt +++ b/test/user/CMakeLists.txt @@ -14,7 +14,10 @@ mujoco_test( user_model_test - ADDITIONAL_LINK_LIBRARIES absl::str_format compare_model + ADDITIONAL_LINK_LIBRARIES absl::str_format compare_model + PROPERTIES + ENVIRONMENT + "MUJOCO_PLUGIN_DIR=$" ) mujoco_test(user_objects_test) diff --git a/test/user/user_model_test.cc b/test/user/user_model_test.cc index 52446a3a..f2215366 100644 --- a/test/user/user_model_test.cc +++ b/test/user/user_model_test.cc @@ -872,6 +872,43 @@ TEST_F(LengthRangeTest, LengthRangeThreading) { mj_deleteSpec(spec); } + +TEST_F(MujocoTest, ResolvePluginMissingInstanceThrowsError) { + // Instance name="my_pid_config" dos not match actuator plugin's instance="pid_config", + // this should throw an appropriate error + static constexpr char xml_mismatch[] = R"( + + + + + + + + + + + + + + + + + )"; + + std::array error_buffer; + mjSpec* spec = mj_parseXMLString(xml_mismatch, 0, error_buffer.data(), error_buffer.size()); + ASSERT_THAT(spec, NotNull()) << error_buffer.data(); + + mjModel* model = mj_compile(spec, nullptr); + EXPECT_THAT(model, IsNull()); + + std::string error_msg = mjs_getError(spec); + EXPECT_THAT(error_msg, HasSubstr("unrecognized name 'pid_config' for plugin instance")); + + if (model) mj_deleteModel(model); + mj_deleteSpec(spec); +} + // ----------------------------- test modeldir -------------------------------- TEST_F(MujocoTest, Modeldir) { From fb4686d049b693d56e3e5843246b73239cd707d6 Mon Sep 17 00:00:00 2001 From: ebms03 Date: Mon, 13 Jul 2026 16:58:55 +0200 Subject: [PATCH 2/4] Update CMakeLists.txt Remove trailing whitespace --- test/user/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/user/CMakeLists.txt b/test/user/CMakeLists.txt index 2cac43e5..87ce1d90 100644 --- a/test/user/CMakeLists.txt +++ b/test/user/CMakeLists.txt @@ -14,7 +14,7 @@ mujoco_test( user_model_test - ADDITIONAL_LINK_LIBRARIES absl::str_format compare_model + ADDITIONAL_LINK_LIBRARIES absl::str_format compare_model PROPERTIES ENVIRONMENT "MUJOCO_PLUGIN_DIR=$" From fc5ed199a65ee0b66d94dfa788493eb09d4c8e8f Mon Sep 17 00:00:00 2001 From: ebms03 Date: Mon, 13 Jul 2026 17:03:51 +0200 Subject: [PATCH 3/4] Update user_model_test.cc Remove trailing whitespace --- test/user/user_model_test.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/user/user_model_test.cc b/test/user/user_model_test.cc index f2215366..c2543292 100644 --- a/test/user/user_model_test.cc +++ b/test/user/user_model_test.cc @@ -880,7 +880,7 @@ TEST_F(MujocoTest, ResolvePluginMissingInstanceThrowsError) { - + @@ -901,13 +901,13 @@ TEST_F(MujocoTest, ResolvePluginMissingInstanceThrowsError) { mjModel* model = mj_compile(spec, nullptr); EXPECT_THAT(model, IsNull()); - + std::string error_msg = mjs_getError(spec); - EXPECT_THAT(error_msg, HasSubstr("unrecognized name 'pid_config' for plugin instance")); - + EXPECT_THAT(error_msg, HasSubstr("unrecognized name 'pid_config' for plugin instance")); + if (model) mj_deleteModel(model); mj_deleteSpec(spec); -} +}commit // ----------------------------- test modeldir -------------------------------- From 07c14a7c1a0669dcc05e5bc3698682177166f2e7 Mon Sep 17 00:00:00 2001 From: ebms03 Date: Mon, 13 Jul 2026 20:46:07 +0200 Subject: [PATCH 4/4] Update user_model_test.cc typo --- test/user/user_model_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/user/user_model_test.cc b/test/user/user_model_test.cc index c2543292..c247d492 100644 --- a/test/user/user_model_test.cc +++ b/test/user/user_model_test.cc @@ -907,7 +907,7 @@ TEST_F(MujocoTest, ResolvePluginMissingInstanceThrowsError) { if (model) mj_deleteModel(model); mj_deleteSpec(spec); -}commit +} // ----------------------------- test modeldir --------------------------------