Skip to content

Commit

Permalink
chore: cppcheck remove cstyled casts
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel committed Mar 7, 2023
1 parent 6b1ce14 commit 87a20ac
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 28 deletions.
9 changes: 5 additions & 4 deletions CMakeModules/cppcheck.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ else ()
--suppress=localMutex:${PROJECT_SOURCE_DIR}/*/main.cpp
# Just style warning
--suppress=unmatchedSuppression:${PROJECT_SOURCE_DIR}/*/final_chain.cpp
# graphql generated
--suppress=shadowVariable:${PROJECT_SOURCE_DIR}/*/TaraxaSchema.*

--suppress=cstyleCast
# exclude graphql generated
-i ${PROJECT_SOURCE_DIR}/libraries/core_libs/network/graphql/gen/
# messy files
--suppress=cstyleCast:${PROJECT_SOURCE_DIR}/*/vector_ref.h
--suppress=cstyleCast:${PROJECT_SOURCE_DIR}/*/Common.h

# Only show found errors
"--quiet"
Expand Down
6 changes: 3 additions & 3 deletions libraries/aleth/libdevcore/CommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ static bool isHash(std::string const& _hash) {
/// Converts byte array to a string containing the same (binary) data. Unless
/// the byte array happens to contain ASCII data, this won't be printable.
inline std::string asString(bytes const& _b) {
return std::string((char const*)_b.data(), (char const*)(_b.data() + _b.size()));
return std::string(reinterpret_cast<char const*>(_b.data()), reinterpret_cast<char const*>(_b.data() + _b.size()));
}

/// Converts byte array ref to a string containing the same (binary) data.
/// Unless the byte array happens to contain ASCII data, this won't be
/// printable.
inline std::string asString(bytesConstRef _b) {
return std::string((char const*)_b.data(), (char const*)(_b.data() + _b.size()));
return std::string(reinterpret_cast<char const*>(_b.data()), reinterpret_cast<char const*>(_b.data() + _b.size()));
}

/// Converts a string to a byte array containing the string's (byte) data.
inline bytes asBytes(std::string const& _b) {
return bytes((::byte const*)_b.data(), (::byte const*)(_b.data() + _b.size()));
return bytes(reinterpret_cast<::byte const*>(_b.data()), reinterpret_cast<::byte const*>(_b.data() + _b.size()));
}

/// Converts a string into the big-endian base-16 stream of integers (NOT
Expand Down
4 changes: 2 additions & 2 deletions libraries/aleth/libdevcore/FixedHash.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ class SecureFixedHash : private FixedHash<T> {
/// Fast equality operator for h256.
template <>
inline bool FixedHash<32>::operator==(FixedHash<32> const& _other) const {
const uint64_t* hash1 = (const uint64_t*)data();
const uint64_t* hash2 = (const uint64_t*)_other.data();
const uint64_t* hash1 = reinterpret_cast<const uint64_t*>(data());
const uint64_t* hash2 = reinterpret_cast<const uint64_t*>(_other.data());
return (hash1[0] == hash2[0]) && (hash1[1] == hash2[1]) && (hash1[2] == hash2[2]) && (hash1[3] == hash2[3]);
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/aleth/libdevcore/RLP.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class RLP {

/// Construct a node to read RLP data in the string.
explicit RLP(std::string const& _s, Strictness _st = VeryStrict)
: RLP(bytesConstRef((::byte const*)_s.data(), _s.size()), _st) {}
: RLP(bytesConstRef(reinterpret_cast<::byte const*>(_s.data()), _s.size()), _st) {}

/// The bare data of the RLP.
bytesConstRef data() const { return m_data; }
Expand Down
2 changes: 1 addition & 1 deletion libraries/aleth/libdevcrypto/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct SignatureStruct {
SignatureStruct() = default;
SignatureStruct(Signature const& _s) { *(h520*)this = _s; }
SignatureStruct(h256 const& _r, h256 const& _s, byte _v) : r(_r), s(_s), v(_v) {}
operator Signature() const { return *(h520 const*)this; }
operator Signature() const { return *reinterpret_cast<h520 const*>(this); }

/// @returns true if r,s,v values are valid, otherwise false
bool isValid() const noexcept;
Expand Down
2 changes: 1 addition & 1 deletion libraries/aleth/libdevcrypto/CryptoPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ bool Secp256k1PP::decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, b
return false;

Secret z;
if (!ecdh::agree(_k, *(Public*)(io_text.data() + 1), z)) return false; // Invalid pubkey or seckey.
if (!ecdh::agree(_k, *reinterpret_cast<Public*>(io_text.data() + 1), z)) return false; // Invalid pubkey or seckey.
auto key = ecies::kdf(z, bytes(), 64);
bytesConstRef eKey = bytesConstRef(&key).cropped(0, 16);
bytesRef mKeyMaterial = bytesRef(&key).cropped(16, 16);
Expand Down
2 changes: 1 addition & 1 deletion libraries/aleth/libp2p/UPnP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ UPnP::UPnP()
#elif MINIUPNPC_API_VERSION >= 9
descXML = (char*)miniwget(dev->descURL, &descXMLsize, 0);
#else
descXML = (char*)miniwget(dev->descURL, &descXMLsize);
descXML = static_cast<char*>(miniwget(dev->descURL, &descXMLsize));
#endif
if (descXML) {
parserootdesc(descXML, descXMLsize, m_data.get());
Expand Down
17 changes: 10 additions & 7 deletions libraries/common/src/vrf_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ namespace taraxa::vrf_wrapper {
std::pair<vrf_pk_t, vrf_sk_t> getVrfKeyPair() {
vrf_sk_t sk;
vrf_pk_t pk;
crypto_vrf_keypair((unsigned char *)pk.data(), (unsigned char *)sk.data());
crypto_vrf_keypair(pk.data(), sk.data());
return {pk, sk};
}

vrf_pk_t getVrfPublicKey(vrf_sk_t const &sk) {
vrf_pk_t pk;
crypto_vrf_sk_to_pk((unsigned char *)pk.data(), (unsigned char *)sk.data());
crypto_vrf_sk_to_pk(pk.data(), const_cast<unsigned char *>(sk.data()));
return pk;
}

bool isValidVrfPublicKey(vrf_pk_t const &pk) { return crypto_vrf_is_valid_key((unsigned char *)pk.data()) == 1; }
bool isValidVrfPublicKey(vrf_pk_t const &pk) {
return crypto_vrf_is_valid_key(const_cast<unsigned char *>(pk.data())) == 1;
}

std::optional<vrf_proof_t> getVrfProof(vrf_sk_t const &sk, bytes const &msg) {
vrf_proof_t proof;
// crypto_vrf_prove return 0 on success!
if (!crypto_vrf_prove((unsigned char *)proof.data(), (const unsigned char *)sk.data(),
(const unsigned char *)msg.data(), msg.size())) {
if (!crypto_vrf_prove(proof.data(), const_cast<unsigned char *>(sk.data()), const_cast<unsigned char *>(msg.data()),
msg.size())) {
return proof;
}
return {};
Expand All @@ -30,8 +32,9 @@ std::optional<vrf_proof_t> getVrfProof(vrf_sk_t const &sk, bytes const &msg) {
std::optional<vrf_output_t> getVrfOutput(vrf_pk_t const &pk, vrf_proof_t const &proof, bytes const &msg) {
vrf_output_t output;
// crypto_vrf_verify return 0 on success!
if (!crypto_vrf_verify((unsigned char *)output.data(), (const unsigned char *)pk.data(),
(const unsigned char *)proof.data(), (const unsigned char *)msg.data(), msg.size())) {
if (!crypto_vrf_verify(output.data(), const_cast<unsigned char *>(pk.data()),
const_cast<unsigned char *>(proof.data()), const_cast<unsigned char *>(msg.data()),
msg.size())) {
return output;
}
return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ DagBlock DagBlockProposer::createDagBlock(DagFrontier&& frontier, level_t level,
trx_hashes.push_back(trx->getHash());
}

const int64_t block_estimation = std::accumulate(estimations.begin(), estimations.end(), 0);
const uint64_t block_estimation = std::accumulate(estimations.begin(), estimations.end(), 0);

// If number of tips is over the limit filter by producer and level
if (frontier.tips.size() > kDagBlockMaxTips || (frontier.tips.size() + 1) > kPbftGasLimit / kDagGasLimit) {
Expand Down
4 changes: 2 additions & 2 deletions libraries/core_libs/consensus/src/final_chain/state_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void from_rlp(taraxa_evm_Bytes b, Result& result) {
util::rlp(dev::RLP(map_bytes(b), 0), result);
}

void to_str(taraxa_evm_Bytes b, string& result) { result = {(char*)b.Data, b.Len}; }
void to_str(taraxa_evm_Bytes b, string& result) { result = {reinterpret_cast<char*>(b.Data), b.Len}; }

void to_bytes(taraxa_evm_Bytes b, bytes& result) { result.assign(b.Data, b.Data + b.Len); }

Expand All @@ -31,7 +31,7 @@ template <typename Result, void (*decode)(taraxa_evm_Bytes, Result&)>
taraxa_evm_BytesCallback decoder_cb_c(Result& res) {
return {
&res,
[](auto receiver, auto b) { decode(b, *(Result*)receiver); },
[](auto receiver, auto b) { decode(b, *static_cast<Result*>(receiver)); },
};
}

Expand Down
4 changes: 2 additions & 2 deletions libraries/core_libs/network/rpc/jsonrpc_ws_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ std::string JsonRpcWsSession::processRequest(const std::string_view &request) {
auto handler = ws_server->GetHandler();
if (handler != NULL) {
try {
LOG(log_tr_) << "WS Read: " << (char *)buffer_.data().data();
handler->HandleRequest((char *)buffer_.data().data(), response);
LOG(log_tr_) << "WS Read: " << static_cast<char *>(buffer_.data().data());
handler->HandleRequest(static_cast<char *>(buffer_.data().data()), response);
} catch (std::exception const &e) {
LOG(log_er_) << "Exception " << e.what();
auto &res_json_error = json_response["error"] = Json::Value(Json::objectValue);
Expand Down
2 changes: 1 addition & 1 deletion libraries/core_libs/network/src/ws_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void WsSession::on_read(beast::error_code ec, std::size_t bytes_transferred) {
return close(is_normal(ec));
}

LOG(log_tr_) << "WS READ " << ((char *)buffer_.data().data());
LOG(log_tr_) << "WS READ " << (static_cast<char *>(buffer_.data().data()));

const std::string_view str_view(static_cast<const char *>(buffer_.data().data()), buffer_.size());
const auto response = processRequest(str_view);
Expand Down
4 changes: 2 additions & 2 deletions libraries/core_libs/storage/src/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ void DbStorage::clearPeriodDataHistory(PbftPeriod end_period) {
auto trx_hashes_raw = lookup(period, DB::Columns::final_chain_transaction_hashes_by_blk_number);
auto hashes_count = trx_hashes_raw.size() / trx_hash_t::size;
for (uint32_t i = 0; i < hashes_count; i++) {
auto hash =
trx_hash_t((uint8_t*)(trx_hashes_raw.data() + i * trx_hash_t::size), trx_hash_t::ConstructFromPointer);
auto hash = trx_hash_t(reinterpret_cast<uint8_t*>(trx_hashes_raw.data() + i * trx_hash_t::size),
trx_hash_t::ConstructFromPointer);
remove(write_batch, Columns::final_chain_receipt_by_trx_hash, hash);
remove(write_batch, Columns::final_chain_transaction_location_by_hash, hash);
}
Expand Down

0 comments on commit 87a20ac

Please sign in to comment.