Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segfault when checking for required elements in joint #610

Merged
merged 3 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1495,13 +1495,13 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
if (_sdf->GetName() == "joint" &&
_sdf->Get<std::string>("type") != "ball")
{
Error err(
Error missingElementError(
ErrorCode::ELEMENT_MISSING,
"XML Missing required element[" + elemDesc->GetName() +
"], child of element[" + _sdf->GetName() + "]",
errorSourcePath, elemXml->GetLineNum());
err.SetXmlPath(elemXmlPath);
_errors.push_back(err);
errorSourcePath, _xml->GetLineNum());
missingElementError.SetXmlPath(elemXmlPath);
_errors.push_back(missingElementError);
return false;
}
else
Expand Down
39 changes: 39 additions & 0 deletions src/parser_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,45 @@ TEST(Parser, ElementFilePath)
EXPECT_EQ("", directModel->FilePath());
}

/////////////////////////////////////////////////
/// Check for missing element segfault, #590
TEST(Parser, MissingRequiredElement)
{
const std::string testString = R"(<?xml version="1.0" ?>
<sdf version="1.8">
<world name="default">
<model name="test_model">
<link name="link_0"/>
<link name="link_1"/>
<joint name="joint" type="revolute">
<parent>link_0</parent>
<axis>
<xyz>1 0 0</xyz>
<limit>
<lower>-1</lower>
<upper>1</upper>
</limit>
</axis>
</joint>
</model>
</world>
</sdf>)";

sdf::Errors errors;
sdf::SDFPtr sdf = InitSDF();
EXPECT_FALSE(sdf::readString(testString, sdf, errors));

ASSERT_NE(errors.size(), 0u);
std::cerr << errors[0] << std::endl;

EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::ELEMENT_MISSING);
ASSERT_TRUE(errors[0].FilePath().has_value());
EXPECT_EQ(errors[0].FilePath().value(),
"<" + std::string(sdf::kSdfStringSource) + ">");
ASSERT_TRUE(errors[0].LineNumber().has_value());
EXPECT_EQ(errors[0].LineNumber().value(), 7);
}

/////////////////////////////////////////////////
/// Main
int main(int argc, char **argv)
Expand Down