Skip to content

Commit 7583eac

Browse files
committedAug 13, 2024··
Merge bitcoin#30617: net: Clarify that m_addr_local is only set once
fa6fe43 net: Clarify that m_addr_local is only set once (MarcoFalke) Pull request description: The function is supposed to be only called once when the version msg arrives (a single time). Calling it twice would be an internal logic bug. However, the `LogError` in this function has many issues: * If the error happens in tests, as is the case for the buggy fuzz test, it will go unnoticed * It is dead code, unless a bug is introduced to execute it Fix all issues by using `Assume(!m_addr_local.IsValid())` instead. Idea taken from bitcoin#30364 (comment) ACKs for top commit: achow101: ACK fa6fe43 mzumsande: utACK fa6fe43 glozow: ACK fa6fe43 Tree-SHA512: 8c1e8c524768f4f36cc50110ae54ee423e057a963ff78f736f3bf92df1ce5af28e3e0149153780897944e1d5c22ddbca9dac9865d9f4d44afffa152bc8559405
2 parents 5fdbc8b + fa6fe43 commit 7583eac

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed
 

‎src/net.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -576,16 +576,14 @@ CService CNode::GetAddrLocal() const
576576
{
577577
AssertLockNotHeld(m_addr_local_mutex);
578578
LOCK(m_addr_local_mutex);
579-
return addrLocal;
579+
return m_addr_local;
580580
}
581581

582582
void CNode::SetAddrLocal(const CService& addrLocalIn) {
583583
AssertLockNotHeld(m_addr_local_mutex);
584584
LOCK(m_addr_local_mutex);
585-
if (addrLocal.IsValid()) {
586-
LogError("Addr local already set for node: %i. Refusing to change from %s to %s\n", id, addrLocal.ToStringAddrPort(), addrLocalIn.ToStringAddrPort());
587-
} else {
588-
addrLocal = addrLocalIn;
585+
if (Assume(!m_addr_local.IsValid())) { // Addr local can only be set once during version msg processing
586+
m_addr_local = addrLocalIn;
589587
}
590588
}
591589

‎src/net.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ class CNode
963963
size_t m_msg_process_queue_size GUARDED_BY(m_msg_process_queue_mutex){0};
964964

965965
// Our address, as reported by the peer
966-
CService addrLocal GUARDED_BY(m_addr_local_mutex);
966+
CService m_addr_local GUARDED_BY(m_addr_local_mutex);
967967
mutable Mutex m_addr_local_mutex;
968968

969969
mapMsgTypeSize mapSendBytesPerMsgType GUARDED_BY(cs_vSend);

‎src/test/fuzz/net.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ FUZZ_TARGET(net, .init = initialize_net)
3333
SetMockTime(ConsumeTime(fuzzed_data_provider));
3434
CNode node{ConsumeNode(fuzzed_data_provider)};
3535
node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
36+
if (const auto service_opt =
37+
ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider)))
38+
{
39+
node.SetAddrLocal(*service_opt);
40+
}
3641
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
3742
CallOneOf(
3843
fuzzed_data_provider,
@@ -52,13 +57,6 @@ FUZZ_TARGET(net, .init = initialize_net)
5257
node.Release();
5358
}
5459
},
55-
[&] {
56-
const std::optional<CService> service_opt = ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider));
57-
if (!service_opt) {
58-
return;
59-
}
60-
node.SetAddrLocal(*service_opt);
61-
},
6260
[&] {
6361
const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
6462
bool complete;

0 commit comments

Comments
 (0)
Please sign in to comment.