Skip to content

Commit

Permalink
Merge pull request #7954 from omoerbeek/backport-7886-to-rec-4.2.x
Browse files Browse the repository at this point in the history
rec backport  to rec 4.2.x: SuffixMatchRee fixes
  • Loading branch information
omoerbeek committed Jun 19, 2019
2 parents 621c379 + 41535f7 commit f36a473
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
18 changes: 14 additions & 4 deletions pdns/dnsname.hh
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ struct SuffixMatchTree
*/
void remove(std::vector<std::string> labels) const
{
if (labels.empty()) { // this allows removal of the root
endNode = false;
return;
}

SuffixMatchTree smt(*labels.rbegin());
auto child = children.find(smt);
if (child == children.end()) {
Expand Down Expand Up @@ -328,8 +333,9 @@ struct SuffixMatchTree
if(children.empty()) { // speed up empty set
if(endNode)
return &d_value;
return 0;
return nullptr;
}

return lookup(name.getRawLabels());
}

Expand All @@ -338,18 +344,22 @@ struct SuffixMatchTree
if(labels.empty()) { // optimization
if(endNode)
return &d_value;
return 0;
return nullptr;
}

SuffixMatchTree smn(*labels.rbegin());
auto child = children.find(smn);
if(child == children.end()) {
if(endNode)
return &d_value;
return 0;
return nullptr;
}
labels.pop_back();
return child->lookup(labels);
auto result = child->lookup(labels);
if (result) {
return result;
}
return endNode ? &d_value : nullptr;
}

// Returns all end-nodes, fully qualified (not as separate labels)
Expand Down
11 changes: 9 additions & 2 deletions pdns/test-dnsname_cc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,16 @@ BOOST_AUTO_TEST_CASE(test_suffixmatch) {
BOOST_CHECK(smn.check(examplenet));
BOOST_CHECK(smn.check(net));

// Remove .net and check that example.net still exists
// Remove .net and the root, and check that example.net still exists
smn.remove(g_rootdnsname);
smn.remove(net);
BOOST_CHECK_EQUAL(smn.check(net), false);
BOOST_CHECK(smn.check(examplenet));

smn.add(DNSName("fr."));
smn.add(DNSName("www.sub.domain.fr."));
// should not match www.sub.domain.fr. but should still match fr.
BOOST_CHECK(smn.check(DNSName("sub.domain.fr.")));
}

BOOST_AUTO_TEST_CASE(test_suffixmatch_tree) {
Expand Down Expand Up @@ -564,7 +570,8 @@ BOOST_AUTO_TEST_CASE(test_suffixmatch_tree) {
BOOST_REQUIRE(smt.lookup(net));
BOOST_CHECK_EQUAL(*smt.lookup(net), net);

// Remove .net, and check that example.net remains
// Remove .net and the root, and check that example.net remains
smt.remove(g_rootdnsname);
smt.remove(net);
BOOST_CHECK(smt.lookup(net) == nullptr);
BOOST_CHECK_EQUAL(*smt.lookup(examplenet), examplenet);
Expand Down

0 comments on commit f36a473

Please sign in to comment.