Browse Source

[1638] corrected salt comparison logic in NSEC3PARAM

JINMEI Tatuya 13 years ago
parent
commit
c356ad2e53

+ 9 - 6
src/lib/dns/rdata/generic/nsec3param_51.cc

@@ -182,15 +182,18 @@ NSEC3PARAM::compare(const Rdata& other) const {
         return (impl_->iterations_ < other_param.impl_->iterations_ ? -1 : 1);
     }
 
-    size_t this_len = impl_->salt_.size();
-    size_t other_len = other_param.impl_->salt_.size();
-    size_t cmplen = min(this_len, other_len);
-    int cmp = memcmp(&impl_->salt_[0], &other_param.impl_->salt_[0],
-                     cmplen);
+    const size_t this_len = impl_->salt_.size();
+    const size_t other_len = other_param.impl_->salt_.size();
+    if (this_len != other_len) {
+        return (this_len - other_len);
+    }
+    const size_t cmplen = min(this_len, other_len);
+    const int cmp = (cmplen == 0) ? 0 :
+        memcmp(&impl_->salt_.at(0), &other_param.impl_->salt_.at(0), cmplen);
     if (cmp != 0) {
         return (cmp);
     } else {
-        return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
+        return (this_len - other_len);
     }
 }
 

+ 1 - 1
src/lib/dns/tests/rdata_nsec3param_like_unittest.cc

@@ -237,7 +237,7 @@ TYPED_TEST(NSEC3PARAMLikeTest, DISABLED_toWire) {
                 this->getWireFilePrefix() + "fromWire13.wire");
 }
 
-TYPED_TEST(NSEC3PARAMLikeTest, DISABLED_compare) {
+TYPED_TEST(NSEC3PARAMLikeTest, compare) {
     // test RDATAs, sorted in the ascendent order.
     this->compare_set.push_back(this->fromText("0 0 0 D399EAAB" +
                                                this->getCommonText()));

+ 12 - 0
src/lib/dns/tests/rdata_nsec3param_unittest.cc

@@ -96,4 +96,16 @@ TEST_F(Rdata_NSEC3PARAM_Test, assign) {
     EXPECT_EQ(0, rdata_nsec3param.compare(other_nsec3param));
 }
 
+TEST_F(Rdata_NSEC3PARAM_Test, compare) {
+    // trivial case: self equivalence
+    EXPECT_EQ(0, generic::NSEC3PARAM(nsec3param_txt).
+              compare(generic::NSEC3PARAM(nsec3param_txt)));
+    EXPECT_EQ(0, generic::NSEC3PARAM("1 1 1 -").
+              compare(generic::NSEC3PARAM("1 1 1 -")));
+
+    // comparison attempt between incompatible RR types should be rejected
+    EXPECT_THROW(generic::NSEC3PARAM(nsec3param_txt).compare(*rdata_nomatch),
+                 bad_cast);
+}
+
 }