Browse Source

[2384] Little bit more range checking

There was a gap when one thing could overflow to negative numbers.
Michal 'vorner' Vaner 12 years ago
parent
commit
4d514f5b0e
2 changed files with 6 additions and 5 deletions
  1. 2 1
      src/lib/dns/rrttl.cc
  2. 4 4
      src/lib/dns/tests/rrttl_unittest.cc

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

@@ -116,7 +116,8 @@ RRTTL::RRTTL(const std::string& ttlstr) {
             // Check the partial value is still in range (the value can only
             // Check the partial value is still in range (the value can only
             // grow, so if we get out of range now, it won't get better, so
             // grow, so if we get out of range now, it won't get better, so
             // there's no need to continue).
             // there's no need to continue).
-            if (value < 0 || val < 0 || val > 0xffffffff) {
+            if (value < 0 || value > 0xffffffff || val < 0 ||
+                val > 0xffffffff) {
                 isc_throw(InvalidRRTTL, "Part of TTL out of range: " <<
                 isc_throw(InvalidRRTTL, "Part of TTL out of range: " <<
                           ttlstr);
                           ttlstr);
             }
             }

+ 4 - 4
src/lib/dns/tests/rrttl_unittest.cc

@@ -131,11 +131,11 @@ TEST_F(RRTTLTest, fromTextUnit) {
     EXPECT_THROW(RRTTL("9223372036854775807S9223372036854775807S2S"),
     EXPECT_THROW(RRTTL("9223372036854775807S9223372036854775807S2S"),
                  InvalidRRTTL);
                  InvalidRRTTL);
     // Second part out of range, but it immediately wraps (2S+2^64-2S)
     // Second part out of range, but it immediately wraps (2S+2^64-2S)
-    EXPECT_THROW(RRTTL("2S18446744073709551614S"),
-                 InvalidRRTTL);
+    EXPECT_THROW(RRTTL("2S18446744073709551614S"), InvalidRRTTL);
     // The whole thing wraps right away (2^64S)
     // The whole thing wraps right away (2^64S)
-    EXPECT_THROW(RRTTL("18446744073709551616S"),
-                 InvalidRRTTL);
+    EXPECT_THROW(RRTTL("18446744073709551616S"), InvalidRRTTL);
+    // Second part out of range, and will become negative with the unit,
+    EXPECT_THROW(RRTTL("256S307445734561825856M"), InvalidRRTTL);
 
 
     // Missing before unit.
     // Missing before unit.
     EXPECT_THROW(RRTTL("W5H"), InvalidRRTTL);
     EXPECT_THROW(RRTTL("W5H"), InvalidRRTTL);