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

[18395] Fix XMLParser null-derefence in parseLogConfig #3769

Merged
merged 2 commits into from
Aug 2, 2023
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
72 changes: 44 additions & 28 deletions src/cpp/rtps/xmlparser/XMLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1390,64 +1390,80 @@ XMLP_ret XMLParser::parseLogConfig(
tinyxml2::XMLElement* p_root)
{
/*
<xs:element name="log">
<xs:complexType>
<xs:boolean name="use_default"/>
<xs:sequence>
<xs:element maxOccurs="consumer">
<xs:complexType>
<xs:element name="class" type="string" minOccurs="1" maxOccurs="1"/>
<xs:sequence>
<xs:element name="propertyType"/>
</xs:sequence>
</xs:complexType>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="log" type="logType"/>
<xs:complexType name="logType">
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:choice minOccurs="1">
<xs:element name="use_default" type="booleanCaps" minOccurs="0" maxOccurs="1"/>
<xs:element name="consumer" type="logConsumerType" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
*/

/*
* TODO(eduponz): Uphold XSD validation in parsing
* Even though the XSD above enforces the log tag to have at least one consumer,
* the parsing allows for an empty log tag (e.g. `<log></log>`).
* This inconsistency is kept to keep a backwards compatible behaviour.
* In fact, test XMLParserTests.parseXMLNoRoot even checks that an empty log tag
* is valid.
*/

XMLP_ret ret = XMLP_ret::XML_OK;

tinyxml2::XMLElement* p_aux0 = p_root->FirstChildElement(LOG);
if (p_aux0 == nullptr)
{
p_aux0 = p_root;
}

tinyxml2::XMLElement* p_element = p_aux0->FirstChildElement();
const char* tag = nullptr;
while (nullptr != p_element)

while (ret == XMLP_ret::XML_OK && nullptr != p_element)
{
if (nullptr != (tag = p_element->Value()))
const char* tag = p_element->Value();
if (nullptr != tag)
{
if (strcmp(tag, USE_DEFAULT) == 0)
{
bool use_default = true;
std::string auxBool = p_element->GetText();
if (std::strcmp(auxBool.c_str(), "FALSE") == 0)
if (nullptr == p_element->GetText())
{
use_default = false;
EPROSIMA_LOG_ERROR(XMLPARSER, "Cannot get text from tag: '" << tag << "'");
ret = XMLP_ret::XML_ERROR;
}
if (!use_default)

if (ret == XMLP_ret::XML_OK)
{
eprosima::fastdds::dds::Log::ClearConsumers();
bool use_default = true;
std::string auxBool = p_element->GetText();
if (std::strcmp(auxBool.c_str(), "FALSE") == 0)
{
use_default = false;
}
if (!use_default)
{
eprosima::fastdds::dds::Log::ClearConsumers();
}
}
}
else if (strcmp(tag, CONSUMER) == 0)
{
ret = parseXMLConsumer(*p_element);
if (ret == XMLP_ret::XML_ERROR)
{
return ret;
}
}
else
{
EPROSIMA_LOG_ERROR(XMLPARSER, "Not expected tag: '" << tag << "'");
ret = XMLP_ret::XML_ERROR;
}
}
p_element = p_element->NextSiblingElement(CONSUMER);

if (ret == XMLP_ret::XML_OK)
{
p_element = p_element->NextSiblingElement(CONSUMER);
}
}

return ret;
}

Expand Down
1 change: 1 addition & 0 deletions test/unittest/xmlparser/XMLParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ TEST_F(XMLParserTests, regressions)
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParser::loadXML("regressions/13513_profile_bin.xml", root));
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParser::loadXML("regressions/14456_profile_bin.xml", root));
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParser::loadXML("regressions/15344_profile_bin.xml", root));
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParser::loadXML("regressions/18395_profile_bin.xml", root));
}

TEST_F(XMLParserTests, NoFile)
Expand Down
2 changes: 2 additions & 0 deletions test/unittest/xmlparser/regressions/18395_profile_bin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
v�< log>suu/</use_default/>use_de�ta-g/>��< /log>��ypes/������
</ds>