From f98f8b10d10d66c8236c12ba2b49ac63c26a6b5c Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 4 Sep 2025 13:16:51 +0100 Subject: [PATCH] refactor: Construct std::string outside critical section There's no need to hold the mutex while constructing a `std::string` from the `char` string. Also use `inet_ntop` instead of the potentially thread-unsafe `inet_ntoa` that might use a static buffer for the result. This makes the `INet` case match the `INet6` case below. --- src/lib/arch/unix/ArchNetworkBSD.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib/arch/unix/ArchNetworkBSD.cpp b/src/lib/arch/unix/ArchNetworkBSD.cpp index 9f5e95c87..21fd9c77a 100644 --- a/src/lib/arch/unix/ArchNetworkBSD.cpp +++ b/src/lib/arch/unix/ArchNetworkBSD.cpp @@ -590,10 +590,13 @@ std::string ArchNetworkBSD::addrToString(ArchNetAddress addr) switch (getAddrFamily(addr)) { case INet: { + char strAddr[INET_ADDRSTRLEN]; const auto *ipAddr = TYPED_ADDR(struct sockaddr_in, addr); - std::scoped_lock lock{m_mutex}; - std::string s = inet_ntoa(ipAddr->sin_addr); - return s; + { + std::scoped_lock lock{m_mutex}; + inet_ntop(AF_INET, &ipAddr->sin_addr, strAddr, INET_ADDRSTRLEN); + } + return strAddr; } case INet6: {