BIND 10 trac2384, updated. 0acf96a8951dc132f064c28b12971f4060531f2f [2384] Test border cases

BIND 10 source code commits bind10-changes at lists.isc.org
Wed Nov 7 09:38:16 UTC 2012


The branch, trac2384 has been updated
       via  0acf96a8951dc132f064c28b12971f4060531f2f (commit)
       via  8ac89e17dd1f86a4eabd949b8cb7d62070dc91dd (commit)
       via  0c55100a70f0b68afd7880e2b1498f773fbff943 (commit)
       via  cc44ed781d1d4affac1ccfbeedb9ec29f7e138a0 (commit)
       via  acd2c55e6eafdb17c6d5fede86cbabef7b40e970 (commit)
       via  cb51ce143fd7127f886d04720933a23b633f7dd7 (commit)
      from  d265c83bdf38b1b136d447c2c1585a8f824c1c46 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 0acf96a8951dc132f064c28b12971f4060531f2f
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Wed Nov 7 10:30:59 2012 +0100

    [2384] Test border cases

commit 8ac89e17dd1f86a4eabd949b8cb7d62070dc91dd
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Wed Nov 7 10:21:06 2012 +0100

    [2384] Rearrange tests
    
    There were some non-fromText related tests in the fromText test case.
    Moving to separate test case.

commit 0c55100a70f0b68afd7880e2b1498f773fbff943
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Wed Nov 7 10:17:31 2012 +0100

    [2384] Better TTL range checking
    
    Also check partial values and wrap-arouds for numbers.

commit cc44ed781d1d4affac1ccfbeedb9ec29f7e138a0
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Wed Nov 7 10:01:56 2012 +0100

    [2384] Test case of reused unit

commit acd2c55e6eafdb17c6d5fede86cbabef7b40e970
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Wed Nov 7 09:59:56 2012 +0100

    [2384] Remove unnecessary cast

commit cb51ce143fd7127f886d04720933a23b633f7dd7
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Wed Nov 7 09:54:30 2012 +0100

    [2384] Show the broken TTL in exception messages

-----------------------------------------------------------------------

Summary of changes:
 src/lib/dns/rrttl.cc                |   22 +++++++++++++++++-----
 src/lib/dns/tests/rrttl_unittest.cc |   34 +++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 6 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/dns/rrttl.cc b/src/lib/dns/rrttl.cc
index 0924439..6234862 100644
--- a/src/lib/dns/rrttl.cc
+++ b/src/lib/dns/rrttl.cc
@@ -86,17 +86,29 @@ RRTTL::RRTTL(const std::string& ttlstr) {
                     }
                 }
                 if (!found) {
-                    isc_throw(InvalidRRTTL, "Unknown unit used: " << *unit);
+                    isc_throw(InvalidRRTTL, "Unknown unit used: " << *unit <<
+                              "in: " << ttlstr);
                 }
             }
             // Now extract the number, defaut to 1 if there's no digit
             if (unit == pos) {
-                isc_throw(InvalidRRTTL, "Missing number in TTL ");
+                isc_throw(InvalidRRTTL, "Missing number in TTL: " << ttlstr);
             }
             const int64_t value = boost::lexical_cast<int64_t>(string(pos,
                                                                       unit));
             // Add what we found
             val += multiply * value;
+            // The partial value is still in range (the value can only grow,
+            // so if we get out now, it won't get better).
+            //
+            // Any valid uint32_t number must have at most 10 digits. If it
+            // has more, it could wrap around the int64_t silently (at least
+            // in theory, some compilers seem to throw from lexical_cast).
+            if (unit - pos > 10 || value < 0 || val < 0 ||
+                val > 0xffffffff) {
+                isc_throw(InvalidRRTTL, "Part of TTL out of range: " <<
+                          ttlstr);
+            }
             // Move to after the unit (if any). But make sure not to increment
             // past end, which is, strictly speaking, illegal.
             pos = unit;
