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

[Backport] Use static_cast instead of C-style casts for non-fundamental types #2185

Merged
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
2 changes: 1 addition & 1 deletion src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CAddrInfo : public CAddress
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(*(CAddress*)this);
READWRITE(*static_cast<CAddress*>(this));
READWRITE(source);
READWRITE(nLastSuccess);
READWRITE(nAttempts);
Expand Down
6 changes: 3 additions & 3 deletions src/core_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ CScript ParseScript(std::string s)
if (op < OP_NOP && op != OP_RESERVED)
continue;

const char* name = GetOpName((opcodetype)op);
const char* name = GetOpName(static_cast<opcodetype>(op));
if (strcmp(name, "OP_UNKNOWN") == 0)
continue;
std::string strName(name);
mapOpNames[strName] = (opcodetype)op;
mapOpNames[strName] = static_cast<opcodetype>(op);
// Convenience: OP_ADD and just ADD are both recognized:
boost::algorithm::replace_first(strName, "OP_", "");
mapOpNames[strName] = (opcodetype)op;
mapOpNames[strName] = static_cast<opcodetype>(op);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ struct event_base* EventBase()
static void httpevent_callback_fn(evutil_socket_t, short, void* data)
{
// Static handler: simply call inner handler
HTTPEvent *self = ((HTTPEvent*)data);
HTTPEvent *self = static_cast<HTTPEvent*>(data);
self->handler();
if (self->deleteWhenTriggered)
delete self;
Expand Down
12 changes: 6 additions & 6 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ CNode* CConnman::FindNode(const CNetAddr& ip)
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes)
if ((CNetAddr)pnode->addr == ip)
if (static_cast<CNetAddr>(pnode->addr) == ip)
return (pnode);
return NULL;
}
Expand All @@ -294,7 +294,7 @@ CNode* CConnman::FindNode(const CSubNet& subNet)
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes)
if (subNet.Match((CNetAddr)pnode->addr))
if (subNet.Match(static_cast<CNetAddr>(pnode->addr)))
return (pnode);
return NULL;
}
Expand All @@ -317,7 +317,7 @@ CNode* CConnman::FindNode(const CService& addr)
for (CNode* pnode : vNodes) {
if (isRegTestNet) {
//if using regtest, just check the IP
if ((CNetAddr)pnode->addr == (CNetAddr)addr)
if (static_cast<CNetAddr>(pnode->addr) == static_cast<CNetAddr>(addr))
return (pnode);
} else {
if (pnode->addr == addr)
Expand Down Expand Up @@ -346,7 +346,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char* pszDest, bool fCo
}

// Look for an existing connection
CNode* pnode = FindNode((CService)addrConnect);
CNode* pnode = FindNode(static_cast<CService>(addrConnect));
if (pnode) {
LogPrintf("Failed to open new connection, already connected\n");
return nullptr;
Expand Down Expand Up @@ -505,7 +505,7 @@ void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t ba
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes) {
if (subNet.Match((CNetAddr)pnode->addr))
if (subNet.Match(static_cast<CNetAddr>(pnode->addr)))
pnode->fDisconnect = true;
}
}
Expand Down Expand Up @@ -1813,7 +1813,7 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
}
if (!pszDest) {
if (IsLocal(addrConnect) ||
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
FindNode(static_cast<CNetAddr>(addrConnect)) || IsBanned(addrConnect) ||
FindNode(addrConnect.ToStringIPPort()))
return false;
} else if (FindNode(pszDest))
Expand Down
2 changes: 1 addition & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,7 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
CNodeState& state = *State(pto->GetId());

