Skip to content

Commit

Permalink
Improves MJCF unknown size vector parsing robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
JafarAbdi authored and Megane Millan committed Jan 6, 2025
1 parent 033a56f commit 5a60333
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 2 additions & 3 deletions include/pinocchio/parsers/mjcf/mjcf-graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ namespace pinocchio
inline std::istringstream getConfiguredStringStream(const std::string & str)
{
std::istringstream posStream(str);
posStream.exceptions(std::ios::failbit);
posStream.exceptions(std::ios::badbit);
return posStream;
}

Expand All @@ -569,9 +569,8 @@ namespace pinocchio
std::istringstream stream = getConfiguredStringStream(str);
std::vector<double> vector;
double elem;
while (!stream.eof())
while (stream >> elem)
{
stream >> elem;
vector.push_back(elem);
}

Expand Down
30 changes: 29 additions & 1 deletion unittest/mjcf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "pinocchio/multibody/model.hpp"

#include "pinocchio/parsers/mjcf.hpp"
#include "pinocchio/parsers/mjcf/mjcf-graph.hpp"
#include "pinocchio/parsers/urdf.hpp"

#include "pinocchio/algorithm/joint-configuration.hpp"
Expand Down Expand Up @@ -923,7 +924,8 @@ BOOST_AUTO_TEST_CASE(adding_keyframes)
<key name="test"
qpos="0 0 0.596
0.988015 0 0.154359 0
0.988015 0 0.154359 0"/>
0.988015 0 0.154359 0
"/>
</keyframe>
</mujoco>)");

Expand Down Expand Up @@ -1396,6 +1398,32 @@ BOOST_AUTO_TEST_CASE(parse_mesh_with_vertices)
{
BOOST_CHECK(mesh.vertices.row(i) == vertices.row(i));
}
BOOST_AUTO_TEST_CASE(test_get_unknown_size_vector_from_stream)
{
const auto v = pinocchio::mjcf::details::internal::getUnknownSizeVectorFromStream("");
BOOST_CHECK(v.size() == 0);

const auto v1 = pinocchio::mjcf::details::internal::getUnknownSizeVectorFromStream("1 2 3");
BOOST_CHECK(v1.size() == 3);
Eigen::VectorXd expected(3);
expected << 1, 2, 3;
BOOST_CHECK(v1 == expected);

const auto v2 = pinocchio::mjcf::details::internal::getUnknownSizeVectorFromStream(R"(1 2 3
4 5 6)");
BOOST_CHECK(v2.size() == 6);
Eigen::VectorXd expected2(6);
expected2 << 1, 2, 3, 4, 5, 6;
BOOST_CHECK(v2 == expected2);

const auto v3 = pinocchio::mjcf::details::internal::getUnknownSizeVectorFromStream(R"(1 2 3
4 5 6
7 8 9
)");
BOOST_CHECK(v3.size() == 9);
Eigen::VectorXd expected3(9);
expected3 << 1, 2, 3, 4, 5, 6, 7, 8, 9;
BOOST_CHECK(v3 == expected3);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 5a60333

Please sign in to comment.