refactor: Return std::optional from GetNameProxy
What changed, and why it matters
This commit is a straightforward code cleanup (refactor) that changes how a proxy configuration helper returns its result. It switches from an old-style output parameter and boolean success flag to a modern C++ optional return value. There is no security-relevant behavior change visible in the diff.
No security action needed. Treat as normal maintenance/refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors GetNameProxy() to return std::optional
Changed components
src/netbase.cppsrc/netbase.hsrc/net.cppInspect captured patch +16 / −13
diff --git a/src/net.cpp b/src/net.cpp
index 4f88aa8a..9dd95dab 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -434,7 +434,6 @@ CNode* CConnman::ConnectNode(CAddress addrConnect,
// Connect
std::unique_ptr<Sock> sock;
- Proxy proxy;
CService addr_bind;
assert(!addr_bind.IsValid());
std::unique_ptr<i2p::sam::Session> i2p_transient_session;
@@ -442,6 +441,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect,
for (auto& target_addr: connect_to) {
if (target_addr.IsValid()) {
bool use_proxy;
+ Proxy proxy;
if (proxy_override.has_value()) {
use_proxy = true;
proxy = proxy_override.value();
@@ -495,12 +495,14 @@ CNode* CConnman::ConnectNode(CAddress addrConnect,
// the proxy, mark this as an attempt.
addrman.get().Attempt(target_addr, fCountFailure);
}
- } else if (pszDest && GetNameProxy(proxy)) {
- std::string host;
- uint16_t port{default_port};
- SplitHostPort(std::string(pszDest), port, host);
- bool proxyConnectionFailed;
- sock = ConnectThroughProxy(proxy, host, port, proxyConnectionFailed);
+ } else if (pszDest) {
+ if (const auto name_proxy = GetNameProxy()) {
+ std::string host;
+ uint16_t port{default_port};
+ SplitHostPort(std::string(pszDest), port, host);
+ bool proxyConnectionFailed;
+ sock = ConnectThroughProxy(*name_proxy, host, port, proxyConnectionFailed);
+ }
}
// Check any other resolved address (if any) if we fail to connect
if (!sock) {
diff --git a/src/netbase.cpp b/src/netbase.cpp
index c1c03c57..f7339a33 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -723,12 +723,13 @@ bool SetNameProxy(const Proxy &addrProxy) {
return true;
}
-bool GetNameProxy(Proxy &nameProxyOut) {
+std::optional<Proxy> GetNameProxy()
+{
LOCK(g_proxyinfo_mutex);
- if(!nameProxy.IsValid())
- return false;
- nameProxyOut = nameProxy;
- return true;
+ if (!nameProxy.IsValid()) {
+ return std::nullopt;
+ }
+ return nameProxy;
}
bool HaveNameProxy() {
diff --git a/src/netbase.h b/src/netbase.h
index 6e538a73..27e1e59d 100644
--- a/src/netbase.h
+++ b/src/netbase.h
@@ -199,7 +199,7 @@ bool IsProxy(const CNetAddr &addr);
*/
bool SetNameProxy(const Proxy &addrProxy);
bool HaveNameProxy();
-bool GetNameProxy(Proxy &nameProxyOut);
+std::optional<Proxy> GetNameProxy();
using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
extern DNSLookupFn g_dns_lookup;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.