Skip to content

Commit

Permalink
mjcf parser: Add check for empty bodies list before accessing root link
Browse files Browse the repository at this point in the history
  • Loading branch information
JafarAbdi committed Jan 24, 2025
1 parent 3c6ee1c commit ac9bc4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/parsers/mjcf/mjcf-graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,10 @@ namespace pinocchio
void MjcfGraph::parseRootTree()
{
urdfVisitor.setName(modelName);
if (bodiesList.empty())
{
PINOCCHIO_THROW_PRETTY(std::runtime_error, modelName + " has no root link");
}
// get name and inertia of first root link
std::string rootLinkName = bodiesList.at(0);
MjcfBody rootBody = mapOfBodies.find(rootLinkName)->second;
Expand Down
15 changes: 15 additions & 0 deletions unittest/mjcf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,21 @@ BOOST_AUTO_TEST_CASE(test_get_unknown_size_vector_from_stream)
BOOST_CHECK(v3 == expected3);
}

BOOST_AUTO_TEST_CASE(no_root_tree)
{
std::istringstream xmlData("<mujoco></mujoco>");

auto namefile = createTempFile(xmlData);

typedef ::pinocchio::mjcf::details::MjcfGraph MjcfGraph;
pinocchio::Model model_m;
MjcfGraph::UrdfVisitor visitor(model_m);

MjcfGraph graph(visitor, "fakeMjcf");
graph.parseGraphFromXML(namefile.name());
BOOST_CHECK_THROW(graph.parseRootTree(), std::runtime_error);
}

BOOST_AUTO_TEST_CASE(process_include_basic)
{
namespace pt = boost::property_tree;
Expand Down

0 comments on commit ac9bc4a

Please sign in to comment.