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

refactor: only use explicit reinterpret/const casts, not implicit #24185

Closed
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
2 changes: 1 addition & 1 deletion src/addrdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data
{
// Generate random temporary filename
uint16_t randv = 0;
GetRandBytes({(unsigned char*)&randv, sizeof(randv)});
GetRandBytes({reinterpret_cast<unsigned char*>(&randv), sizeof(randv)});
std::string tmpfn = strprintf("%s.%04x", prefix, randv);

// open temp output file, and associate with CAutoFile
Expand Down
2 changes: 1 addition & 1 deletion src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ std::string EncodeBase58Check(Span<const unsigned char> input)
// add 4-byte hash check to the end
std::vector<unsigned char> vch(input.begin(), input.end());
uint256 hash = Hash(vch);
vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
vch.insert(vch.end(), reinterpret_cast<unsigned char*>(&hash), reinterpret_cast<unsigned char*>(&hash) + 4);
return EncodeBase58(vch);
}

Expand Down
2 changes: 1 addition & 1 deletion src/bench/crypto_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static void SipHash_32b(benchmark::Bench& bench)
uint256 x;
uint64_t k1 = 0;
bench.run([&] {
*((uint64_t*)x.begin()) = SipHashUint256(0, ++k1, x);
*(reinterpret_cast<uint64_t*>(x.begin())) = SipHashUint256(0, ++k1, x);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/bench/verify_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void VerifyScriptBench(benchmark::Bench& bench)
txCredit.vout[0].scriptPubKey.data(),
txCredit.vout[0].scriptPubKey.size(),
txCredit.vout[0].nValue,
(const unsigned char*)stream.data(), stream.size(), 0, flags, nullptr);
reinterpret_cast<const unsigned char*>(stream.data()), stream.size(), 0, flags, nullptr);
assert(csuccess == 1);
#endif
});
Expand Down
4 changes: 2 additions & 2 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ static void http_request_done(struct evhttp_request *req, void *ctx)
if (buf)
{
size_t size = evbuffer_get_length(buf);
const char *data = (const char*)evbuffer_pullup(buf, size);
const char *data = reinterpret_cast<const char*>(evbuffer_pullup(buf, size));
if (data)
reply->body = std::string(data, size);
evbuffer_drain(buf, size);
Expand Down Expand Up @@ -740,7 +740,7 @@ static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, co
}

