]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2052] Fix code for comparisons of non-absolute label sequences
authorMukund Sivaraman <muks@isc.org>
Thu, 28 Jun 2012 04:22:11 +0000 (09:52 +0530)
committerMukund Sivaraman <muks@isc.org>
Thu, 28 Jun 2012 04:22:11 +0000 (09:52 +0530)
src/lib/dns/labelsequence.cc
src/lib/dns/name.cc
src/lib/dns/name.h

index 817aa7c404a8a72d838dac4cdba18fb95965a93f..76aee189438bb49bd168129c228499359bcf13d8 100644 (file)
@@ -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));
 }
 
index 286472866c50223757cc76aa3e24cf5acd0fc71d..87bf41e9a3016f109faadab28e96dd89ea5deebc 100644 (file)
@@ -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;
     }
index 728f3a1fb3a2b1bf54ecc1764a4ee4c9ac39ed98..d6fb29493a5f90bc45413a332453894779b5d033 100644 (file)
@@ -411,16 +411,22 @@ private:
     /// indices are passed.
     ///
     /// \param other the right-hand operand to compare against.
-    /// \param first_label the leftmost label of <code>Name</code> to
+    /// \param first_label the left-most label of <code>Name</code> to
     /// begin comparing from.
-    /// \param first_label_other the leftmost label of
+    /// \param first_label_other the left-most label of
     /// <code>other</code> to begin comparing from.
+    /// \param last_label the right-most label of <code>Name</code> to
+    /// end comparing at.
+    /// \param last_label_other the right-most label of
+    /// <code>other</code> to end comparing at.
     /// \param case_sensitive If true, comparison is case-insensitive
     /// \return a <code>NameComparisonResult</code> 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: