diff --git a/src/parsers/mjcf/mjcf-graph.cpp b/src/parsers/mjcf/mjcf-graph.cpp index b25782ebc..d83e0ec29 100644 --- a/src/parsers/mjcf/mjcf-graph.cpp +++ b/src/parsers/mjcf/mjcf-graph.cpp @@ -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; diff --git a/unittest/mjcf.cpp b/unittest/mjcf.cpp index 1539fcd2b..8ecb04d04 100644 --- a/unittest/mjcf.cpp +++ b/unittest/mjcf.cpp @@ -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(""); + + 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;