From: Otto Moerbeek Date: Wed, 17 Jul 2019 08:37:19 +0000 (+0200) Subject: Fix the rfc1982LessThan template, it only works properly if the cast is done to a... X-Git-Tag: dnsdist-1.4.0-rc1~39^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9a3631021af59677d8522b7bb9ab20560a557239;p=pdns Fix the rfc1982LessThan template, it only works properly if the cast is done to a same-sized type. If we use it for uint8_t or uint64_t it breaks currenty. Add unit tests while there. --- diff --git a/pdns/misc.hh b/pdns/misc.hh index ca327e092..0ead1e41b 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -112,7 +112,8 @@ stringtok (Container &container, string const &in, template bool rfc1982LessThan(T a, T b) { - return ((signed)(a - b)) < 0; + typedef typename std::make_signed::type signed_t; + return static_cast(a - b) < 0; } // fills container with ranges, so {posbegin,posend} diff --git a/pdns/test-misc_hh.cc b/pdns/test-misc_hh.cc index 081661afc..499d6ecf0 100644 --- a/pdns/test-misc_hh.cc +++ b/pdns/test-misc_hh.cc @@ -176,5 +176,31 @@ BOOST_AUTO_TEST_CASE(test_SimpleMatch) { BOOST_CHECK_EQUAL(SimpleMatch("abc*").match(std::string("abc")), true); } +template 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(0, 1)); + BOOST_CHECK(rfc1982check(0, 44)); + BOOST_CHECK(rfc1982check(0, 100)); + BOOST_CHECK(rfc1982check(44, 100)); + BOOST_CHECK(rfc1982check(100, 200)); + BOOST_CHECK(rfc1982check(200, 255)); + BOOST_CHECK(rfc1982check(255, 0)); + BOOST_CHECK(rfc1982check(255, 100)); + BOOST_CHECK(rfc1982check(200, 0)); + BOOST_CHECK(rfc1982check(200, 44)); + + BOOST_CHECK(rfc1982check(0, 1)); + BOOST_CHECK(rfc1982check(UINT32_MAX-10, 1)); + BOOST_CHECK(rfc1982check(UINT32_MAX/2, UINT32_MAX-10)); + + BOOST_CHECK(rfc1982check(0, 1)); + BOOST_CHECK(rfc1982check(UINT64_MAX-10, 1)); + BOOST_CHECK(rfc1982check(UINT64_MAX/2, UINT64_MAX-10)); +} + BOOST_AUTO_TEST_SUITE_END()