Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update coverity scan #404

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/example013_ecdsa_sign_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ namespace example013_ecdsa
* sexatuple_sint_type(inverse_mod(k, curve_n()))
);

s = double_sint_type(divmod(num, n).second);
s = std::move(double_sint_type(divmod(num, n).second));
}

return
Expand All @@ -738,14 +738,14 @@ namespace example013_ecdsa
MsgIteratorType msg_last,
const std::pair<uint_type, uint_type>& sig) -> bool
{
const auto w = sexatuple_sint_type(inverse_mod(sig.second, curve_n()));
const sexatuple_sint_type w(inverse_mod(sig.second, curve_n()));

const auto n = sexatuple_sint_type(curve_n());
const sexatuple_sint_type n(curve_n());

const auto z = hash_message(msg_first, msg_last);

const auto u1 = double_sint_type(divmod(sexatuple_sint_type(z) * w, n).second);
const auto u2 = double_sint_type(divmod(sexatuple_sint_type(sig.first) * w, n).second);
const double_sint_type u1(divmod(sexatuple_sint_type(z) * w, n).second);
const double_sint_type u2(divmod(sexatuple_sint_type(sig.first) * w, n).second);

const auto pt =
point_add
Expand Down
110 changes: 49 additions & 61 deletions math/wide_integer/uintwide_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,10 @@

} // namespace iterator_detail

// Forward declaration of:
// Use a local, constexpr, unsafe implementation of the abs-function.
template<typename ArithmeticType>
constexpr auto abs_unsafe(ArithmeticType val) -> ArithmeticType
{
return ((val > static_cast<int>(INT8_C(0))) ? val : -val);
}
constexpr auto abs_unsafe(ArithmeticType val) -> ArithmeticType;

// Use a local, constexpr, unsafe implementation of the fill-function.
template<typename DestinationIterator,
Expand Down Expand Up @@ -2411,7 +2409,7 @@
}

// Move constructor.
constexpr uintwide_t(uintwide_t&&) noexcept = default;
constexpr uintwide_t(uintwide_t&&) noexcept = default; // LCOV_EXCL_LINE

// Move-like constructor from the other signed-ness type.
// This constructor is non-explicit because it is a trivial conversion.
Expand All @@ -2421,7 +2419,7 @@
: values(static_cast<representation_type&&>(other.values)) { }

// Assignment operator.
constexpr auto operator=(const uintwide_t&) -> uintwide_t& = default;
constexpr auto operator=(const uintwide_t&) -> uintwide_t& = default; // LCOV_EXCL_LINE

// Assignment operator from the other signed-ness type.
template<const bool OtherIsSigned,
Expand All @@ -2434,7 +2432,7 @@
}

// Trivial move assignment operator.
constexpr auto operator=(uintwide_t&& other) noexcept -> uintwide_t& = default;
constexpr auto operator=(uintwide_t&& other) noexcept -> uintwide_t& = default; // LCOV_EXCL_LINE

// Trivial move assignment operator from the other signed-ness type.
template<const bool OtherIsSigned,
Expand Down Expand Up @@ -2486,29 +2484,13 @@
: static_cast<size_t>(other_wide_integer_type::number_of_limbs)
);

other_wide_integer_type other;
other_wide_integer_type other { };

if(!this_is_neg)
{
detail::copy_unsafe(crepresentation().cbegin(),
detail::advance_and_point(crepresentation().cbegin(), sz),
other.values.begin());

// TBD: Can proper/better template specialization remove the need for if constexpr here?
#if ( (defined(_MSC_VER) && ((_MSC_VER >= 1900) && defined(_HAS_CXX17) && (_HAS_CXX17 != 0))) \
|| (defined(__cplusplus) && ((__cplusplus >= 201703L) || defined(__cpp_if_constexpr))) \
|| (defined(_MSVC_LANG) && ((_MSVC_LANG >= 201703L) || defined(__cpp_if_constexpr))))
if constexpr(Width2 < OtherWidth2)
#else
#if defined(_MSC_VER)
#pragma warning(disable : 4127)
if(Width2 < OtherWidth2)
#pragma warning(default:4127)
#endif
#endif
{
detail::fill_unsafe(detail::advance_and_point(other.values.begin(), sz), other.values.end(), static_cast<limb_type>(UINT8_C(0)));
}
}
else
{
Expand All @@ -2520,21 +2502,6 @@
detail::advance_and_point(uv.crepresentation().cbegin(), sz),
other.values.begin());

// TBD: Can proper/better template specialization remove the need for if constexpr here?
#if ( (defined(_MSC_VER) && ((_MSC_VER >= 1900) && defined(_HAS_CXX17) && (_HAS_CXX17 != 0))) \
|| (defined(__cplusplus) && ((__cplusplus >= 201703L) || defined(__cpp_if_constexpr))))
if constexpr(Width2 < OtherWidth2)
#else
#if defined(_MSC_VER)
#pragma warning(disable : 4127)
if(Width2 < OtherWidth2)
#pragma warning(default:4127)
#endif
#endif
{
detail::fill_unsafe(detail::advance_and_point(other.values.begin(), sz), other.values.end(), static_cast<limb_type>(UINT8_C(0)));
}

other.negate();
}

Expand Down Expand Up @@ -3009,7 +2976,7 @@