@@ -105,13 +117,13 @@ RRTTL::RRTTL(const std::string& ttlstr) {
             }
         }
     } catch (const boost::bad_lexical_cast&) {
-        isc_throw(InvalidRRTTL, "invalid TTL");
+        isc_throw(InvalidRRTTL, "invalid TTL: " << ttlstr);
     }
 
     if (val >= 0 && val <= 0xffffffff) {
-        ttlval_ = static_cast<uint32_t>(val);
+        ttlval_ = val;
     } else {
-        isc_throw(InvalidRRTTL, "invalid TTL");
+        isc_throw(InvalidRRTTL, "TTL out of range: " << ttlstr);
     }
 }
 
diff --git a/src/lib/dns/tests/rrttl_unittest.cc b/src/lib/dns/tests/rrttl_unittest.cc
index bf73c3a..03ab7a6 100644
--- a/src/lib/dns/tests/rrttl_unittest.cc
+++ b/src/lib/dns/tests/rrttl_unittest.cc
@@ -65,13 +65,20 @@ RRTTLTest::rrttlFactoryFromWire(const char* datafile) {
     return (RRTTL(buffer));
 }
 
-TEST_F(RRTTLTest, fromText) {
+TEST_F(RRTTLTest, getValue) {
     EXPECT_EQ(0, ttl_0.getValue());
     EXPECT_EQ(3600, ttl_1h.getValue());
     EXPECT_EQ(86400, ttl_1d.getValue());
     EXPECT_EQ(0x12345678, ttl_32bit.getValue());
     EXPECT_EQ(0xffffffff, ttl_max.getValue());
+}
+
+TEST_F(RRTTLTest, fromText) {
+    // Border cases
+    EXPECT_EQ(0, RRTTL("0").getValue());
+    EXPECT_EQ(4294967295, RRTTL("4294967295").getValue());
 
+    // Invalid cases
     EXPECT_THROW(RRTTL("0xdeadbeef"), InvalidRRTTL); // must be decimal
     EXPECT_THROW(RRTTL("-1"), InvalidRRTTL); // must be positive
     EXPECT_THROW(RRTTL("1.1"), InvalidRRTTL); // must be integer
@@ -99,12 +106,37 @@ TEST_F(RRTTLTest, fromTextUnit) {
     checkUnit(24 * 60 * 60, 'D');
     checkUnit(7 * 24 * 60 * 60, 'W');
 
+    // Some border cases
+    EXPECT_EQ(4294967295, RRTTL("4294967295S").getValue());
+    EXPECT_EQ(0, RRTTL("0W0D0H0M0S").getValue());
+    EXPECT_EQ(4294967295, RRTTL("1193046H1695S").getValue());
+
     // Now some compound ones. We allow any order (it would be much work to
     // check the order anyway). The last part can be without unit, in which
     // case it is considered seconds.
     EXPECT_EQ(60 * 60 + 3, RRTTL("1H3S").getValue());
     EXPECT_EQ(2 * 24 * 60 * 60 + 75 * 60 + 4, RRTTL("75M2D4").getValue());
 
+    // Awkward, but allowed case - the same unit used twice.
+    EXPECT_EQ(20 * 3600, RRTTL("12H8H").getValue());
+
+    // Negative number in part of the expression, but the total is positive.
+    // Rejected.
+    EXPECT_THROW(RRTTL("-1S1H"), InvalidRRTTL);
+
+    // Some things out of range in the ttl, but it wraps to number in range
+    // in int64_t. Should still not get fooled and reject it.
+
+    // First part out of range
+    EXPECT_THROW(RRTTL("9223372036854775807S9223372036854775807S2S"),
+                 InvalidRRTTL);
+    // Second part out of range, but it immediately wraps (2S+2^64-2S)
+    EXPECT_THROW(RRTTL("2S18446744073709551614S"),
+                 InvalidRRTTL);
+    // The whole thing wraps right away (2^64S)
+    EXPECT_THROW(RRTTL("18446744073709551616S"),
+                 InvalidRRTTL);
+
     // Missing before unit.
     EXPECT_THROW(RRTTL("W5H"), InvalidRRTTL);
     EXPECT_THROW(RRTTL("5hW"), InvalidRRTTL);



More information about the bind10-changes mailing list