From: bert hubert Date: Wed, 11 Mar 2015 10:55:39 +0000 (+0100) Subject: Kees Monshouwer spotted a bug in our DNSName isParent() implementation and delivered... X-Git-Tag: dnsdist-1.0.0-alpha1~248^2~88^2~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3bd38afa8fb4d8a34a12952bd2016875e2d7cbc5;p=thirdparty%2Fpdns.git Kees Monshouwer spotted a bug in our DNSName isParent() implementation and delivered a fix too. Thank you kees! --- diff --git a/pdns/dnsname.cc b/pdns/dnsname.cc index be9821f1a1..78df576b85 100644 --- a/pdns/dnsname.cc +++ b/pdns/dnsname.cc @@ -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= (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) diff --git a/pdns/test-dnsname_cc.cc b/pdns/test-dnsname_cc.cc index 9eff64975a..f1122e0069 100644 --- a/pdns/test-dnsname_cc.cc +++ b/pdns/test-dnsname_cc.cc @@ -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));