]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3068] fix prefixesInRange after uint128_t was moved to checked_uint128_t
authorAndrei Pavel <andrei@isc.org>
Mon, 2 Oct 2023 14:03:09 +0000 (17:03 +0300)
committerAndrei Pavel <andrei@isc.org>
Thu, 5 Oct 2023 14:40:02 +0000 (17:40 +0300)
src/lib/asiolink/addr_utilities.cc
src/lib/asiolink/tests/addr_utilities_unittest.cc

index 9dfbf0b244bbb80fb8b66ff5b73a3141de893efb..5b03349aa1ebba106bd4c1eb590fbb2ae57be238 100644 (file)
@@ -9,9 +9,9 @@
 #include <asiolink/addr_utilities.h>
 #include <exceptions/exceptions.h>
 
-#include <vector>
+#include <cstring>
 #include <limits>
-#include <string.h>
+#include <vector>
 
 using namespace isc;
 using namespace isc::asiolink;
@@ -357,8 +357,9 @@ uint128_t prefixesInRange(const uint8_t pool_len, const uint8_t delegated_len) {
     uint8_t const count(delegated_len - pool_len);
 
     if (count == 128) {
-        // One off is the best we can do, unless we promote to uint256_t.
-        return uint128_t(-1);
+        // UINT128_MAX is one off from the real value, but it is the best we
+        // can do, unless we promote to uint256_t.
+        return std::numeric_limits<uint128_t>::max();
     }
 
     return (uint128_t(1) << count);
index 30a98170da0d2e46d013f6aabbbfc81dd44a6949..4db67cd5c50c6e469d1fc53dc71cc4a77f490d7a 100644 (file)
 
 #include <gtest/gtest.h>
 
+#include <cstdint>
+#include <cstdlib>
+#include <limits>
 #include <vector>
 
-#include <stdint.h>
-#include <stdlib.h>
-
 using namespace std;
 using namespace isc::asiolink;
 using namespace isc::util;
@@ -346,27 +346,42 @@ TEST(AddrUtilitiesTest, prefixLengthFromRange6) {
 
 // Checks if prefixInRange returns valid number of prefixes in specified range.
 TEST(AddrUtilitiesTest, prefixesInRange) {
-
     // How many /64 prefixes are in /64 pool?
-    EXPECT_EQ(1, prefixesInRange(64, 64));
+    EXPECT_NO_THROW({
+        EXPECT_EQ(1, prefixesInRange(64, 64));
+    });
 
     // How many /63 prefixes are in /64 pool?
-    EXPECT_EQ(2, prefixesInRange(63, 64));
+    EXPECT_NO_THROW({
+        EXPECT_EQ(2, prefixesInRange(63, 64));
+    });
 
     // How many /64 prefixes are in /48 pool?
-    EXPECT_EQ(65536, prefixesInRange(48, 64));
+    EXPECT_NO_THROW({
+        EXPECT_EQ(65536, prefixesInRange(48, 64));
+    });
 
     // How many /127 prefixes are in /64 pool?
-    EXPECT_EQ(uint64_t(9223372036854775808ull), prefixesInRange(64, 127));
+    EXPECT_NO_THROW({
+        EXPECT_EQ(uint64_t(9223372036854775808ull), prefixesInRange(64, 127));
+    });
 
     // How many /128 prefixes are in /64 pool?
-    EXPECT_EQ(uint128_t(1) << 64, prefixesInRange(64, 128));
-
-    // Let's go overboard again. How many IPv6 addresses are there?
-    EXPECT_EQ(uint128_t(1) << 127, prefixesInRange(1, 128));
-
-    // Let's go overboard again. How many IPv6 addresses are there?
-    EXPECT_EQ(uint128_t(-1), prefixesInRange(0, 128));
+    EXPECT_NO_THROW({
+        EXPECT_EQ(uint128_t(1) << 64, prefixesInRange(64, 128));
+    });
+
+    // Let's go overboard again. How many IPv6 addresses are there in a /1?
+    EXPECT_NO_THROW({
+        EXPECT_EQ(uint128_t(1) << 127, prefixesInRange(1, 128));
+    });
+
+    // Let's go overboard again. How many IPv6 addresses are there? The result is
+    // one off from the real value, but it's preferred rather than having an
+    // overflow_error thrown.
+    EXPECT_NO_THROW({
+        EXPECT_EQ(numeric_limits<uint128_t>::max(), prefixesInRange(0, 128));
+    });
 }
 
 // Checks the function which finds an IPv4 address from input address and offset.