Skip to content

Commit fcb732b

Browse files
pstratemFuzzbawls
authored andcommitted
Refactor: Bail early in AcceptConnection
1 parent 411766d commit fcb732b

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/net.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -912,24 +912,36 @@ static void AcceptConnection(const ListenSocket& hListenSocket) {
912912
int nErr = WSAGetLastError();
913913
if (nErr != WSAEWOULDBLOCK)
914914
LogPrintf("socket error accept failed: %s\n", NetworkErrorString(nErr));
915-
} else if (!IsSelectableSocket(hSocket)) {
915+
return;
916+
}
917+
918+
if (!IsSelectableSocket(hSocket)) {
916919
LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
917920
CloseSocket(hSocket);
918-
} else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS) {
921+
return;
922+
}
923+
924+
if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS) {
919925
LogPrint(BCLog::NET, "connection from %s dropped (full)\n", addr.ToString());
920926
CloseSocket(hSocket);
921-
} else if (CNode::IsBanned(addr) && !whitelisted) {
927+
return;
928+
}
929+
930+
if (CNode::IsBanned(addr) && !whitelisted) {
922931
LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
923932
CloseSocket(hSocket);
924-
} else {
925-
CNode* pnode = new CNode(hSocket, addr, "", true);
926-
pnode->AddRef();
927-
pnode->fWhitelisted = whitelisted;
933+
return;
934+
}
928935

929-
{
930-
LOCK(cs_vNodes);
931-
vNodes.push_back(pnode);
932-
}
936+
CNode* pnode = new CNode(hSocket, addr, "", true);
937+
pnode->AddRef();
938+
pnode->fWhitelisted = whitelisted;
939+
940+
LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
941+
942+
{
943+
LOCK(cs_vNodes);
944+
vNodes.push_back(pnode);
933945
}
934946
}
935947

0 commit comments

Comments
 (0)