]> granicus.if.org Git - pdns/commitdiff
Fix the rfc1982LessThan template, it only works properly if the cast is done to a...
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 17 Jul 2019 08:37:19 +0000 (10:37 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 17 Jul 2019 08:37:19 +0000 (10:37 +0200)
If we use it for uint8_t or uint64_t it breaks currenty.  Add unit tests while there.

pdns/misc.hh
pdns/test-misc_hh.cc

index ca327e0920fa32d15fded37da04f68f47d5aa622..0ead1e41baf62be1f2f489cd05989721fafaed2d 100644 (file)
@@ -112,7 +112,8 @@ stringtok (Container &container, string const &in,
 
 template<typename T> bool rfc1982LessThan(T a, T b)
 {
-  return ((signed)(a - b)) < 0;
+  typedef typename std::make_signed<T>::type signed_t;
+  return static_cast<signed_t>(a - b) < 0;
 }
 
 // fills container with ranges, so {posbegin,posend}
index 081661afc3c752b419cb2dc3abcf0324a88b6a1a..499d6ecf0f0224fc24b2c9a29c2c5cf11e77b720 100644 (file)
@@ -176,5 +176,31 @@ BOOST_AUTO_TEST_CASE(test_SimpleMatch) {
   BOOST_CHECK_EQUAL(SimpleMatch("abc*").match(std::string("abc")), true);
 }
 
+template<typename T> bool rfc1982check(T x, T y) {
+  return rfc1982LessThan(x, y);
+}
+
+BOOST_AUTO_TEST_CASE(test_rfc1982LessThan) {
+  // The test cases from rfc1982 section 5.2
+  BOOST_CHECK(rfc1982check<uint8_t>(0, 1));
+  BOOST_CHECK(rfc1982check<uint8_t>(0, 44));
+  BOOST_CHECK(rfc1982check<uint8_t>(0, 100));
+  BOOST_CHECK(rfc1982check<uint8_t>(44, 100));
+  BOOST_CHECK(rfc1982check<uint8_t>(100, 200));
+  BOOST_CHECK(rfc1982check<uint8_t>(200, 255));
+  BOOST_CHECK(rfc1982check<uint8_t>(255, 0));
+  BOOST_CHECK(rfc1982check<uint8_t>(255, 100));
+  BOOST_CHECK(rfc1982check<uint8_t>(200, 0));
+  BOOST_CHECK(rfc1982check<uint8_t>(200, 44));
+
+  BOOST_CHECK(rfc1982check<uint32_t>(0, 1));
+  BOOST_CHECK(rfc1982check<uint32_t>(UINT32_MAX-10, 1));
+  BOOST_CHECK(rfc1982check<uint32_t>(UINT32_MAX/2, UINT32_MAX-10));
+
+  BOOST_CHECK(rfc1982check<uint64_t>(0, 1));
+  BOOST_CHECK(rfc1982check<uint64_t>(UINT64_MAX-10, 1));
+  BOOST_CHECK(rfc1982check<uint64_t>(UINT64_MAX/2, UINT64_MAX-10));
+}
+
 BOOST_AUTO_TEST_SUITE_END()