Check for errors when calling std::stod in XML loading.
PiperOrigin-RevId: 576608985 Change-Id: I148ca67d8960d73c281df2d6f2440d47940a6369
This commit is contained in:
committed by
Copybara-Service
parent
034bb62986
commit
f8c5ad2ba8
@@ -20,6 +20,7 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -406,7 +407,11 @@ bool mjCComposite::MakeParticle(mjCModel* model, mjCBody* body, char* error, int
|
||||
std::vector<mjtNum> 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);
|
||||
|
||||
+10
-4
@@ -21,6 +21,7 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
@@ -303,13 +304,18 @@ void mjCMesh::LoadSDF() {
|
||||
}
|
||||
|
||||
int i=0;
|
||||
mjtNum attributes[10] = {0};
|
||||
std::vector<mjtNum> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,6 +303,29 @@ TEST_F(ElasticityTest, InvalidMixedAttribute) {
|
||||
ASSERT_THAT(m, testing::IsNull());
|
||||
}
|
||||
|
||||
TEST_F(ElasticityTest, InvalidThickness) {
|
||||
static constexpr char xml[] = R"(
|
||||
<mujoco>
|
||||
<extension>
|
||||
<plugin plugin="mujoco.elasticity.shell"/>
|
||||
</extension>
|
||||
|
||||
<worldbody>
|
||||
<composite type="particle" count="2 2 1" spacing="1">
|
||||
<geom size=".025"/>
|
||||
<plugin plugin="mujoco.elasticity.shell">
|
||||
<config key="thickness" value="hello"/>
|
||||
</plugin>
|
||||
</composite>
|
||||
</worldbody>
|
||||
</mujoco>
|
||||
)";
|
||||
|
||||
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"(
|
||||
<mujoco>
|
||||
|
||||
Reference in New Issue
Block a user