Skip to content

Commit ef26f2f

Browse files
committed
net: mark CNode unique_ptr members as const
Dereferencing a unique_ptr is not necessarily thread safe. The reason these are safe is because their values are set at construction and do not change later; so mark them as const and set them via the initializer list to guarantee that.
1 parent bbec32c commit ef26f2f

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/net.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,9 @@ CNode::CNode(NodeId idIn,
27232723
ConnectionType conn_type_in,
27242724
bool inbound_onion,
27252725
std::unique_ptr<i2p::sam::Session>&& i2p_sam_session)
2726-
: m_sock{sock},
2726+
: m_deserializer{std::make_unique<V1TransportDeserializer>(V1TransportDeserializer(Params(), idIn, SER_NETWORK, INIT_PROTO_VERSION))},
2727+
m_serializer{std::make_unique<V1TransportSerializer>(V1TransportSerializer())},
2728+
m_sock{sock},
27272729
m_connected{GetTime<std::chrono::seconds>()},
27282730
addr{addrIn},
27292731
addrBind{addrBindIn},
@@ -2746,9 +2748,6 @@ CNode::CNode(NodeId idIn,
27462748
} else {
27472749
LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
27482750
}
2749-
2750-
m_deserializer = std::make_unique<V1TransportDeserializer>(V1TransportDeserializer(Params(), id, SER_NETWORK, INIT_PROTO_VERSION));
2751-
m_serializer = std::make_unique<V1TransportSerializer>(V1TransportSerializer());
27522751
}
27532752

27542753
bool CConnman::NodeFullyConnected(const CNode* pnode)

src/net.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ class CNode
341341
friend struct ConnmanTestMsg;
342342

343343
public:
344-
std::unique_ptr<TransportDeserializer> m_deserializer;
345-
std::unique_ptr<const TransportSerializer> m_serializer;
344+
const std::unique_ptr<TransportDeserializer> m_deserializer; // Used only by SocketHandler thread
345+
const std::unique_ptr<const TransportSerializer> m_serializer;
346346

347347
NetPermissionFlags m_permissionFlags{NetPermissionFlags::None};
348348

0 commit comments

Comments
 (0)