]> granicus.if.org Git - pdns/commitdiff
Kees Monshouwer spotted a bug in our DNSName isParent() implementation and delivered...
authorbert hubert <bert.hubert@netherlabs.nl>
Wed, 11 Mar 2015 10:55:39 +0000 (11:55 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Wed, 11 Mar 2015 10:55:39 +0000 (11:55 +0100)
pdns/dnsname.cc
pdns/test-dnsname_cc.cc

index be9821f1a1fe84f50e027b106ba373e87131f52c..78df576b859a90a7c747f924f7342d875ad7191d 100644 (file)
@@ -72,17 +72,24 @@ std::string DNSName::toDNSString() const
   return ret;
 }
 
-
-// true of a comparison from the end of parent terminates
+// are WE part of parent
 bool DNSName::isPartOf(const DNSName& parent) const
 {
-  auto us = d_storage.crbegin();
-  auto p = parent.d_storage.crbegin();
-  for(; us != d_storage.crend() && p != parent.d_storage.crend(); ++us, ++p) {
-    if(tolower(*p) != tolower(*us))
-      break;
+  if(parent.d_storage.size() > d_storage.size()) 
+    return false;
+
+  // this is slightly complicated since we can't start from the end, since we can't see where a label begins/ends then
+  for(auto us=d_storage.cbegin(); us<d_storage.cend() && d_storage.cend()-us >= (unsigned int)parent.d_storage.size(); us+=*us+1) {
+    if (d_storage.cend()-us == (unsigned int)parent.d_storage.size()) {
+      auto p = parent.d_storage.cbegin();
+      for(; us != d_storage.cend() && p != parent.d_storage.cend(); ++us, ++p) {
+        if(tolower(*p) != tolower(*us))
+          break;
+      }
+      return (p==parent.d_storage.end());
+    }
   }
-  return (p==parent.d_storage.crend());
+  return false;
 }
 
 void DNSName::appendRawLabel(const std::string& label)
index 9eff64975a3d1ded5b179e3b84af96a482729583..f1122e006927304006eee5d9a1f68c1c3419c8a3 100644 (file)
@@ -20,8 +20,10 @@ BOOST_AUTO_TEST_CASE(test_basic) {
   BOOST_CHECK_EQUAL(before, after);
 
   DNSName wwwds9anl("www.ds9a.nl.");
+  DNSName wwwds9anl1("www.ds9a\002nl.");
   DNSName nl("nl.");
   BOOST_CHECK(wwwds9anl.isPartOf(nl));
+  BOOST_CHECK(!wwwds9anl1.isPartOf(nl));
   BOOST_CHECK(wwwds9anl.isPartOf(wwwds9anl));
 
   BOOST_CHECK(!nl.isPartOf(wwwds9anl));