Match alerts both with and without comments

We always locally check alerts against our subversion string without comments.
This change ensures that we will also propagate alerts to peers based on their
subversion strings without comments. Note that if an alert specifically targets
a commented subversion string, we will only relay it to peers with the exact
same comments.
This commit is contained in:
Jack Grigg 2018-03-28 17:18:52 +02:00
parent 5fd97dcdf9
commit 6278085c23
No known key found for this signature in database
GPG Key ID: 9E8255172BBF9898
2 changed files with 22 additions and 2 deletions

View File

@ -114,10 +114,28 @@ bool CAlert::Cancels(const CAlert& alert) const
bool CAlert::AppliesTo(int nVersion, const std::string& strSubVerIn) const
{
// Get a subversion without comments
std::string strSubVer = "";
auto start = 0;
auto end = 0;
while (start < strSubVerIn.length() && end < strSubVerIn.length()) {
end = strSubVerIn.find('(', start);
if (end == std::string::npos) {
// Ensure we get the section of strSubVerIn after the final comment
end = strSubVerIn.length();
}
strSubVer.append(strSubVerIn.substr(start, end - start));
start = strSubVerIn.find(')', end);
if (start != std::string::npos) {
// Finish with start pointing at the next character we want
start += 1;
}
}
// Check against both the commented and uncommented subversion
// TODO: rework for client-version-embedded-in-strSubVer ?
return (IsInEffect() &&
nMinVer <= nVersion && nVersion <= nMaxVer &&
(setSubVer.empty() || setSubVer.count(strSubVerIn)));
(setSubVer.empty() || setSubVer.count(strSubVerIn) || setSubVer.count(strSubVer)));
}
bool CAlert::AppliesToMe() const

View File

@ -311,9 +311,12 @@ BOOST_AUTO_TEST_CASE(AlertApplies)
BOOST_CHECK(alerts[2].AppliesTo(1, "/MagicBean:0.1.0/"));
BOOST_CHECK(alerts[2].AppliesTo(1, "/MagicBean:0.2.0/"));
BOOST_CHECK(alerts[2].AppliesTo(1, "/MagicBean:0.2.0(foo)/"));
BOOST_CHECK(alerts[2].AppliesTo(1, "/MagicBean:0.2.0(bar)/"));
BOOST_CHECK(alerts[3].AppliesTo(1, "/MagicBean:0.1.0/"));
BOOST_CHECK(alerts[3].AppliesTo(1, "/MagicBean:0.2.0/"));
BOOST_CHECK(alerts[2].AppliesTo(1, "/MagicBean:0.2.0(foo)/"));
BOOST_CHECK(alerts[3].AppliesTo(1, "/MagicBean:0.2.1(foo)/"));
BOOST_CHECK(alerts[4].AppliesTo(1, "/MagicBean:0.1.0/"));
@ -335,7 +338,6 @@ BOOST_AUTO_TEST_CASE(AlertApplies)
BOOST_CHECK(!alerts[1].AppliesTo(1, "/MagicBean:0.2.0/"));
// SubVer with comment doesn't match SubVer pattern without
BOOST_CHECK(!alerts[2].AppliesTo(1, "/MagicBean:0.2.0(foo)/"));
BOOST_CHECK(!alerts[2].AppliesTo(1, "/MagicBean:0.2.1/"));
BOOST_CHECK(!alerts[2].AppliesTo(1, "/MagicBean:0.3.0/"));