From f8c5ad2ba8fb879fffa1688f7f22723a0cb675bd Mon Sep 17 00:00:00 2001 From: Nimrod Gileadi Date: Wed, 25 Oct 2023 12:25:33 -0700 Subject: [PATCH] Check for errors when calling std::stod in XML loading. PiperOrigin-RevId: 576608985 Change-Id: I148ca67d8960d73c281df2d6f2440d47940a6369 --- src/user/user_composite.cc | 7 ++++++- src/user/user_mesh.cc | 14 ++++++++++---- test/plugin/elasticity/elasticity_test.cc | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/user/user_composite.cc b/src/user/user_composite.cc index 03c8e9b6..b82fb680 100644 --- a/src/user/user_composite.cc +++ b/src/user/user_composite.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -406,7 +407,11 @@ bool mjCComposite::MakeParticle(mjCModel* model, mjCBody* body, char* error, int std::vector volume(uservert.size()/3); mjtNum t = 1; if (dim == 2 && plugin_instance) { - t = stod(plugin_instance->config_attribs["thickness"], nullptr); + try { + t = std::stod(plugin_instance->config_attribs["thickness"], nullptr); + } catch (const std::invalid_argument& e) { + return comperr(error, "Invalid thickness attribute", error_sz); + } } if (!userface.empty()) { mjXUtil::String2Vector(userface, face); diff --git a/src/user/user_mesh.cc b/src/user/user_mesh.cc index b0e1175a..0c70f03a 100644 --- a/src/user/user_mesh.cc +++ b/src/user/user_mesh.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -303,13 +304,18 @@ void mjCMesh::LoadSDF() { } int i=0; - mjtNum attributes[10] = {0}; + std::vector attributes(plugin->nattribute, 0); for (auto const& pair : plugin_instance->config_attribs) { - attributes[i++] = std::stod(pair.second); + try { + attributes[i++] = std::stod(pair.second); + } catch (const std::invalid_argument& e) { + throw mjCError(this, "invalid attribute value for '%s'", + pair.first.c_str()); + } } mjtNum aabb[6] = {0}; - plugin->sdf_aabb(aabb, attributes); + plugin->sdf_aabb(aabb, attributes.data()); mjtNum total = aabb[3] + aabb[4] + aabb[5]; const mjtNum n = 300; @@ -325,7 +331,7 @@ void mjCMesh::LoadSDF() { mjtNum point[] = {aabb[0]-aabb[3] + 2 * aabb[3] * i / (nx-1), aabb[1]-aabb[4] + 2 * aabb[4] * j / (ny-1), aabb[2]-aabb[5] + 2 * aabb[5] * k / (nz-1)}; - field[(k * ny + j) * nx + i] = plugin->sdf_staticdistance(point, attributes); + field[(k * ny + j) * nx + i] = plugin->sdf_staticdistance(point, attributes.data()); } } } diff --git a/test/plugin/elasticity/elasticity_test.cc b/test/plugin/elasticity/elasticity_test.cc index 3bbfd040..ae240eba 100644 --- a/test/plugin/elasticity/elasticity_test.cc +++ b/test/plugin/elasticity/elasticity_test.cc @@ -303,6 +303,29 @@ TEST_F(ElasticityTest, InvalidMixedAttribute) { ASSERT_THAT(m, testing::IsNull()); } +TEST_F(ElasticityTest, InvalidThickness) { + static constexpr char xml[] = R"( + + + + + + + + + + + + + + + )"; + + char error[1024] = {0}; + mjModel* m = LoadModelFromString(xml, error, sizeof(error)); + ASSERT_THAT(m, testing::IsNull()); +} + TEST_F(ElasticityTest, ValidAttributes) { static constexpr char cantilever_xml[] = R"(