if(base_rep == static_cast<std::uint_fast8_t>(UINT8_C(8)))
{
uintwide_t t(*this);
uintwide_t<my_width2, limb_type, AllocatorType, false> t(*this);

const auto mask = static_cast<limb_type>(static_cast<std::uint8_t>(0x7U));

Expand Down Expand Up @@ -3038,17 +3005,15 @@
}
else
{
uintwide_t<my_width2, limb_type, AllocatorType, false> tu(t);

while(!tu.is_zero()) // NOLINT(altera-id-dependent-backward-branch)
while(!t.is_zero()) // NOLINT(altera-id-dependent-backward-branch)
{
auto c = static_cast<char>(*tu.values.cbegin() & mask);
auto c = static_cast<char>(*t.values.cbegin() & mask);

if(c <= static_cast<char>(INT8_C(8))) { c = static_cast<char>(c + static_cast<char>(INT8_C(0x30))); }

str_temp[static_cast<typename string_storage_oct_type::size_type>(--pos)] = c;

tu >>= static_cast<unsigned>(UINT8_C(3));
t >>= static_cast<unsigned>(UINT8_C(3));
}
}

Expand Down Expand Up @@ -6570,17 +6535,17 @@
if(u == v)
{ // NOLINT(bugprone-branch-clone)
// This handles cases having (u = v) and also (u = v = 0).
result = u; // LCOV_EXCL_LINE
result = std::move(u); // LCOV_EXCL_LINE
}
else if((static_cast<local_ushort_type>(v) == static_cast<local_ushort_type>(UINT8_C(0))) && (v == static_cast<unsigned>(UINT8_C(0))))
{
// This handles cases having (v = 0) with (u != 0).
result = u; // LCOV_EXCL_LINE
result = std::move(u); // LCOV_EXCL_LINE
}
else if((static_cast<local_ushort_type>(u) == static_cast<local_ushort_type>(UINT8_C(0))) && (u == static_cast<unsigned>(UINT8_C(0))))
{
// This handles cases having (u = 0) with (v != 0).
result = v; // LCOV_EXCL_LINE
result = std::move(v); // LCOV_EXCL_LINE
}
else
{
Expand Down Expand Up @@ -6644,8 +6609,13 @@

result = (u << left_shift_amount);
}

if(u_is_neg != v_is_neg)
{
result.negate();
}

return ((u_is_neg == v_is_neg) ? result : -result);
return result;
}

template<typename UnsignedShortType>
Expand All @@ -6657,6 +6627,24 @@

namespace detail {

// Use a local, constexpr, unsafe implementation of the abs-function.
template<typename ArithmeticType>
constexpr auto abs_unsafe(ArithmeticType val) -> ArithmeticType
{
if(val > static_cast<int>(INT8_C(0)))
{
return val;
}
else // NOLINT(llvm-else-after-return,readability-else-after-return)
{
ArithmeticType val_unsigned(val);

val_unsigned.negate();

return val_unsigned;
}
}

template<typename IntegerType>
constexpr auto lcm_impl(const IntegerType& a, const IntegerType& b) -> IntegerType
{
Expand Down Expand Up @@ -7766,12 +7754,12 @@
using local_result_value_type = typename detail::iterator_detail::iterator_traits<local_result_iterator_type>::value_type;
using local_input_value_type = typename local_unsigned_wide_integer_type::representation_type::value_type;

const auto val_unsigned =
(
(!uintwide_t<Width2, LimbType, AllocatorType, IsSigned>::is_neg(val))
? static_cast<local_unsigned_wide_integer_type>(val)
: static_cast<local_unsigned_wide_integer_type>(-val)
);
local_unsigned_wide_integer_type val_unsigned(val);

if(uintwide_t<Width2, LimbType, AllocatorType, IsSigned>::is_neg(val))
{
val_unsigned.negate();
}

static_assert(std::numeric_limits<local_result_value_type>::digits == std::numeric_limits<local_input_value_type>::digits,
"Error: Erroneous mismatch for input element width and result uintwide_t limb width");
Expand Down Expand Up @@ -7880,12 +7868,12 @@
using local_result_value_type = typename detail::iterator_detail::iterator_traits<local_result_iterator_type>::value_type;
using local_input_value_type = typename local_unsigned_wide_integer_type::representation_type::value_type;

const auto val_unsigned =
(
(!uintwide_t<Width2, LimbType, AllocatorType, IsSigned>::is_neg(val))
? static_cast<local_unsigned_wide_integer_type>(val)
: static_cast<local_unsigned_wide_integer_type>(-val)
);
local_unsigned_wide_integer_type val_unsigned(val);

if(uintwide_t<Width2, LimbType, AllocatorType, IsSigned>::is_neg(val))
{
val_unsigned.negate();
}

static_assert(std::numeric_limits<local_result_value_type>::digits != std::numeric_limits<local_input_value_type>::digits,
"Error: Erroneous match for input element width and result uintwide_t limb width");
Expand Down
2 changes: 1 addition & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// make gcov -f make_gcov_01_generic.gmk --jobs=8 MY_ALL_COV=0 MY_BOOST_ROOT=/mnt/c/boost/boost_1_83_0 MY_CC=g++

// cd /mnt/c/Users/User/Documents/Ks/PC_Software/NumericalPrograms/ExtendedNumberTypes/wide_integer
// PATH=/home/chris/local/coverity/cov-analysis-linux64-2022.6.0/bin:$PATH
// PATH=/home/chris/coverity/cov-analysis-linux64-2023.6.2/bin:$PATH
// cov-build --dir cov-int g++ -finline-functions -march=native -mtune=native -O3 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -std=c++14 -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -DWIDE_INTEGER_HAS_MUL_8_BY_8_UNROLL -I. -I/mnt/c/boost/boost_1_83_0 -pthread -lpthread test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp examples/example000a_builtin_convert.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp examples/example013_ecdsa_sign_verify.cpp examples/example014_pi_spigot_wide.cpp -o wide_integer.exe
// tar caf wide-integer.bz2 cov-int

Expand Down