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

[20608][20610] Address XMLProfiles fuzzer regressions #4554

Merged
merged 6 commits into from
Mar 20, 2024
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
4 changes: 2 additions & 2 deletions src/cpp/rtps/participant/RTPSParticipantImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ static void set_builtin_transports_from_env_var(
if (std::regex_search(env_value, mr, msg_size_regex, std::regex_constants::match_not_null))
{
std::string value = mr[2];
std::string unit = (mr[3] == "") ? "B" : mr[3].str();
std::string unit = mr[3].str();
options.maxMessageSize = eprosima::fastdds::dds::utils::parse_value_and_units(value, unit);
}
// Sockets_size parser
if (std::regex_search(env_value, mr, sockets_size_regex, std::regex_constants::match_not_null))
{
std::string value = mr[2];
std::string unit = (mr[3] == "") ? "B" : mr[3].str();
std::string unit = mr[3].str();
options.sockets_buffer_size = eprosima::fastdds::dds::utils::parse_value_and_units(value, unit);
}
// Non-blocking-send parser
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/rtps/xmlparser/XMLElementParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4767,7 +4767,7 @@ XMLP_ret XMLParser::getXMLBuiltinTransports(
if (std::regex_search(temp, mr, msg_size_regex, std::regex_constants::match_not_null))
{
std::string value = mr[1];
std::string unit = (mr[2] == "") ? "B" : mr[2].str();
std::string unit = mr[2].str();
bt_opts->maxMessageSize = eprosima::fastdds::dds::utils::parse_value_and_units(value, unit);
}
}
Expand Down Expand Up @@ -4799,7 +4799,7 @@ XMLP_ret XMLParser::getXMLBuiltinTransports(
if (std::regex_search(temp, mr, sockets_size_regex, std::regex_constants::match_not_null))
{
std::string value = mr[1];
std::string unit = (mr[2] == "") ? "B" : mr[2].str();
std::string unit = mr[2].str();
bt_opts->sockets_buffer_size = eprosima::fastdds::dds::utils::parse_value_and_units(value, unit);
}
}
Expand Down
22 changes: 18 additions & 4 deletions src/cpp/utils/UnitsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ uint32_t parse_value_and_units(
std::string units)
{
static const std::map<std::string, std::uint32_t> magnitudes = {
{"", 1},
{"B", 1},
{"KB", 1000},
{"MB", 1000 * 1000},
Expand All @@ -64,16 +65,29 @@ uint32_t parse_value_and_units(
{"GIB", 1024 * 1024 * 1024},
};

uint64_t num = std::stoull(value);
uint64_t num = 0;
try
{
num = std::stoull(value);
}
catch (std::out_of_range&)
{
throw std::invalid_argument("Failed to parse value from string." \
" The number is too large to be converted to bytes (Max: (2^32)-1 Bytes).");
}

to_uppercase(units);

std::regex pattern(R"(([kibmgKIBMG]{1,3})|(\d+))");
if (!std::regex_match(units, pattern))
uint32_t magnitude = 0;
try
{
magnitude = magnitudes.at(units);
}
catch (std::out_of_range&)
{
throw std::invalid_argument(
"The units are not in the expected format. Use: {B, KB, MG, GB, KIB, MIB, GIB}.");
}
const auto magnitude = magnitudes.at(units);

// Check whether the product of number * magnitude overflows
if (num > std::numeric_limits<std::uint32_t>::max() / magnitude)
Expand Down
2 changes: 2 additions & 0 deletions test/unittest/xmlparser/XMLParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ TEST_F(XMLParserTests, regressions)
EXPECT_EQ(XMLP_ret::XML_OK, XMLParser::loadXML("regressions/simple_participant_profiles_ok.xml", root));
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParser::loadXML("regressions/20186_profile_bin.xml", root));
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParser::loadXML("regressions/20187_profile_bin.xml", root));
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParser::loadXML("regressions/20608_profile_bin.xml", root));
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParser::loadXML("regressions/20610_profile_bin.xml", root));
}

TEST_F(XMLParserTests, NoFile)
Expand Down
1 change: 1 addition & 0 deletions test/unittest/xmlparser/regressions/20608_profile_bin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<profiles><participant><rtps><builtinTransports max_msg_size="88888888888888888884"/></rtps></participant></profiles>
1 change: 1 addition & 0 deletions test/unittest/xmlparser/regressions/20610_profile_bin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<profiles><participant><rtps><builtinTransports max_msg_size="1i"/></rtps></participant></profiles>
Loading