HTTPReply response;
raii_evhttp_request req = obtain_evhttp_request(http_request_done, (void*)&response);
raii_evhttp_request req = obtain_evhttp_request(http_request_done, reinterpret_cast<void*>(&response));
if (req == nullptr) {
throw std::runtime_error("create http request failed");
}
Expand Down
2 changes: 1 addition & 1 deletion src/blockencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void CBlockHeaderAndShortTxIDs::FillShortTxIDSelector() const {
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << header << nonce;
CSHA256 hasher;
hasher.Write((unsigned char*)&(*stream.begin()), stream.end() - stream.begin());
hasher.Write(reinterpret_cast<const unsigned char*>(&(*stream.begin())), stream.end() - stream.begin());
uint256 shorttxidhash;
hasher.Finalize(shorttxidhash.begin());
shorttxidk0 = shorttxidhash.GetUint64(0);
Expand Down
2 changes: 1 addition & 1 deletion src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
txNew.nVersion = 1;
txNew.vin.resize(1);
txNew.vout.resize(1);
txNew.vin[0].scriptSig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
txNew.vin[0].scriptSig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>(reinterpret_cast<const unsigned char*>(pszTimestamp), reinterpret_cast<const unsigned char*>(pszTimestamp) + strlen(pszTimestamp));
txNew.vout[0].nValue = genesisReward;
txNew.vout[0].scriptPubKey = genesisOutputScript;

Expand Down
22 changes: 11 additions & 11 deletions src/crypto/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,73 +17,73 @@
uint16_t static inline ReadLE16(const unsigned char* ptr)
{
uint16_t x;
memcpy((char*)&x, ptr, 2);
memcpy(reinterpret_cast<char*>(&x), ptr, 2);
return le16toh(x);
}

uint32_t static inline ReadLE32(const unsigned char* ptr)
{
uint32_t x;
memcpy((char*)&x, ptr, 4);
memcpy(reinterpret_cast<char*>(&x), ptr, 4);
return le32toh(x);
}

uint64_t static inline ReadLE64(const unsigned char* ptr)
{
uint64_t x;
memcpy((char*)&x, ptr, 8);
memcpy(reinterpret_cast<char*>(&x), ptr, 8);
return le64toh(x);
}

void static inline WriteLE16(unsigned char* ptr, uint16_t x)
{
uint16_t v = htole16(x);
memcpy(ptr, (char*)&v, 2);
memcpy(ptr, reinterpret_cast<char*>(&v), 2);
}

void static inline WriteLE32(unsigned char* ptr, uint32_t x)
{
uint32_t v = htole32(x);
memcpy(ptr, (char*)&v, 4);
memcpy(ptr, reinterpret_cast<char*>(&v), 4);
}

void static inline WriteLE64(unsigned char* ptr, uint64_t x)
{
uint64_t v = htole64(x);
memcpy(ptr, (char*)&v, 8);
memcpy(ptr, reinterpret_cast<char*>(&v), 8);
}

uint16_t static inline ReadBE16(const unsigned char* ptr)
{
uint16_t x;
memcpy((char*)&x, ptr, 2);
memcpy(reinterpret_cast<char*>(&x), ptr, 2);
return be16toh(x);
}

uint32_t static inline ReadBE32(const unsigned char* ptr)
{
uint32_t x;
memcpy((char*)&x, ptr, 4);
memcpy(reinterpret_cast<char*>(&x), ptr, 4);
return be32toh(x);
}

uint64_t static inline ReadBE64(const unsigned char* ptr)
{
uint64_t x;
memcpy((char*)&x, ptr, 8);
memcpy(reinterpret_cast<char*>(&x), ptr, 8);
return be64toh(x);
}

void static inline WriteBE32(unsigned char* ptr, uint32_t x)
{
uint32_t v = htobe32(x);
memcpy(ptr, (char*)&v, 4);
memcpy(ptr, reinterpret_cast<char*>(&v), 4);
}

void static inline WriteBE64(unsigned char* ptr, uint64_t x)
{
uint64_t v = htobe64(x);
memcpy(ptr, (char*)&v, 8);
memcpy(ptr, reinterpret_cast<char*>(&v), 8);
}

/** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/hkdf_sha256_32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

CHKDF_HMAC_SHA256_L32::CHKDF_HMAC_SHA256_L32(const unsigned char* ikm, size_t ikmlen, const std::string& salt)
{
CHMAC_SHA256((const unsigned char*)salt.data(), salt.size()).Write(ikm, ikmlen).Finalize(m_prk);
CHMAC_SHA256(reinterpret_cast<const unsigned char*>(salt.data()), salt.size()).Write(ikm, ikmlen).Finalize(m_prk);
}

void CHKDF_HMAC_SHA256_L32::Expand32(const std::string& info, unsigned char hash[OUTPUT_SIZE])
{
// expand a 32byte key (single round)
assert(info.size() <= 128);
static const unsigned char one[1] = {1};
CHMAC_SHA256(m_prk, 32).Write((const unsigned char*)info.data(), info.size()).Write(one, 1).Finalize(hash);
CHMAC_SHA256(m_prk, 32).Write(reinterpret_cast<const unsigned char*>(info.data()), info.size()).Write(one, 1).Finalize(hash);
}
36 changes: 18 additions & 18 deletions src/crypto/sha256_x86_shani.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ void inline __attribute__((always_inline)) Unshuffle(__m128i& s0, __m128i& s1)

__m128i inline __attribute__((always_inline)) Load(const unsigned char* in)
{
return _mm_shuffle_epi8(_mm_loadu_si128((const __m128i*)in), _mm_load_si128((const __m128i*)MASK));
return _mm_shuffle_epi8(_mm_loadu_si128(reinterpret_cast<const __m128i*>(in)), _mm_load_si128(reinterpret_cast<const __m128i*>(MASK)));
}

void inline __attribute__((always_inline)) Save(unsigned char* out, __m128i s)
{
_mm_storeu_si128((__m128i*)out, _mm_shuffle_epi8(s, _mm_load_si128((const __m128i*)MASK)));
_mm_storeu_si128(reinterpret_cast<__m128i*>(out), _mm_shuffle_epi8(s, _mm_load_si128(reinterpret_cast<const __m128i*>(MASK))));
}
}

Expand All @@ -80,8 +80,8 @@ void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks)
__m128i m0, m1, m2, m3, s0, s1, so0, so1;

/* Load state */
s0 = _mm_loadu_si128((const __m128i*)s);
s1 = _mm_loadu_si128((const __m128i*)(s + 4));
s0 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(s));
s1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(s + 4));
Shuffle(s0, s1);

