From 0bf3e67162ef83d2fdf760bf2e96991af74f0720 Mon Sep 17 00:00:00 2001 From: jpk68 Date: Fri, 7 Aug 2026 15:24:40 -0400 Subject: [PATCH] epee: replace some fragile code --- contrib/epee/include/net/abstract_tcp_server2.inl | 7 +++++-- contrib/epee/src/network_throttle-detail.cpp | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl index 3473b1a37..0a64e7811 100644 --- a/contrib/epee/include/net/abstract_tcp_server2.inl +++ b/contrib/epee/include/net/abstract_tcp_server2.inl @@ -87,7 +87,8 @@ namespace net_utils static std::mutex hosts_mutex; std::lock_guard guard(hosts_mutex); static std::map hosts; - unsigned int &val = hosts[m_host]; + auto it = hosts.emplace(m_host, 0).first; + unsigned int &val = it->second; if (delta > 0) MTRACE("New connection from host " << m_host << ": " << val); else if (delta < 0) @@ -95,7 +96,9 @@ namespace net_utils CHECK_AND_ASSERT_THROW_MES(delta >= 0 || val >= (unsigned)-delta, "Count would go negative"); CHECK_AND_ASSERT_THROW_MES(delta <= 0 || val <= std::numeric_limits::max() - (unsigned)delta, "Count would wrap"); val += delta; - return val; + const unsigned int ret = val; + if (ret == 0) hosts.erase(it); + return ret; } template diff --git a/contrib/epee/src/network_throttle-detail.cpp b/contrib/epee/src/network_throttle-detail.cpp index 86a6a5a5b..f23578046 100644 --- a/contrib/epee/src/network_throttle-detail.cpp +++ b/contrib/epee/src/network_throttle-detail.cpp @@ -345,7 +345,7 @@ size_t network_throttle::get_recommended_size_of_planned_transport() const { double network_throttle::get_current_speed() const { unsigned int bytes_transferred = 0; - if (m_history.size() == 0 || m_slot_size == 0) + if (m_history.size() <= 1 || m_slot_size == 0) return 0; auto it = m_history.begin();