Browse Source

[2384] Disallow empty TTLs and units-only TTLs

These are questionable and bind9 does not support them. Even the units
are bind extension of some kind probably.
Michal 'vorner' Vaner 12 years ago
parent
commit
d265c83bdf
2 changed files with 13 additions and 5 deletions
  1. 7 2
      src/lib/dns/rrttl.cc
  2. 6 3
      src/lib/dns/tests/rrttl_unittest.cc

+ 7 - 2
src/lib/dns/rrttl.cc

@@ -58,6 +58,9 @@ namespace isc {
 namespace dns {
 
 RRTTL::RRTTL(const std::string& ttlstr) {
+    if (ttlstr.empty()) {
+        isc_throw(InvalidRRTTL, "Empty TTL string");
+    }
     // We use a larger data type during the computation. This is because
     // some compilers don't fail when out of range, so we check the range
     // ourselves later.
@@ -87,8 +90,10 @@ RRTTL::RRTTL(const std::string& ttlstr) {
                 }
             }
             // Now extract the number, defaut to 1 if there's no digit
-            const int64_t value = (unit == pos) ? 1 :
-                                  boost::lexical_cast<int64_t>(string(pos,
+            if (unit == pos) {
+                isc_throw(InvalidRRTTL, "Missing number in TTL ");
+            }
+            const int64_t value = boost::lexical_cast<int64_t>(string(pos,
                                                                       unit));
             // Add what we found
             val += multiply * value;

+ 6 - 3
src/lib/dns/tests/rrttl_unittest.cc

@@ -105,9 +105,12 @@ TEST_F(RRTTLTest, fromTextUnit) {
     EXPECT_EQ(60 * 60 + 3, RRTTL("1H3S").getValue());
     EXPECT_EQ(2 * 24 * 60 * 60 + 75 * 60 + 4, RRTTL("75M2D4").getValue());
 
-    // Missing number is taken as 1 (or should we disallow that?)
-    EXPECT_EQ(7 * 24 * 60 * 60 + 5 * 60 * 60, RRTTL("W5H").getValue());
-    EXPECT_EQ(7 * 24 * 60 * 60 + 5 * 60 * 60, RRTTL("5hW").getValue());
+    // Missing before unit.
+    EXPECT_THROW(RRTTL("W5H"), InvalidRRTTL);
+    EXPECT_THROW(RRTTL("5hW"), InvalidRRTTL);
+
+    // Empty string is not allowed
+    EXPECT_THROW(RRTTL(""), InvalidRRTTL);
 
     // There are some wrong units
     EXPECT_THROW(RRTTL("13X"), InvalidRRTTL);