while (blocks--) {
Expand Down Expand Up @@ -134,8 +134,8 @@ void Transform(uint32_t* s, const unsigned char* chunk, size_t blocks)
}

Unshuffle(s0, s1);
_mm_storeu_si128((__m128i*)s, s0);
_mm_storeu_si128((__m128i*)(s + 4), s1);
_mm_storeu_si128(reinterpret_cast<__m128i*>(s), s0);
_mm_storeu_si128(reinterpret_cast<__m128i*>(s + 4), s1);
}
}

Expand All @@ -147,8 +147,8 @@ void Transform_2way(unsigned char* out, const unsigned char* in)
__m128i bm0, bm1, bm2, bm3, bs0, bs1, bso0, bso1;

/* Transform 1 */
bs0 = as0 = _mm_load_si128((const __m128i*)INIT0);
bs1 = as1 = _mm_load_si128((const __m128i*)INIT1);
bs0 = as0 = _mm_load_si128(reinterpret_cast<const __m128i*>(INIT0));
bs1 = as1 = _mm_load_si128(reinterpret_cast<const __m128i*>(INIT1));
am0 = Load(in);
bm0 = Load(in + 64);
QuadRound(as0, as1, am0, 0xe9b5dba5b5c0fbcfull, 0x71374491428a2f98ull);
Expand Down Expand Up @@ -217,10 +217,10 @@ void Transform_2way(unsigned char* out, const unsigned char* in)
ShiftMessageC(bm1, bm2, bm3);
QuadRound(as0, as1, am3, 0xc67178f2bef9A3f7ull, 0xa4506ceb90befffaull);
QuadRound(bs0, bs1, bm3, 0xc67178f2bef9A3f7ull, 0xa4506ceb90befffaull);
as0 = _mm_add_epi32(as0, _mm_load_si128((const __m128i*)INIT0));
bs0 = _mm_add_epi32(bs0, _mm_load_si128((const __m128i*)INIT0));
as1 = _mm_add_epi32(as1, _mm_load_si128((const __m128i*)INIT1));
bs1 = _mm_add_epi32(bs1, _mm_load_si128((const __m128i*)INIT1));
as0 = _mm_add_epi32(as0, _mm_load_si128(reinterpret_cast<const __m128i*>(INIT0)));
bs0 = _mm_add_epi32(bs0, _mm_load_si128(reinterpret_cast<const __m128i*>(INIT0)));
as1 = _mm_add_epi32(as1, _mm_load_si128(reinterpret_cast<const __m128i*>(INIT1)));
bs1 = _mm_add_epi32(bs1, _mm_load_si128(reinterpret_cast<const __m128i*>(INIT1)));

