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

Error when delimiter "::" found in element name in SDFormat 1.8 #515

Merged
merged 4 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ but with improved human-readability..

1. **heightmap.sdf**: sampling now defaults to 1 instead of 2.

1. Element attribute names containing delimiter "::" no longer accepted
* [Issue 420](https://github.com/osrf/sdformat/issues/420)

### Deprecations

1. **joint.sdf** `initial_position` element in `<joint><axis>` and `<joint><axis2>` is deprecated
Expand Down
9 changes: 9 additions & 0 deletions include/sdf/parser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,15 @@ namespace sdf
SDFORMAT_VISIBLE
bool recursiveSiblingUniqueNames(sdf::ElementPtr _elem);

/// \brief Check that all sibling elements do not the delimiter double colons
scpeters marked this conversation as resolved.
Show resolved Hide resolved
/// '::' in element names which is reserved for forming scopes in SDFormat 1.8
/// This checks recursively and should check the files exhaustively rather
/// than terminating early when the first name containing '::' is found.
/// \param[in] _elem SDF Element to check recursively.
/// \return True if all contained element names do not have the delimiter '::'
SDFORMAT_VISIBLE
bool recursiveSiblingNoDoubleColonInNames(sdf::ElementPtr _elem);

/// \brief Check whether the element should be validated. If this returns
/// false, validators such as the unique name and reserve name checkers should
/// skip this element and its descendants.
Expand Down
50 changes: 50 additions & 0 deletions src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,18 @@ bool readDoc(tinyxml2::XMLDocument *_xmlDoc, SDFPtr _sdf,
"Error reading element <" + _sdf->Root()->GetName() + ">"});
return false;
}

// delimiter '::' in element names not allowed in SDFormat >= 1.8
ignition::math::SemanticVersion sdfVersion(_sdf->Root()->OriginalVersion());
if (sdfVersion >= ignition::math::SemanticVersion(1, 8)
&& !recursiveSiblingNoDoubleColonInNames(_sdf->Root()))
{
_errors.push_back({ErrorCode::RESERVED_NAME,
"Delimiter '::' found in attribute names of element <"
+ _sdf->Root()->GetName() +
">, which is not allowed in SDFormat >= 1.8"});
return false;
}
}
else
{
Expand Down Expand Up @@ -755,6 +767,18 @@ bool readDoc(tinyxml2::XMLDocument *_xmlDoc, ElementPtr _sdf,
"Unable to parse sdf element["+ _sdf->GetName() + "]"});
return false;
}

// delimiter '::' in element names not allowed in SDFormat >= 1.8
ignition::math::SemanticVersion sdfVersion(_sdf->OriginalVersion());
if (sdfVersion >= ignition::math::SemanticVersion(1, 8)
&& !recursiveSiblingNoDoubleColonInNames(_sdf))
{
_errors.push_back({ErrorCode::RESERVED_NAME,
"Delimiter '::' found in attribute names of element <"
+ _sdf->GetName() +
">, which is not allowed in SDFormat >= 1.8"});
return false;
}
}
else
{
Expand Down Expand Up @@ -1739,6 +1763,32 @@ bool recursiveSiblingUniqueNames(sdf::ElementPtr _elem)
return result;
}

//////////////////////////////////////////////////
bool recursiveSiblingNoDoubleColonInNames(sdf::ElementPtr _elem)
{
if (!shouldValidateElement(_elem))
return true;

bool result = true;
if (_elem->HasAttribute("name")
&& _elem->Get<std::string>("name").find("::") != std::string::npos)
{
std::cerr << "Error: Detected delimiter '::' in element name in\n"
<< _elem->ToString("")
<< std::endl;
result = false;
}

sdf::ElementPtr child = _elem->GetFirstElement();
while (child)
{
result = recursiveSiblingNoDoubleColonInNames(child) && result;
child = child->GetNextElement();
}

return result;
}

//////////////////////////////////////////////////
bool checkFrameAttachedToGraph(const sdf::Root *_root)
{
Expand Down
25 changes: 25 additions & 0 deletions src/parser_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,31 @@ TEST(Parser, PlacementFrameMissingPose)
EXPECT_EQ(sdf::ErrorCode::MODEL_PLACEMENT_FRAME_INVALID, errors[0].Code());
}

/////////////////////////////////////////////////
// Delimiter '::' in name should error in SDFormat 1.8 but not in 1.7
TEST(Parser, DoubleColonNameAttrError)
{
sdf::SDFPtr sdf = InitSDF();
std::ostringstream stream;
stream << "<?xml version=\"1.0\"?>"
<< "<sdf version='1.8'>"
<< " <model name='test'>"
<< " <link name='A::B'/>"
<< " </model>"
<< "</sdf>";

EXPECT_FALSE(sdf::readString(stream.str(), sdf));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could pass an sdf::Errors object to sdf::readString and check its contents

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great!


sdf = InitSDF();
stream.str("");
stream << "<?xml version=\"1.0\"?>"
<< "<sdf version='1.7'>"
<< " <model name='test::A'/>"
<< "</sdf>";

EXPECT_TRUE(sdf::readString(stream.str(), sdf));
}

/////////////////////////////////////////////////
/// Fixture for setting up stream redirection
class ValueConstraintsFixture : public ::testing::Test
Expand Down