Skip to content

Commit 070eaf7

Browse files
committed
Merge #15144: [refactor] CNode: Use C++11 default member initializers
fac2f5e Use C++11 default member initializers (MarcoFalke) Pull request description: The second and last change on this topic (c.f. #15109). Split up because the diff would otherwise interleave, making review harder than necessary. This is not a stylistic change, but a change that avoids bugs such as: * fix uninitialized read when stringifying an addrLocal #14728 * qt: Initialize members in WalletModel #12426 * net: correctly initialize nMinPingUsecTime #6636 * ... Tree-SHA512: 547ae72b87aeaed5890eb5fdcff612bfc93354632b238d89e1e1c0487187f39609bcdc537ef21345e0aea8cfcf1ea48da432d672c5386dd87cf58742446a86b1
2 parents 43a79d2 + fac2f5e commit 070eaf7

File tree

2 files changed

+45
-87
lines changed

2 files changed

+45
-87
lines changed

src/net.cpp

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,8 +2780,8 @@ int CConnman::GetBestHeight() const
27802780

27812781
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
27822782

2783-
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress &addrBindIn, const std::string& addrNameIn, bool fInboundIn) :
2784-
nTimeConnected(GetSystemTimeInSeconds()),
2783+
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, bool fInboundIn)
2784+
: nTimeConnected(GetSystemTimeInSeconds()),
27852785
addr(addrIn),
27862786
addrBind(addrBindIn),
27872787
fInbound(fInboundIn),
@@ -2791,56 +2791,14 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
27912791
id(idIn),
27922792
nLocalHostNonce(nLocalHostNonceIn),
27932793
nLocalServices(nLocalServicesIn),
2794-
nMyStartingHeight(nMyStartingHeightIn),
2795-
nSendVersion(0)
2794+
nMyStartingHeight(nMyStartingHeightIn)
27962795
{
2797-
nServices = NODE_NONE;
27982796
hSocket = hSocketIn;
2799-
nRecvVersion = INIT_PROTO_VERSION;
2800-
nLastSend = 0;
2801-
nLastRecv = 0;
2802-
nSendBytes = 0;
2803-
nRecvBytes = 0;
2804-
nTimeOffset = 0;
28052797
addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
2806-
nVersion = 0;
28072798
strSubVer = "";
2808-
fWhitelisted = false;
2809-
fOneShot = false;
2810-
m_manual_connection = false;
2811-
fClient = false; // set by version message
2812-
m_limited_node = false; // set by version message
2813-
fFeeler = false;
2814-
fSuccessfullyConnected = false;
2815-
fDisconnect = false;
2816-
nRefCount = 0;
2817-
nSendSize = 0;
2818-
nSendOffset = 0;
28192799
hashContinue = uint256();
2820-
nStartingHeight = -1;
28212800
filterInventoryKnown.reset();
2822-
fSendMempool = false;
2823-
fGetAddr = false;
2824-
nNextLocalAddrSend = 0;
2825-
nNextAddrSend = 0;
2826-
nNextInvSend = 0;
2827-
fRelayTxes = false;
2828-
fSentAddr = false;
28292801
pfilter = MakeUnique<CBloomFilter>();
2830-
timeLastMempoolReq = 0;
2831-
nLastBlockTime = 0;
2832-
nLastTXTime = 0;
2833-
nPingNonceSent = 0;
2834-
nPingUsecStart = 0;
2835-
nPingUsecTime = 0;
2836-
fPingQueued = false;
2837-
nMinPingUsecTime = std::numeric_limits<int64_t>::max();
2838-
minFeeFilter = 0;
2839-
lastSentFeeFilter = 0;
2840-
nextSendTimeFeeFilter = 0;
2841-
fPauseRecv = false;
2842-
fPauseSend = false;
2843-
nProcessQueueSize = 0;
28442802

28452803
for (const std::string &msg : getAllNetMessageTypes())
28462804
mapRecvBytesPerMsgCmd[msg] = 0;

src/net.h

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -646,80 +646,80 @@ class CNode
646646
friend class CConnman;
647647
public:
648648
// socket
649-
std::atomic<ServiceFlags> nServices;
649+
std::atomic<ServiceFlags> nServices{NODE_NONE};
650650
SOCKET hSocket GUARDED_BY(cs_hSocket);
651-
size_t nSendSize; // total size of all vSendMsg entries
652-
size_t nSendOffset; // offset inside the first vSendMsg already sent
653-
uint64_t nSendBytes GUARDED_BY(cs_vSend);
651+
size_t nSendSize{0}; // total size of all vSendMsg entries
652+
size_t nSendOffset{0}; // offset inside the first vSendMsg already sent
653+
uint64_t nSendBytes GUARDED_BY(cs_vSend){0};
654654
std::deque<std::vector<unsigned char>> vSendMsg GUARDED_BY(cs_vSend);
655655
CCriticalSection cs_vSend;
656656
CCriticalSection cs_hSocket;
657657
CCriticalSection cs_vRecv;
658658

659659
CCriticalSection cs_vProcessMsg;
660660
std::list<CNetMessage> vProcessMsg GUARDED_BY(cs_vProcessMsg);
661-
size_t nProcessQueueSize;
661+
size_t nProcessQueueSize{0};
662662

663663
CCriticalSection cs_sendProcessing;
664664

665665
std::deque<CInv> vRecvGetData;
666-
uint64_t nRecvBytes GUARDED_BY(cs_vRecv);
667-
std::atomic<int> nRecvVersion;
666+
uint64_t nRecvBytes GUARDED_BY(cs_vRecv){0};
667+
std::atomic<int> nRecvVersion{INIT_PROTO_VERSION};
668668

669-
std::atomic<int64_t> nLastSend;
670-
std::atomic<int64_t> nLastRecv;
669+
std::atomic<int64_t> nLastSend{0};
670+
std::atomic<int64_t> nLastRecv{0};
671671
const int64_t nTimeConnected;
672-
std::atomic<int64_t> nTimeOffset;
672+
std::atomic<int64_t> nTimeOffset{0};
673673
// Address of this peer
674674
const CAddress addr;
675675
// Bind address of our side of the connection
676676
const CAddress addrBind;
677-
std::atomic<int> nVersion;
677+
std::atomic<int> nVersion{0};
678678
// strSubVer is whatever byte array we read from the wire. However, this field is intended
679679
// to be printed out, displayed to humans in various forms and so on. So we sanitize it and
680680
// store the sanitized version in cleanSubVer. The original should be used when dealing with
681681
// the network or wire types and the cleaned string used when displayed or logged.
682682
std::string strSubVer GUARDED_BY(cs_SubVer), cleanSubVer GUARDED_BY(cs_SubVer);
683683
CCriticalSection cs_SubVer; // used for both cleanSubVer and strSubVer
684-
bool fWhitelisted; // This peer can bypass DoS banning.
685-
bool fFeeler; // If true this node is being used as a short lived feeler.
686-
bool fOneShot;
687-
bool m_manual_connection;
688-
bool fClient;
689-
bool m_limited_node; //after BIP159
684+
bool fWhitelisted{false}; // This peer can bypass DoS banning.
685+
bool fFeeler{false}; // If true this node is being used as a short lived feeler.
686+
bool fOneShot{false};
687+
bool m_manual_connection{false};
688+
bool fClient{false}; // set by version message
689+
bool m_limited_node{false}; //after BIP159, set by version message
690690
const bool fInbound;
691-
std::atomic_bool fSuccessfullyConnected;
692-
std::atomic_bool fDisconnect;
691+
std::atomic_bool fSuccessfullyConnected{false};
692+
std::atomic_bool fDisconnect{false};
693693
// We use fRelayTxes for two purposes -
694694
// a) it allows us to not relay tx invs before receiving the peer's version message
695695
// b) the peer may tell us in its version message that we should not relay tx invs
696696
// unless it loads a bloom filter.
697-
bool fRelayTxes GUARDED_BY(cs_filter);
698-
bool fSentAddr;
697+
bool fRelayTxes GUARDED_BY(cs_filter){false};
698+
bool fSentAddr{false};
699699
CSemaphoreGrant grantOutbound;
700700
mutable CCriticalSection cs_filter;
701701
std::unique_ptr<CBloomFilter> pfilter PT_GUARDED_BY(cs_filter);
702-
std::atomic<int> nRefCount;
702+
std::atomic<int> nRefCount{0};
703703

704704
const uint64_t nKeyedNetGroup;
705-
std::atomic_bool fPauseRecv;
706-
std::atomic_bool fPauseSend;
705+
std::atomic_bool fPauseRecv{false};
706+
std::atomic_bool fPauseSend{false};
707707

708708
protected:
709709
mapMsgCmdSize mapSendBytesPerMsgCmd;
710710
mapMsgCmdSize mapRecvBytesPerMsgCmd GUARDED_BY(cs_vRecv);
711711

712712
public:
713713
uint256 hashContinue;
714-
std::atomic<int> nStartingHeight;
714+
std::atomic<int> nStartingHeight{-1};
715715

716716
// flood relay
717717
std::vector<CAddress> vAddrToSend;
718718
CRollingBloomFilter addrKnown;
719-
bool fGetAddr;
719+
bool fGetAddr{false};
720720
std::set<uint256> setKnown;
721-
int64_t nNextAddrSend GUARDED_BY(cs_sendProcessing);
722-
int64_t nNextLocalAddrSend GUARDED_BY(cs_sendProcessing);
721+
int64_t nNextAddrSend GUARDED_BY(cs_sendProcessing){0};
722+
int64_t nNextLocalAddrSend GUARDED_BY(cs_sendProcessing){0};
723723

724724
// inventory based relay
725725
CRollingBloomFilter filterInventoryKnown GUARDED_BY(cs_inventory);
@@ -733,35 +733,35 @@ class CNode
733733
CCriticalSection cs_inventory;
734734
std::set<uint256> setAskFor;
735735
std::multimap<int64_t, CInv> mapAskFor;
736-
int64_t nNextInvSend;
736+
int64_t nNextInvSend{0};
737737
// Used for headers announcements - unfiltered blocks to relay
738738
std::vector<uint256> vBlockHashesToAnnounce GUARDED_BY(cs_inventory);
739739
// Used for BIP35 mempool sending
740-
bool fSendMempool GUARDED_BY(cs_inventory);
740+
bool fSendMempool GUARDED_BY(cs_inventory){false};
741741

742742
// Last time a "MEMPOOL" request was serviced.
743-
std::atomic<int64_t> timeLastMempoolReq;
743+
std::atomic<int64_t> timeLastMempoolReq{0};
744744

745745
// Block and TXN accept times
746-
std::atomic<int64_t> nLastBlockTime;
747-
std::atomic<int64_t> nLastTXTime;
746+
std::atomic<int64_t> nLastBlockTime{0};
747+
std::atomic<int64_t> nLastTXTime{0};
748748

749749
// Ping time measurement:
750750
// The pong reply we're expecting, or 0 if no pong expected.
751-
std::atomic<uint64_t> nPingNonceSent;
751+
std::atomic<uint64_t> nPingNonceSent{0};
752752
// Time (in usec) the last ping was sent, or 0 if no ping was ever sent.
753-
std::atomic<int64_t> nPingUsecStart;
753+
std::atomic<int64_t> nPingUsecStart{0};
754754
// Last measured round-trip time.
755-
std::atomic<int64_t> nPingUsecTime;
755+
std::atomic<int64_t> nPingUsecTime{0};
756756
// Best measured round-trip time.
757-
std::atomic<int64_t> nMinPingUsecTime;
757+
std::atomic<int64_t> nMinPingUsecTime{std::numeric_limits<int64_t>::max()};
758758
// Whether a ping is requested.
759-
std::atomic<bool> fPingQueued;
759+
std::atomic<bool> fPingQueued{false};
760760
// Minimum fee rate with which to filter inv's to this node
761-
CAmount minFeeFilter GUARDED_BY(cs_feeFilter);
761+
CAmount minFeeFilter GUARDED_BY(cs_feeFilter){0};
762762
CCriticalSection cs_feeFilter;
763-
CAmount lastSentFeeFilter;
764-
int64_t nextSendTimeFeeFilter;
763+
CAmount lastSentFeeFilter{0};
764+
int64_t nextSendTimeFeeFilter{0};
765765

766766
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress &addrBindIn, const std::string &addrNameIn = "", bool fInboundIn = false);
767767
~CNode();
@@ -774,7 +774,7 @@ class CNode
774774
// Services offered to this peer
775775
const ServiceFlags nLocalServices;
776776
const int nMyStartingHeight;
777-
int nSendVersion;
777+
int nSendVersion{0};
778778
std::list<CNetMessage> vRecvMsg; // Used only by SocketHandler thread
779779

780780
mutable CCriticalSection cs_addrName;

0 commit comments

Comments
 (0)