diff options
author | Jack Lloyd <[email protected]> | 2018-12-19 11:27:51 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-12-19 11:27:51 -0500 |
commit | d27d042bf811327a829729037c5e7f4fd71fdb9e (patch) | |
tree | 83b0ca5194a0bf58ba9388a7abe12f65b97d9371 | |
parent | 3782374933d00933558beeca0f45059029405986 (diff) |
Address a couple of Coverity false positives
Add tests for is_power_of_2
-rw-r--r-- | src/lib/utils/bit_ops.h | 2 | ||||
-rw-r--r-- | src/lib/utils/poly_dbl/poly_dbl.cpp | 18 | ||||
-rw-r--r-- | src/tests/test_utils.cpp | 48 | ||||
-rw-r--r-- | src/tests/tests.cpp | 1 |
4 files changed, 62 insertions, 7 deletions
diff --git a/src/lib/utils/bit_ops.h b/src/lib/utils/bit_ops.h index d781d64d5..c6cce8ae2 100644 --- a/src/lib/utils/bit_ops.h +++ b/src/lib/utils/bit_ops.h @@ -24,7 +24,7 @@ namespace Botan { template<typename T> inline constexpr bool is_power_of_2(T arg) { - return ((arg != 0 && arg != 1) && ((arg & (arg-1)) == 0)); + return ((arg != 0 && arg != 1) && ((arg & static_cast<T>(arg-1)) == 0)); } /** diff --git a/src/lib/utils/poly_dbl/poly_dbl.cpp b/src/lib/utils/poly_dbl/poly_dbl.cpp index 65cee9f01..618fce202 100644 --- a/src/lib/utils/poly_dbl/poly_dbl.cpp +++ b/src/lib/utils/poly_dbl/poly_dbl.cpp @@ -35,8 +35,13 @@ void poly_double(uint8_t out[], const uint8_t in[]) const uint64_t POLY = static_cast<uint64_t>(P); const uint64_t carry = POLY * (W[0] >> 63); - for(size_t i = 0; i != LIMBS - 1; ++i) - W[i] = (W[i] << 1) ^ (W[i+1] >> 63); + + if(LIMBS > 0) + { + for(size_t i = 0; i != LIMBS - 1; ++i) + W[i] = (W[i] << 1) ^ (W[i+1] >> 63); + } + W[LIMBS-1] = (W[LIMBS-1] << 1) ^ carry; copy_out_be(out, LIMBS*8, W); @@ -51,8 +56,13 @@ void poly_double_le(uint8_t out[], const uint8_t in[]) const uint64_t POLY = static_cast<uint64_t>(P); const uint64_t carry = POLY * (W[LIMBS-1] >> 63); - for(size_t i = 0; i != LIMBS - 1; ++i) - W[LIMBS-1-i] = (W[LIMBS-1-i] << 1) ^ (W[LIMBS-2-i] >> 63); + + if(LIMBS > 0) + { + for(size_t i = 0; i != LIMBS - 1; ++i) + W[LIMBS-1-i] = (W[LIMBS-1-i] << 1) ^ (W[LIMBS-2-i] >> 63); + } + W[0] = (W[0] << 1) ^ carry; copy_out_le(out, LIMBS*8, W); diff --git a/src/tests/test_utils.cpp b/src/tests/test_utils.cpp index 6708524b4..485d72a2a 100644 --- a/src/tests/test_utils.cpp +++ b/src/tests/test_utils.cpp @@ -1,5 +1,5 @@ /* -* (C) 2015 Jack Lloyd +* (C) 2015,2018 Jack Lloyd * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity * (C) 2017 René Korthaus, Rohde & Schwarz Cybersecurity * @@ -15,6 +15,7 @@ #include <botan/calendar.h> #include <botan/internal/rounding.h> #include <botan/internal/ct_utils.h> +#include <botan/internal/bit_ops.h> #include <botan/charset.h> #include <botan/parsing.h> #include <botan/version.h> @@ -236,6 +237,51 @@ class CT_Mask_Tests final : public Test BOTAN_REGISTER_TEST("ct_utils", CT_Mask_Tests); +class BitOps_Tests final : public Test + { + public: + std::vector<Test::Result> run() override + { + std::vector<Test::Result> results; + + results.push_back(test_power_of_2()); + + return results; + } + private: + template<typename T> + void test_power_of_2(Test::Result& result, T val, bool expected) + { + result.test_eq("power_of_2(" + std::to_string(val) + ")", Botan::is_power_of_2<T>(val), expected); + } + + Test::Result test_power_of_2() + { + Test::Result result("is_power_of_2"); + + test_power_of_2<uint32_t>(result, 0, false); + test_power_of_2<uint32_t>(result, 1, false); + test_power_of_2<uint32_t>(result, 2, true); + test_power_of_2<uint32_t>(result, 3, false); + test_power_of_2<uint32_t>(result, 0x8000, true); + test_power_of_2<uint32_t>(result, 0x8001, false); + test_power_of_2<uint32_t>(result, 0x8000000, true); + + test_power_of_2<uint64_t>(result, 0, false); + test_power_of_2<uint64_t>(result, 1, false); + test_power_of_2<uint64_t>(result, 2, true); + test_power_of_2<uint64_t>(result, 3, false); + test_power_of_2<uint64_t>(result, 0x8000, true); + test_power_of_2<uint64_t>(result, 0x8001, false); + test_power_of_2<uint64_t>(result, 0x8000000, true); + test_power_of_2<uint64_t>(result, 0x100000000000, true); + + return result; + } + }; + +BOTAN_REGISTER_TEST("bit_ops", BitOps_Tests); + #if defined(BOTAN_HAS_POLY_DBL) class Poly_Double_Tests final : public Text_Based_Test diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index 71c3e3726..2300dfaea 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -12,7 +12,6 @@ #include <botan/hex.h> #include <botan/parsing.h> #include <botan/internal/filesystem.h> -#include <botan/internal/bit_ops.h> #include <botan/internal/stl_util.h> #if defined(BOTAN_HAS_BIGINT) |