Skip to content

Commit 0277173

Browse files
author
MarcoFalke
committed
Merge #10498: Use static_cast instead of C-style casts for non-fundamental types
9ad6746 Use static_cast instead of C-style casts for non-fundamental types (practicalswift) Pull request description: 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. For a more thorough discussion, see ["ES.49: If you must use a cast, use a named cast"](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es49-if-you-must-use-a-cast-use-a-named-cast) in the C++ Core Guidelines (Stroustrup & Sutter). Tree-SHA512: bd6349b7ea157da93a47b8cf238932af5dff84731374ccfd69b9f732fabdad1f9b1cdfca67497040f14eaa85346391404f4c0495e22c467f26ca883cd2de4d3c
2 parents 1462bde + 9ad6746 commit 0277173

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
@@ -525,7 +525,7 @@ struct event_base* EventBase()
525525
static void httpevent_callback_fn(evutil_socket_t, short, void* data)
526526
{
527527
// Static handler: simply call inner handler
528-
HTTPEvent *self = ((HTTPEvent*)data);
528+
HTTPEvent *self = static_cast<HTTPEvent*>(data);
529529
self->handler();
530530
if (self->deleteWhenTriggered)
531531
delete self;

src/net.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ CNode* CConnman::FindNode(const CNetAddr& ip)
297297
{
298298
LOCK(cs_vNodes);
299299
for (CNode* pnode : vNodes) {
300-
if ((CNetAddr)pnode->addr == ip) {
300+
if (static_cast<CNetAddr>(pnode->addr) == ip) {
301301
return pnode;
302302
}
303303
}
@@ -308,7 +308,7 @@ CNode* CConnman::FindNode(const CSubNet& subNet)
308308
{
309309
LOCK(cs_vNodes);
310310
for (CNode* pnode : vNodes) {
311-
if (subNet.Match((CNetAddr)pnode->addr)) {
311+
if (subNet.Match(static_cast<CNetAddr>(pnode->addr))) {
312312
return pnode;
313313
}
314314
}
@@ -330,7 +330,7 @@ CNode* CConnman::FindNode(const CService& addr)
330330
{
331331
LOCK(cs_vNodes);
332332
for (CNode* pnode : vNodes) {
333-
if ((CService)pnode->addr == addr) {
333+
if (static_cast<CService>(pnode->addr) == addr) {
334334
return pnode;
335335
}
336336
}
@@ -370,7 +370,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
370370
return nullptr;
371371

372372
// Look for an existing connection
373-
CNode* pnode = FindNode((CService)addrConnect);
373+
CNode* pnode = FindNode(static_cast<CService>(addrConnect));
374374
if (pnode)
375375
{
376376
LogPrintf("Failed to open new connection, already connected\n");
@@ -398,7 +398,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
398398
// Also store the name we used to connect in that CNode, so that future FindNode() calls to that
399399
// name catch this early.
400400
LOCK(cs_vNodes);
401-
CNode* pnode = FindNode((CService)addrConnect);
401+
CNode* pnode = FindNode(static_cast<CService>(addrConnect));
402402
if (pnode)
403403
{
404404
pnode->MaybeSetAddrName(std::string(pszDest));
@@ -559,7 +559,7 @@ void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t ba
559559
{
560560
LOCK(cs_vNodes);
561561
for (CNode* pnode : vNodes) {
562-
if (subNet.Match((CNetAddr)pnode->addr))
562+
if (subNet.Match(static_cast<CNetAddr>(pnode->addr)))
563563
pnode->fDisconnect = true;
564564
}
565565
}
@@ -1965,7 +1965,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
19651965
}
19661966
if (!pszDest) {
19671967
if (IsLocal(addrConnect) ||
1968-
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1968+
FindNode(static_cast<CNetAddr>(addrConnect)) || IsBanned(addrConnect) ||
19691969
FindNode(addrConnect.ToStringIPPort()))
19701970
return;
19711971
} else if (FindNode(std::string(pszDest)))

src/net_processing.cpp

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

28652865
for (const CBlockReject& reject : state.rejects) {
2866-
connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, (std::string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
2866+
connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, std::string(NetMsgType::BLOCK), reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
28672867
}
28682868
state.rejects.clear();
28692869

src/netaddress.cpp

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

523523
bool operator==(const CService& a, const CService& b)
524524
{
525-
return (CNetAddr)a == (CNetAddr)b && a.port == b.port;
525+
return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
526526
}
527527

528528
bool operator!=(const CService& a, const CService& b)
529529
{
530-
return (CNetAddr)a != (CNetAddr)b || a.port != b.port;
530+
return static_cast<CNetAddr>(a) != static_cast<CNetAddr>(b) || a.port != b.port;
531531
}
532532

533533
bool operator<(const CService& a, const CService& b)
534534
{
535-
return (CNetAddr)a < (CNetAddr)b || ((CNetAddr)a == (CNetAddr)b && a.port < b.port);
535+
return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
536536
}
537537

538538
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
@@ -572,7 +572,7 @@ bool HaveNameProxy() {
572572
bool IsProxy(const CNetAddr &addr) {
573573
LOCK(cs_proxyInfos);
574574
for (int i = 0; i < NET_MAX; i++) {
575-
if (addr == (CNetAddr)proxyInfo[i].proxy)
575+
if (addr == static_cast<CNetAddr>(proxyInfo[i].proxy))
576576
return true;
577577
}
578578
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
@@ -346,8 +346,8 @@ class CAddress : public CService
346346
READWRITE(nTime);
347347
uint64_t nServicesInt = nServices;
348348
READWRITE(nServicesInt);
349-
nServices = (ServiceFlags)nServicesInt;
350-
READWRITE(*(CService*)this);
349+
nServices = static_cast<ServiceFlags>(nServicesInt);
350+
READWRITE(*static_cast<CService*>(this));
351351
}
352352

353353
// TODO: make private (improves encapsulation)

src/qt/bitcoin.cpp

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

0 commit comments

Comments
 (0)