for (const CBlockReject& reject : state.rejects)
connman.PushMessage(pto, msgMaker.Make(NetMsgType::REJECT, (std::string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
connman.PushMessage(pto, msgMaker.Make(NetMsgType::REJECT, std::string(NetMsgType::BLOCK), reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
state.rejects.clear();

if (state.fShouldBan) {
Expand Down
6 changes: 3 additions & 3 deletions src/netaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,17 +498,17 @@ unsigned short CService::GetPort() const

bool operator==(const CService& a, const CService& b)
{
return (CNetAddr)a == (CNetAddr)b && a.port == b.port;
return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
}

bool operator!=(const CService& a, const CService& b)
{
return (CNetAddr)a != (CNetAddr)b || a.port != b.port;
return static_cast<CNetAddr>(a) != static_cast<CNetAddr>(b) || a.port != b.port;
}

bool operator<(const CService& a, const CService& b)
{
return (CNetAddr)a < (CNetAddr)b || ((CNetAddr)a == (CNetAddr)b && a.port < b.port);
return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
}

bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
Expand Down
2 changes: 1 addition & 1 deletion src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ bool IsProxy(const CNetAddr& addr)
{
LOCK(cs_proxyInfos);
for (int i = 0; i < NET_MAX; i++) {
if (addr == (CNetAddr)proxyInfo[i].proxy)
if (addr == static_cast<CNetAddr>(proxyInfo[i].proxy))
return true;
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ class CBlock : public CBlockHeader
CBlock(const CBlockHeader &header)
{
SetNull();
*((CBlockHeader*)this) = header;
*(static_cast<CBlockHeader*>(this)) = header;
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(*(CBlockHeader*)this);
READWRITE(*static_cast<CBlockHeader*>(this));
READWRITE(vtx);
if(vtx.size() > 1 && vtx[1]->IsCoinStake())
READWRITE(vchBlockSig);
Expand Down
4 changes: 2 additions & 2 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ class CAddress : public CService
READWRITE(nTime);
uint64_t nServicesInt = nServices;
READWRITE(nServicesInt);
nServices = (ServiceFlags)nServicesInt;
READWRITE(*(CService*)this);
nServices = static_cast<ServiceFlags>(nServicesInt);
READWRITE(*static_cast<CService*>(this));
}

// TODO: make private (improves encapsulation)
Expand Down
2 changes: 1 addition & 1 deletion src/qt/coincontroldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ CoinControlDialog::CoinControlDialog(QWidget* parent, bool _forDelegation) : QDi
ui->treeWidget->setRootIsDecorated(false);
}
if (settings.contains("nCoinControlSortColumn") && settings.contains("nCoinControlSortOrder"))
sortView(settings.value("nCoinControlSortColumn").toInt(), ((Qt::SortOrder)settings.value("nCoinControlSortOrder").toInt()));
sortView(settings.value("nCoinControlSortColumn").toInt(), (static_cast<Qt::SortOrder>(settings.value("nCoinControlSortOrder").toInt())));
}

CoinControlDialog::~CoinControlDialog()
Expand Down
2 changes: 1 addition & 1 deletion src/qt/coincontroltreewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void CoinControlTreeWidget::keyPressEvent(QKeyEvent* event)
} else if (event->key() == Qt::Key_Escape) // press esc -> close dialog
{
event->ignore();
auto* coinControlDialog = (CoinControlDialog*) this->parentWidget()->parentWidget();
auto* coinControlDialog = static_cast<CoinControlDialog*>(this->parentWidget()->parentWidget());
coinControlDialog->done(QDialog::Accepted);
} else {
this->QTreeWidget::keyPressEvent(event);
Expand Down
2 changes: 1 addition & 1 deletion src/qt/pivx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ int main(int argc, char* argv[])
app.createWindow(networkStyle.data());
app.requestInitialize();
#if defined(Q_OS_WIN)
WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("PIVX Core didn't yet exit safely..."), (HWND)app.getMainWinId());
WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("%1 didn't yet exit safely...").arg(QObject::tr(PACKAGE_NAME)), (HWND)app.getMainWinId());
#endif
app.exec();
app.requestShutdown();
Expand Down
2 changes: 1 addition & 1 deletion src/qt/pivx/pivxgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ void PIVXGUI::message(const QString& title, const QString& message, unsigned int
// Append title to "PIVX - "
if (!msgType.isEmpty())
strTitle += " - " + msgType;
notificator->notify((Notificator::Class) nNotifyIcon, strTitle, message);
notificator->notify(static_cast<Notificator::Class>(nNotifyIcon), strTitle, message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class CScript : public CScriptBase
pc += nSize;
}

opcodeRet = (opcodetype)opcode;
opcodeRet = static_cast<opcodetype>(opcode);
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/torcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ TorControlConnection::~TorControlConnection()

void TorControlConnection::readcb(struct bufferevent *bev, void *ctx)
{
TorControlConnection *self = (TorControlConnection*)ctx;
TorControlConnection *self = static_cast<TorControlConnection*>(ctx);
struct evbuffer *input = bufferevent_get_input(bev);
size_t n_read_out = 0;
char *line;
Expand Down Expand Up @@ -180,7 +180,7 @@ void TorControlConnection::readcb(struct bufferevent *bev, void *ctx)

void TorControlConnection::eventcb(struct bufferevent *bev, short what, void *ctx)
{
TorControlConnection *self = (TorControlConnection*)ctx;
TorControlConnection *self = static_cast<TorControlConnection*>(ctx);
if (what & BEV_EVENT_CONNECTED) {
LogPrint(BCLog::TOR, "tor: Successfully connected!\n");
self->connected(*self);
Expand Down Expand Up @@ -726,7 +726,7 @@ fs::path TorController::GetPrivateKeyFile()

void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg)
{
TorController *self = (TorController*)arg;
TorController *self = static_cast<TorController*>(arg);
self->Reconnect();
}

Expand Down
2 changes: 1 addition & 1 deletion src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct CDiskTxPos : public CDiskBlockPos
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(*(CDiskBlockPos*)this);
READWRITE(*static_cast<CDiskBlockPos*>(this));
READWRITE(VARINT(nTxOffset));
}

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ class CWalletTx : public CMerkleTx
mapValue["timesmart"] = strprintf("%u", nTimeSmart);
}

READWRITE(*(CMerkleTx*)this);
READWRITE(*static_cast<CMerkleTx*>(this));
std::vector<CMerkleTx> vUnused; //! Used to be vtxPrev
READWRITE(vUnused);
READWRITE(mapValue);
Expand Down