/* Transform 2 */
aso0 = as0;
Expand Down Expand Up @@ -273,8 +273,8 @@ void Transform_2way(unsigned char* out, const unsigned char* in)
bm1 = bs1;

/* Transform 3 */
bs0 = as0 = _mm_load_si128((const __m128i*)INIT0);
bs1 = as1 = _mm_load_si128((const __m128i*)INIT1);
bs0 = as0 = _mm_load_si128(reinterpret_cast<const __m128i*>(INIT0));
bs1 = as1 = _mm_load_si128(reinterpret_cast<const __m128i*>(INIT1));
QuadRound(as0, as1, am0, 0xe9b5dba5B5c0fbcfull, 0x71374491428a2f98ull);
QuadRound(bs0, bs1, bm0, 0xe9b5dba5B5c0fbcfull, 0x71374491428a2f98ull);
QuadRound(as0, as1, am1, 0xab1c5ed5923f82a4ull, 0x59f111f13956c25bull);
Expand Down Expand Up @@ -337,10 +337,10 @@ void Transform_2way(unsigned char* out, const unsigned char* in)
ShiftMessageC(bm1, bm2, bm3);
QuadRound(as0, as1, am3, 0xc67178f2bef9a3f7ull, 0xa4506ceb90befffaull);
QuadRound(bs0, bs1, bm3, 0xc67178f2bef9a3f7ull, 0xa4506ceb90befffaull);
as0 = _mm_add_epi32(as0, _mm_load_si128((const __m128i*)INIT0));
bs0 = _mm_add_epi32(bs0, _mm_load_si128((const __m128i*)INIT0));
as1 = _mm_add_epi32(as1, _mm_load_si128((const __m128i*)INIT1));
bs1 = _mm_add_epi32(bs1, _mm_load_si128((const __m128i*)INIT1));
as0 = _mm_add_epi32(as0, _mm_load_si128(reinterpret_cast<const __m128i*>(INIT0)));
bs0 = _mm_add_epi32(bs0, _mm_load_si128(reinterpret_cast<const __m128i*>(INIT0)));
as1 = _mm_add_epi32(as1, _mm_load_si128(reinterpret_cast<const __m128i*>(INIT1)));
bs1 = _mm_add_epi32(bs1, _mm_load_si128(reinterpret_cast<const __m128i*>(INIT1)));

/* Extract hash into out */
Unshuffle(as0, as1);
Expand Down
20 changes: 10 additions & 10 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ class CDBBatch
{
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
leveldb::Slice slKey(reinterpret_cast<const char*>(ssKey.data()), ssKey.size());

ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
ssValue << value;
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
leveldb::Slice slValue((const char*)ssValue.data(), ssValue.size());
leveldb::Slice slValue(reinterpret_cast<const char*>(ssValue.data()), ssValue.size());

batch.Put(slKey, slValue);
// LevelDB serializes writes as:
Expand All @@ -99,7 +99,7 @@ class CDBBatch
{
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
leveldb::Slice slKey(reinterpret_cast<const char*>(ssKey.data()), ssKey.size());

batch.Delete(slKey);
// LevelDB serializes erases as:
Expand Down Expand Up @@ -138,7 +138,7 @@ class CDBIterator
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
leveldb::Slice slKey(reinterpret_cast<const char*>(ssKey.data()), ssKey.size());
piter->Seek(slKey);
}

Expand Down Expand Up @@ -233,7 +233,7 @@ class CDBWrapper
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
leveldb::Slice slKey(reinterpret_cast<const char*>(ssKey.data()), ssKey.size());

std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
Expand Down Expand Up @@ -267,7 +267,7 @@ class CDBWrapper
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey << key;
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
leveldb::Slice slKey(reinterpret_cast<const char*>(ssKey.data()), ssKey.size());

std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
Expand Down Expand Up @@ -311,8 +311,8 @@ class CDBWrapper
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey1 << key_begin;
ssKey2 << key_end;
leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size());
leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size());
leveldb::Slice slKey1(reinterpret_cast<const char*>(ssKey1.data()), ssKey1.size());
leveldb::Slice slKey2(reinterpret_cast<const char*>(ssKey2.data()), ssKey2.size());
uint64_t size = 0;
leveldb::Range range(slKey1, slKey2);
pdb->GetApproximateSizes(&range, 1, &size);
Expand All @@ -330,8 +330,8 @@ class CDBWrapper
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey1 << key_begin;
ssKey2 << key_end;
leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size());
leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size());
leveldb::Slice slKey1(reinterpret_cast<const char*>(ssKey1.data()), ssKey1.size());
leveldb::Slice slKey2(reinterpret_cast<const char*>(ssKey2.data()), ssKey2.size());
pdb->CompactRange(&slKey1, &slKey2);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ CHashWriter TaggedHash(const std::string& tag)
{
CHashWriter writer(SER_GETHASH, 0);
uint256 taghash;
CSHA256().Write((const unsigned char*)tag.data(), tag.size()).Finalize(taghash.begin());
CSHA256().Write(reinterpret_cast<const unsigned char*>(tag.data()), tag.size()).Finalize(taghash.begin());
writer << taghash << taghash;
return writer;
}
5 changes: 2 additions & 3 deletions src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ std::string HTTPRequest::ReadBody()
* better to not copy into an intermediate string but use a stream
* abstraction to consume the evbuffer on the fly in the parsing algorithm.
*/
const char* data = (const char*)evbuffer_pullup(buf, size);
const char* data = reinterpret_cast<const char*>(evbuffer_pullup(buf, size));
if (!data) // returns nullptr in case of empty buffer
return "";
std::string rv(data, size);
Expand Down Expand Up @@ -597,9 +597,8 @@ CService HTTPRequest::GetPeer() const
#ifdef HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
evhttp_connection_get_peer(con, &address, &port);
#else
evhttp_connection_get_peer(con, (char**)&address, &port);
evhttp_connection_get_peer(con, const_cast<char**>(reinterpret_cast<const char**>(&address)), &port);
#endif // HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR

peer = LookupNumeric(address, port);
}
return peer;
Expand Down
8 changes: 4 additions & 4 deletions src/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ CPubKey CKey::GetPubKey() const {
CPubKey result;
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
assert(ret);
secp256k1_ec_pubkey_serialize(secp256k1_context_sign, (unsigned char*)result.begin(), &clen, &pubkey, fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
secp256k1_ec_pubkey_serialize(secp256k1_context_sign, const_cast<unsigned char*>(result.begin()), &clen, &pubkey, fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
assert(result.size() == clen);
assert(result.IsValid());
return result;
Expand Down Expand Up @@ -301,7 +301,7 @@ bool CKey::SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint2
}

bool CKey::Load(const CPrivKey &seckey, const CPubKey &vchPubKey, bool fSkipCheck=false) {
if (!ec_seckey_import_der(secp256k1_context_sign, (unsigned char*)begin(), seckey.data(), seckey.size()))
if (!ec_seckey_import_der(secp256k1_context_sign, const_cast<unsigned char*>(begin()), seckey.data(), seckey.size()))
return false;
fCompressed = vchPubKey.IsCompressed();
fValid = true;
Expand All @@ -325,8 +325,8 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
BIP32Hash(cc, nChild, 0, begin(), vout.data());
}
memcpy(ccChild.begin(), vout.data()+32, 32);
memcpy((unsigned char*)keyChild.begin(), begin(), 32);
bool ret = secp256k1_ec_seckey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), vout.data());
memcpy(const_cast<unsigned char*>(keyChild.begin()), begin(), 32);
bool ret = secp256k1_ec_seckey_tweak_add(secp256k1_context_sign, const_cast<unsigned char*>(keyChild.begin()), vout.data());
keyChild.fCompressed = true;
keyChild.fValid = ret;
return ret;
Expand Down
Loading