Parcourir la source

[2052] Fix code for comparisons of non-absolute label sequences

Mukund Sivaraman il y a 13 ans
Parent
commit
c09a49cd3d
3 fichiers modifiés avec 38 ajouts et 9 suppressions
  1. 3 1
      src/lib/dns/labelsequence.cc
  2. 27 6
      src/lib/dns/name.cc
  3. 8 2
      src/lib/dns/name.h

+ 3 - 1
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));
 }
 

+ 27 - 6
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;
     }

+ 8 - 2
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 <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: