Skip to content

Commit 9ad6746

Browse files
Use static_cast instead of C-style casts for non-fundamental types
A C-style cast is equivalent to try casting in the following order: 1. const_cast(...) 2. static_cast(...) 3. const_cast(static_cast(...)) 4. reinterpret_cast(...) 5. const_cast(reinterpret_cast(...)) By using static_cast<T>(...) explicitly we avoid the possibility of an unintentional and dangerous reinterpret_cast. Furthermore static_cast<T>(...) allows for easier grepping of casts.
1 parent 9821274 commit 9ad6746

24 files changed

+44
-44
lines changed

src/addrman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CAddrInfo : public CAddress
5959

6060
template <typename Stream, typename Operation>
6161
inline void SerializationOp(Stream& s, Operation ser_action) {
62-
READWRITE(*(CAddress*)this);
62+
READWRITE(*static_cast<CAddress*>(this));
6363
READWRITE(source);
6464
READWRITE(nLastSuccess);
6565
READWRITE(nAttempts);

src/core_read.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ CScript ParseScript(const std::string& s)
3333
if (op < OP_NOP && op != OP_RESERVED)
3434
continue;
3535

36-
const char* name = GetOpName((opcodetype)op);
36+
const char* name = GetOpName(static_cast<opcodetype>(op));
3737
if (strcmp(name, "OP_UNKNOWN") == 0)
3838
continue;
3939
std::string strName(name);
40-
mapOpNames[strName] = (opcodetype)op;
40+
mapOpNames[strName] = static_cast<opcodetype>(op);
4141
// Convenience: OP_ADD and just ADD are both recognized:
4242
boost::algorithm::replace_first(strName, "OP_", "");
43-
mapOpNames[strName] = (opcodetype)op;
43+
mapOpNames[strName] = static_cast<opcodetype>(op);
4444
}
4545
}
4646

src/httpserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ struct event_base* EventBase()
512512
static void httpevent_callback_fn(evutil_socket_t, short, void* data)
513513
{
514514
// Static handler: simply call inner handler
515-
HTTPEvent *self = ((HTTPEvent*)data);
515+
HTTPEvent *self = static_cast<HTTPEvent*>(data);
516516
self->handler();
517517
if (self->deleteWhenTriggered)
518518
delete self;

src/net.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ CNode* CConnman::FindNode(const CNetAddr& ip)
299299
{
300300
LOCK(cs_vNodes);
301301
for (CNode* pnode : vNodes) {
302-
if ((CNetAddr)pnode->addr == ip) {
302+
if (static_cast<CNetAddr>(pnode->addr) == ip) {
303303
return pnode;
304304
}
305305
}
@@ -310,7 +310,7 @@ CNode* CConnman::FindNode(const CSubNet& subNet)
310310
{
311311
LOCK(cs_vNodes);
312312
for (CNode* pnode : vNodes) {
313-
if (subNet.Match((CNetAddr)pnode->addr)) {
313+
if (subNet.Match(static_cast<CNetAddr>(pnode->addr))) {
314314
return pnode;
315315
}
316316
}
@@ -332,7 +332,7 @@ CNode* CConnman::FindNode(const CService& addr)
332332
{
333333
LOCK(cs_vNodes);
334334
for (CNode* pnode : vNodes) {
335-
if ((CService)pnode->addr == addr) {
335+
if (static_cast<CService>(pnode->addr) == addr) {
336336
return pnode;
337337
}
338338
}
@@ -372,7 +372,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
372372
return nullptr;
373373

374374
// Look for an existing connection
375-
CNode* pnode = FindNode((CService)addrConnect);
375+
CNode* pnode = FindNode(static_cast<CService>(addrConnect));
376376
if (pnode)
377377
{
378378
LogPrintf("Failed to open new connection, already connected\n");
@@ -403,7 +403,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
403403
// Also store the name we used to connect in that CNode, so that future FindNode() calls to that
404404
// name catch this early.
405405
LOCK(cs_vNodes);
406-
CNode* pnode = FindNode((CService)addrConnect);
406+
CNode* pnode = FindNode(static_cast<CService>(addrConnect));
407407
if (pnode)
408408
{
409409
pnode->MaybeSetAddrName(std::string(pszDest));
@@ -533,7 +533,7 @@ void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t ba
533533
{
534534
LOCK(cs_vNodes);
535535
for (CNode* pnode : vNodes) {
536-
if (subNet.Match((CNetAddr)pnode->addr))
536+
if (subNet.Match(static_cast<CNetAddr>(pnode->addr)))
537537
pnode->fDisconnect = true;
538538
}
539539
}
@@ -1946,7 +1946,7 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
19461946
}
19471947
if (!pszDest) {
19481948
if (IsLocal(addrConnect) ||
1949-
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1949+
FindNode(static_cast<CNetAddr>(addrConnect)) || IsBanned(addrConnect) ||
19501950
FindNode(addrConnect.ToStringIPPort()))
19511951
return false;
19521952
} else if (FindNode(std::string(pszDest)))

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,7 @@ static bool SendRejectsAndCheckIfBanned(CNode* pnode, CConnman* connman)
26162616
CNodeState &state = *State(pnode->GetId());
26172617

26182618
for (const CBlockReject& reject : state.rejects) {
2619-
connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, (std::string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
2619+
connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, std::string(NetMsgType::BLOCK), reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
26202620
}
26212621
state.rejects.clear();
26222622

src/netaddress.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,17 +526,17 @@ unsigned short CService::GetPort() const
526526

527527
bool operator==(const CService& a, const CService& b)
528528
{
529-
return (CNetAddr)a == (CNetAddr)b && a.port == b.port;
529+
return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
530530
}
531531

532532
bool operator!=(const CService& a, const CService& b)
533533
{
534-
return (CNetAddr)a != (CNetAddr)b || a.port != b.port;
534+
return static_cast<CNetAddr>(a) != static_cast<CNetAddr>(b) || a.port != b.port;
535535
}
536536

537537
bool operator<(const CService& a, const CService& b)
538538
{
539-
return (CNetAddr)a < (CNetAddr)b || ((CNetAddr)a == (CNetAddr)b && a.port < b.port);
539+
return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
540540
}
541541

542542
bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const

src/netbase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ bool HaveNameProxy() {
528528
bool IsProxy(const CNetAddr &addr) {
529529
LOCK(cs_proxyInfos);
530530
for (int i = 0; i < NET_MAX; i++) {
531-
if (addr == (CNetAddr)proxyInfo[i].proxy)
531+
if (addr == static_cast<CNetAddr>(proxyInfo[i].proxy))
532532
return true;
533533
}
534534
return false;

src/primitives/block.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ class CBlock : public CBlockHeader
8686
CBlock(const CBlockHeader &header)
8787
{
8888
SetNull();
89-
*((CBlockHeader*)this) = header;
89+
*(static_cast<CBlockHeader*>(this)) = header;
9090
}
9191

9292
ADD_SERIALIZE_METHODS;
9393

9494
template <typename Stream, typename Operation>
9595
inline void SerializationOp(Stream& s, Operation ser_action) {
96-
READWRITE(*(CBlockHeader*)this);
96+
READWRITE(*static_cast<CBlockHeader*>(this));
9797
READWRITE(vtx);
9898
}
9999

src/protocol.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ class CAddress : public CService
301301
READWRITE(nTime);
302302
uint64_t nServicesInt = nServices;
303303
READWRITE(nServicesInt);
304-
nServices = (ServiceFlags)nServicesInt;
305-
READWRITE(*(CService*)this);
304+
nServices = static_cast<ServiceFlags>(nServicesInt);
305+
READWRITE(*static_cast<CService*>(this));
306306
}
307307

308308
// TODO: make private (improves encapsulation)

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ int main(int argc, char *argv[])
709709
if (BitcoinCore::baseInitialize()) {
710710
app.requestInitialize();
711711
#if defined(Q_OS_WIN) && QT_VERSION >= 0x050000
712-
WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("%1 didn't yet exit safely...").arg(QObject::tr(PACKAGE_NAME)), (HWND)app.getMainWinId());
712+
WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("%1 didn't yet exit safely...").arg(QObject::tr(PACKAGE_NAME)), static_cast<HWND>(app.getMainWinId()));
713713
#endif
714714
app.exec();
715715
app.requestShutdown();

0 commit comments

Comments
 (0)