From: Mukund Sivaraman Date: Thu, 28 Jun 2012 04:22:11 +0000 (+0530) Subject: [2052] Fix code for comparisons of non-absolute label sequences X-Git-Tag: trac2351_base~197^2~3^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c09a49cd3d1cd3498a7c7fb778abbe5485989e65;p=thirdparty%2Fkea.git [2052] Fix code for comparisons of non-absolute label sequences --- diff --git a/src/lib/dns/labelsequence.cc b/src/lib/dns/labelsequence.cc index 817aa7c404..76aee18943 100644 --- a/src/lib/dns/labelsequence.cc +++ b/src/lib/dns/labelsequence.cc @@ -74,13 +74,15 @@ LabelSequence::equals(const LabelSequence& other, bool case_sensitive) const { NameComparisonResult LabelSequence::compare(const LabelSequence& other, bool case_sensitive) const { - if ((!isAbsolute()) || (!other.isAbsolute())) { + if (isAbsolute() ^ other.isAbsolute()) { return (NameComparisonResult(0, 0, NameComparisonResult::NONE)); } return (name_.partial_compare(other.name_, first_label_, other.first_label_, + last_label_, + other.last_label_, case_sensitive)); } diff --git a/src/lib/dns/name.cc b/src/lib/dns/name.cc index 286472866c..87bf41e9a3 100644 --- a/src/lib/dns/name.cc +++ b/src/lib/dns/name.cc @@ -507,26 +507,33 @@ Name::toText(bool omit_final_dot) const { NameComparisonResult Name::compare(const Name& other) const { - return (partial_compare(other, 0, 0)); + return (partial_compare(other, 0, 0, labelcount_, other.labelcount_)); } NameComparisonResult Name::partial_compare(const Name& other, unsigned int first_label, unsigned int first_label_other, + unsigned int last_label, + unsigned int last_label_other, bool case_sensitive) const { // Determine the relative ordering under the DNSSEC order relation of // 'this' and 'other', and also determine the hierarchical relationship // of the names. + if ((first_label > last_label) || + (first_label_other > last_label_other)) { + isc_throw(BadValue, "Bad label index ranges were passed"); + } + if ((first_label > labelcount_) || (first_label_other > other.labelcount_)) { isc_throw(BadValue, "Bad first label indices were passed"); } unsigned int nlabels = 0; - int l1 = labelcount_ - first_label; - int l2 = other.labelcount_ - first_label_other; + int l1 = last_label - first_label; + int l2 = last_label_other - first_label_other; int ldiff = (int)l1 - (int)l2; unsigned int l = (ldiff < 0) ? l1 : l2; @@ -558,16 +565,30 @@ Name::partial_compare(const Name& other, } if (chdiff != 0) { - return (NameComparisonResult(chdiff, nlabels, - NameComparisonResult::COMMONANCESTOR)); + if ((nlabels == 0) && + ((last_label < labelcount_) || + (last_label_other < other.labelcount_))) { + return (NameComparisonResult(chdiff, 0, + NameComparisonResult::NONE)); + } else { + return (NameComparisonResult(chdiff, nlabels, + NameComparisonResult::COMMONANCESTOR)); + } } --count; ++pos1; ++pos2; } if (cdiff != 0) { + if ((nlabels == 0) && + ((last_label < labelcount_) || + (last_label_other < other.labelcount_))) { + return (NameComparisonResult(cdiff, 0, + NameComparisonResult::NONE)); + } else { return (NameComparisonResult(cdiff, nlabels, - NameComparisonResult::COMMONANCESTOR)); + NameComparisonResult::COMMONANCESTOR)); + } } ++nlabels; } diff --git a/src/lib/dns/name.h b/src/lib/dns/name.h index 728f3a1fb3..d6fb29493a 100644 --- a/src/lib/dns/name.h +++ b/src/lib/dns/name.h @@ -411,16 +411,22 @@ private: /// indices are passed. /// /// \param other the right-hand operand to compare against. - /// \param first_label the leftmost label of Name to + /// \param first_label the left-most label of Name to /// begin comparing from. - /// \param first_label_other the leftmost label of + /// \param first_label_other the left-most label of /// other to begin comparing from. + /// \param last_label the right-most label of Name to + /// end comparing at. + /// \param last_label_other the right-most label of + /// other to end comparing at. /// \param case_sensitive If true, comparison is case-insensitive /// \return a NameComparisonResult object representing the /// comparison result. NameComparisonResult partial_compare(const Name& other, unsigned int first_label, unsigned int first_label_other, + unsigned int last_label, + unsigned int last_label_other, bool case_sensitive = false) const; public: